Share via

vba loop based on cell value

Anonymous
2018-01-31T21:58:52+00:00

Hello,

I want to be able to create a list based on my sheet 'output' on values from the 'list' sheet. The complication is that the list sheet contains a value in column B that I want to use to define the number of rows allocated to that record when it is created on the 'output' sheet. The image below shows my starting 'list' data and what I want on my 'output' sheet:

Any help would be awesome.

Cheers,

Mark

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2018-01-31T22:30:13+00:00

For the worksheets and data as shown:

Sub test()

    Dim rngC As Range

    Dim wsS As Worksheet

    Dim wsT As Worksheet

    Dim i As Integer

    Dim lngR As Long

    Set wsS = Worksheets("List")

    Set wsT = Worksheets("Output")

    For Each rngC In wsS.Range(wsS.Range("B2"), wsS.Cells(wsS.Rows.Count, "B").End(xlUp))

        For i = 1 To rngC.Value

            With wsT

                lngR = .Cells(.Rows.Count, "A").End(xlUp).Row + 1

                .Cells(lngR, "A").Value = wsS.Range("H1").Value

                .Cells(lngR, "B").Value = rngC.Offset(0, -1).Value

            End With

        Next i

    Next rngC

End Sub

Was this answer helpful?

3 people found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2018-02-01T06:16:07+00:00

    Bernie, you're the MAN.

    Thanks a lot, that's going to save me heaps of time.

    Cheers,

    Mark

    Was this answer helpful?

    0 comments No comments