Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/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
只要列相同,就可以在Excel文件中循环_Excel_Vba - Fatal编程技术网

只要列相同,就可以在Excel文件中循环

只要列相同,就可以在Excel文件中循环,excel,vba,Excel,Vba,我有一个Excel文件,A列是名字,B列是月份。 同样的(A,B):汤姆-简-汤姆-简-汤姆-十一月-汤姆-十二月-杰克-九月-杰克-十二月-杰克-八月 我现在正试着通过这个循环。我已经克服了第一个障碍,用这个“脚本”循环直到专栏的(未知)结尾 Sub-DoWhile() 我想我会坚持多久 Dim c作为集合 集合c=新集合 c、 添加单元格(1,1),“名称” c、 添加单元格(1,2),“月” c、 添加单元格(1,3),“体积” i=2 使用ActiveSheet 当我时,我想这更简单:

我有一个Excel文件,A列是名字,B列是月份。 同样的(A,B):汤姆-简-汤姆-简-汤姆-十一月-汤姆-十二月-杰克-九月-杰克-十二月-杰克-八月

我现在正试着通过这个循环。我已经克服了第一个障碍,用这个“脚本”循环直到专栏的(未知)结尾

Sub-DoWhile()
我想我会坚持多久
Dim c作为集合
集合c=新集合
c、 添加单元格(1,1),“名称”
c、 添加单元格(1,2),“月”
c、 添加单元格(1,3),“体积”
i=2
使用ActiveSheet

当我时,我想这更简单:

Dim i As Long
Dim nA(1 To 10000) As String
Dim nB(1 To 10000) As String

i = 2
e = 0
With ActiveSheet
    Do While i <= .Rows.Count
        If .Cells(i, 1) <> "" Then
            If Cells(i - 1, 1) = Cells(i, 1) Then
                nB(e) = nB(e) & " - " & Cells(i, 2)
            Else
                e = e + 1
                nA(e) = Cells(i, 1)
                nB(e) = Cells(i, 2)
            End If
            MsgBox nA(e) & " : " & nB(e)
        Else
            Exit Do
        End If
        i = i + 1
    Loop
End With
Dim i尽可能长
尺寸nA(1到10000)作为字符串
尺寸nB(1至10000)为字符串
i=2
e=0
使用ActiveSheet

当我时,我建议使用下面的方法。代码将找到当前区域,然后根据A1中的行和前3列调整大小。然后我将打印到MsgBox,但如果需要,您可以将这些内容粘贴到另一个工作表上

Dim arr As Variant
Dim i As Integer

arr = Range("A1").CurrentRegion.Resize(Range("A1").End(xlDown).Row, 3).Value

For i = 1 To UBound(arr)

    MsgBox arr(i, 1)
    MsgBox arr(i, 2)
    MsgBox arr(i, 3)

Next i
Dim arr As Variant
Dim i As Integer

arr = Range("A1").CurrentRegion.Resize(Range("A1").End(xlDown).Row, 3).Value

For i = 1 To UBound(arr)

    MsgBox arr(i, 1)
    MsgBox arr(i, 2)
    MsgBox arr(i, 3)

Next i