gpt4 book ai didi

vba - 如何将字符串拆分为多个单元格的单元格?

转载 作者:行者123 更新时间:2023-12-02 23:22:51 26 4
gpt4 key购买 nike

我希望我的代码能够遍历包含名称的单元格列表,并将它们拆分到原始单元格旁边的单元格中。我有一些基本代码来完成第一步,但我正在努力让它循环遍历列表的其余部分,并将其输出到原始代码旁边,而不是像目前那样在 A1 中输出。我认为这是代码的“Cell”部分的问题,但我无法完全修复它。

Sub NameSplit()

Dim txt As String
Dim i As Integer
Dim FullName As Variant
Dim x As String, cell As Range

txt = ActiveCell.Value

FullName = Split(txt, " ")

For i = 0 To UBound(FullName)

Cells(1, i + 1).Value = FullName(i)

Next i


End Sub

最佳答案

对名称值范围使用 For Each 循环。在本例中,我只是假设它们位于第一列,但您可以进行相应调整:

Sub NameSplit()

Dim txt As String
Dim i As Integer
Dim FullName As Variant
Dim x As String, cell As Range

For Each cell In ActiveSheet.Range(Cells(1,1),Cells(ActiveSheet.UsedRange.Count,1))
txt = cell.Value

FullName = Split(txt, " ")

For i = 0 To UBound(FullName)

cell.offset(0,i + 1).Value = FullName(i)

Next i

Next cell

End Sub

关于vba - 如何将字符串拆分为多个单元格的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38261957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com