VBA loop code Error help!

erimhast83

New Member
Joined
Jan 3, 2018
Messages
14
I am running the below code and getting an error at the end when the do untilempty is not working and the code runs until the third line and there are no more rows in the spreadsheet since column G had no data in any cells. Can someone please help me figure out the error in my coding. Thank you.



Do Until IsEmpty("D2:D")


Range("G2").End(xlDown).Select
ActiveCell.Offset(1, -3).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToRight).Offset(0, 3)).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveCell.Offset(-1, 3).Select
Selection.Cut
ActiveCell.Offset(1, -1).Select
ActiveSheet.Paste
ActiveCell.Offset(-1, -1& - 1).Select
Range(Selection, Selection.End(xlToRight).Offset(0, -1)).Select
Selection.Copy
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste

Loop
 
I'll post something when I get in (just don't want to make a typo on my phone). What do you want to base the last row variable on?
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
I'll post something when I get in (just don't want to make a typo on my phone). What do you want to base the last row variable on?

I would like the last row to be based on the last cell with data in the column D. My data will change monthly and i don't want to be tied to a set row number.
 
Upvote 0
Not tested but can you run the below on a copy of your data and let me know what happens please.
I suspect we will need to loop upwards and so be prepared to break the sub.


Code:
Sub test1()
Dim myCell As Range
For Each myCell In Range("D2:D" & Range("D" & Rows.Count).End(xlUp).Row)
If myCell.Value <> "" Then
myCell.Offset(, 3).End(xlDown).Select
ActiveCell.Offset(1, -3).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToRight).Offset(0, 3)).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveCell.Offset(-1, 3).Select
Selection.Cut
ActiveCell.Offset(1, -1).Select
ActiveSheet.Paste
ActiveCell.Offset(-1, -1& - 1).Select
Range(Selection, Selection.End(xlToRight).Offset(0, -1)).Select
Selection.Copy
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste

Next

End Sub
 
Upvote 0
Thank you for your help! I am getting a compile error: Next without for when i try to run the code you sent.


Not tested but can you run the below on a copy of your data and let me know what happens please.
I suspect we will need to loop upwards and so be prepared to break the sub.


Code:
Sub test1()
Dim myCell As Range
For Each myCell In Range("D2:D" & Range("D" & Rows.Count).End(xlUp).Row)
If myCell.Value <> "" Then
myCell.Offset(, 3).End(xlDown).Select
ActiveCell.Offset(1, -3).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToRight).Offset(0, 3)).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveCell.Offset(-1, 3).Select
Selection.Cut
ActiveCell.Offset(1, -1).Select
ActiveSheet.Paste
ActiveCell.Offset(-1, -1& - 1).Select
Range(Selection, Selection.End(xlToRight).Offset(0, -1)).Select
Selection.Copy
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste

Next

End Sub
 
Upvote 0
That's why I said we will probably have to loop backwards because you are inserting lines.
I will have to do some tests with the code you have recorded (I do hate recorded code :() but I probably won't get round to it until the weekend.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,478
Messages
6,125,040
Members
449,205
Latest member
Eggy66

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