关于通过行循环代码的问题,无法正常工作

时间:2019-06-26 06:19:35

标签: excel vba

我有两列数字的表格,数字越小在左边,数字越大在右边。我需要一个遍历每一行并打印A和B列之间的每个数字(包括起点和终点)的代码。打印完这些文件后,需要转到下一行并重复该过程。

我尝试过在线查找,但没有任何帮助

Sub Values_between_dates()
    Application.ScreenUpdating = False
    Dim rng As Range
    Dim row As Range
    Dim cell As Range

    Set rng = Range("z3:AA11")

    For Each row In rng.Rows
        For Each cell In row.Cells
            cell = numone
            Print numone.Range("AB3")
        Next cell

        cell = "numtwo"
        Range("AC3").Select
        Range("AC3") = numone
        numone = numone + 1

        Do While numone < Format(numtwo + 1, "00000000000")
            ActiveCell.Offset(1, 0).Range("A1").Select
            ActiveCell.Value = numone
            numone = Format(numone + 1, "00000000000")    
        Loop
    Next row
End Sub

1 个答案:

答案 0 :(得分:0)

您可以使用:

Option Explicit

Sub test()

    Dim StartPoint As Long, EndPoint As Long, LastRow As Long, i As Long
    Dim str As String

    With ThisWorkbook.Worksheets("Sheet1")

        'Find last row of column A
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

        'Loop from row 1 to lastrow
        For i = 1 To LastRow

            StartPoint = .Range("A" & i).Value
            EndPoint = .Range("B" & i).Value

            Do Until StartPoint = EndPoint + 1

                If str = "" Then
                    str = StartPoint
                Else
                    str = str & ", " & StartPoint
                End If

                StartPoint = StartPoint + 1

            Loop

            'Paste re results in Column C row i
            .Range("C" & i).Value = str

            'Clear str variable
            str = ""

        Next i

    End With

End Sub

结果:

enter image description here