Looping through code - I'm sure this is simple :(

smilie224

New Member
Joined
Nov 18, 2014
Messages
19
I'm having a problem where I have gotten some code to work to do what I want: Insert the date from column G into the blank row above it in column A.
I can get it to copy/paste the date in column G with an offset into first blank row created, but I can't figure out how to loop it to repeat the command until it runs down the rest of the data set looking for each of the blank row breaks.
I would think it would just be a matter of encompassing the code within a loop somehow, but I can't seem to figure it out. Any help would be greatly appreciated - and hopefully this is a no-brainer for you smart folks out there!!! :)

Dim ws As Worksheet
Set ws = ActiveSheet

For Each cell In ws.Columns(1).Cells
If IsEmpty(cell) = True Then cell.Select: Exit For
Next cell
ActiveCell.Offset(1, 6).Copy
ActiveCell.PasteSpecial xlPasteAll
Application.CutCopyMode = False

For my print screen attached, you can see I got it to work in Cell A2, but I can't figure out how to get it to repeat the code and do it again in the A10, A14, A19 in the sample. (quantity of rows will vary, and is somewhat extensive on the list - this was just a sample so you could see what I mean).
 

Attachments

  • Screenshot 2023-06-02 at 4.41.03 PM.png
    Screenshot 2023-06-02 at 4.41.03 PM.png
    175 KB · Views: 12

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
try this:
VBA Code:
Sub test()
Dim ws As Worksheet
Set ws = ActiveSheet
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
inarr = Range(Cells(1, 1), Cells(lastrow, 7))
For i = 1 To lastrow
If IsEmpty(inarr(i, 1)) Then
Range(Cells(i, 1), Cells(i, 1)) = inarr(i + 1, 7)
End If
Next i
End Sub
 
Upvote 0
try this:
VBA Code:
Sub test()
Dim ws As Worksheet
Set ws = ActiveSheet
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
inarr = Range(Cells(1, 1), Cells(lastrow, 7))
For i = 1 To lastrow
If IsEmpty(inarr(i, 1)) Then
Range(Cells(i, 1), Cells(i, 1)) = inarr(i + 1, 7)
End If
Next i
End Sub
That worked! I guess I was going about it all the wrong way in trying to piece meal it. Thank you so much! I never would have come up with this on my own... it is very much appreciated.
 
Upvote 0
Another option would be to do them all at once with no looping required.

VBA Code:
Sub BlanksToDate()
  With Range("A1", Range("A" & Rows.Count).End(xlUp))
    .SpecialCells(xlBlanks).FormulaR1C1 = "=R[1]C[6]"
    .Value = .Value
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,371
Messages
6,124,529
Members
449,169
Latest member
mm424

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