Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 连接多个列_Vba_Excel - Fatal编程技术网

Vba 连接多个列

Vba 连接多个列,vba,excel,Vba,Excel,我有一张表格,上面列出了从C12开始的多个值,其中前六个数字相同,后三个不同,并用空格分隔(例如123456 111 222 333 444)。我希望使用VBA将单元格中前六位数字的值与该单元格中的三位数字值(例如123456111、123456222、123456333、123456444)组合起来。这些组合值应分别位于各自的单元格中。每个单元格中的三位数数值不同。我在想,也许最简单的方法就是把它们分成几列,然后把它们组合起来,但我想不出来 以下是一个例子: #DATA# 1. 541259

我有一张表格,上面列出了从C12开始的多个值,其中前六个数字相同,后三个不同,并用空格分隔(例如123456 111 222 333 444)。我希望使用VBA将单元格中前六位数字的值与该单元格中的三位数字值(例如123456111、123456222、123456333、123456444)组合起来。这些组合值应分别位于各自的单元格中。每个单元格中的三位数数值不同。我在想,也许最简单的方法就是把它们分成几列,然后把它们组合起来,但我想不出来

以下是一个例子:

#DATA#
1. 541259 139 285
2. 452679 245
3. 894623 455 654

#DESIRED RESULT#
1. 541259139
2. 541259285
3. 452679245
4. 894623455
5. 894623654
我能够使用以下代码将值分离到第二张纸上:

Sub Split()
Dim totalRows As Long, i As Long, sColNames As String
totalRows = LastRowWithData(Sheet1, "C")
For i = 1 To totalRows
    sColNames = Sheet1.Range("C" & i).value
    Call SplitToColumns(sColNames, " ", Sheet2.Range("A" & i))
Next i
End Sub
我不确定这是否是最好的方式,我也不知道如何加入他们


谢谢你抽出时间

以下是我的做法:

Sub SplitMyNum()
    Dim i&, j&
    Dim splt() As String
    Dim rngArr() As Variant
    Dim oWs As Worksheet
    Dim tWs As Worksheet

    Set oWs = Worksheets("Sheet1") 'change to your input
    Set tWs = Worksheets("Sheet2") 'change to your output sheet, may be the same as above

    With oWs
        rngArr = .Range(.Cells(1, 3), .Cells(.Rows.Count, 3).End(xlUp)).Value 'loads all the numbers into an array
    End With
    With tWs
        For i = LBound(rngArr, 1) To UBound(rngArr, 1) 'iterate through the numbers
            splt = Split(rngArr(i, 1), " ") 'Split the numbers on spaces
            For j = LBound(splt) + 1 To UBound(splt) 'iterates through the split values
                'Next line concatenates the first with each set after and puts it in the next available cell in column A.
                .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Value = splt(LBound(splt)) & splt(j)
            Next j
        Next i
    End With
End Sub

我可能会先粘贴到文本编辑器中,然后对空格字符进行查找/替换。将其替换为
选项卡
字符。然后将其粘贴回Excel中,至少可以完成解析。我会让其他人插话,告诉他们如何组合这些字符串:)