vba, loop,

bahllr

Board Regular
Joined
May 7, 2009
Messages
62
I have written the below code (as part of a larger code set) to copy a range and paste it in cell Z4 if Z4 is empty. If it isn't empty, I want it to move one cell to the right and paste it in AA4. This should be a loop pasting one cell to the right of the last filled cell.

I have come up with the following which moves one cell to the right, but only once. It doesn't loop. Also, this loop operates within a larger loop....is there anything I must consider for it to be successful?

Can you help me accomplish what I am trying to do?

Range("z4").Select
If Range("z4") = "" Then
ActiveSheet.Paste
Application.CutCopyMode = False
Else

i = 1
Range("z4").Offset(0, i).Select
i = i + 1

ActiveSheet.Paste
Application.CutCopyMode = False
End If
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Code:
Sub PasteMe()
Dim i As Long
i = 26
Redo:
If Cells(4, i) = "" Then
   Cells(1, 1).Copy Cells(4, i)
   Exit Sub
Else
   i = i + 1
   GoTo Redo
End If
End Sub

Change the Cells(1,1) to the Range you want to copy
lenze
 
Upvote 0
Do you just need row 4, and how far to the right will it need to loop?

Is there a case where Z4 will have something, it looks to paste in AA4, but AA4 might have something already? Would you skip over AA4 in this case or overwrite it?
 
Upvote 0
FWIW, if you're pasting to the rightmost cell in row 4, you could use:

Code:
Cells(4, Columns.Count).End(xlToLeft).Offset(0, 1) = Cells(1,1)

and skip the looping all together.
 
Upvote 0
thank you for the responses. As clarification:

1. the code before this section I am trying to fix copies a range
2. I want it pasted in cell Z4 (this is the first place something will be pasted)
3. loop operation, reselect a range to be copied
4. paste in the cell to the right of z4
5 continue to loop and move one cell right of the last filled cell in the 4th row

There will never be filled cells in the 4th row from column z on.

There will be at most, 100 loops possible here

Hope that helps explain what I am trying to do.

Thanks.
 
Upvote 0
Sorry. I am copying a dynamic sized range starting at B23. I use the following code to sort it for unique values before copying it.

Range("B23").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.Sort Key1:=Range("B23"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range(Selection, Selection.End(xlDown)).AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Selection.Copy
 
Upvote 0
Code:
Range(Selection, Selection.End(xlDown)).AdvancedFilter Action:=xlFilterInPlace, Unique:=True
'Selection.Copy  <!-- / message -->
Dim i As Long
i = 26
Redo:
If Cells(4, i) = "" Then
   Selection.Copy Cells(4, i)
   Exit Sub
Else
   i = i + 1
   GoTo Redo
End if

lenze
 
Upvote 0
Thanks everyone for your help! I think between everyones input, I have figured out how to put it together.
 
Upvote 0
You could use Oaktree's idea
Code:
Range(Selection, Selection.End(xlDown)).AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Selection.Copy  Cells(4, Columns.Count).End(xlToLeft).Offset(0, 1)
No loop

lenze<!-- / message -->
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,853
Members
449,194
Latest member
HellScout

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