Macro to copy cells to a variable range and then loop

Cam Johnston

New Member
Joined
Apr 11, 2013
Messages
10
Needing a macro that can copy cells on a line in columns B, C and D to a variable range in those same columns defined by the next line of the spreadsheet that has text in columns B, C and D and then repeat. I have tried a number of loops but failed miserably. I have attached images of what the input looks like "before" and desired "after."

Very grateful for any pointers or assistance that can be provided.

Thank you in advance.

BEFORE
Screenshot 2023-07-31 192717.png



AFTER
Screenshot 2023-07-31 193106.png
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Please provide your attempts at VBA code. It may just need some minor tweaking.
 
Upvote 0
Thanks for the quick follow-up. I "attempted" to create a macro capturing the relative references. and then place it in a loop.

Clearly, the relative references is not working as intended :)

Here is the VBA looking forward the constructive feedback

Sub UnknownNumRows()

Range("a15").Select

Do Until IsEmpty(ActiveCell)

Range("B19").Select

Range(Selection, Selection.End(xlToRight)).Select

Range(Selection, Selection.End(xlToLeft)).Select

Range("B19:D19").Select

Selection.Copy

Range(Selection, Selection.End(xlDown)).Select

Range("B19:D33").Select

ActiveSheet.Paste

Selection.End(xlDown).Select

Range("A39").Select

Loop

End Sub
 
Upvote 0
There's a number of different ways you could do this. Here's just one suggestion:
VBA Code:
Option Explicit
Sub CamJohnston()
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<~~ *** Change to actual sheet name ***
    Dim LRow As Long, r As Range, c As Range
    LRow = ws.Cells(Rows.Count, "E").End(xlUp).Row
    Set r = ws.Range("B2:D" & LRow)
    
    For Each c In r
      If c = "" Then
        c.FillDown
      End If
    Next c
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,133
Messages
6,123,235
Members
449,092
Latest member
SCleaveland

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top