-1

I have a table of numbers with two columns, the lower number is on the left and the higher is on the right. I need a code which goes through each row and prints every number between column A and B including the start and end point. After printing those it needs to go to the next row and repeat the process.

I tried looking online but can't get any help

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
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
  • What is wrong with your code? Where did you get stuck? Any errors (if so which and where)? What does your code vs. what did you expect from your code? Please [edit] your original question to add all these information. • Also you might benefit from reading [How to avoid using Select in Excel VBA](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba). – Pᴇʜ Jun 26 '19 at 06:25

1 Answers1

0

You could use:

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

Results:

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Error 1004
  • 7,877
  • 3
  • 23
  • 46