VBA - failed to use loop

striving12345

New Member
Joined
Sep 25, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I have the following code from a recorded macro and need to copy>paste special>print for approximately 100 rows down until a blank cell is detected.
- Have tried to edit to loop but failed.

Am very new to VBA. Would appreciate the help.


Sub Macro3()
'
' Macro3 Macro
'

'
Range("C2").Select
Selection.Copy
Range("K2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("SO").Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False
Sheets("Extract").Select
Range("C3").Select
Application.CutCopyMode = False
Selection.Copy
Range("K2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("SO").Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False
Sheets("Extract").Select
Range("C4").Select
Application.CutCopyMode = False
Selection.Copy
Range("K2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("SO").Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
This is what I tried for just the copy and paste special portion but there was an error in "Sheets("Extract").Range("K2").Value = Sheets("Extract").Range(Cells(r, "A")).Value".

Dim lr As Long
Dim r As Long

'Find last row in column A with data
lr = Sheets("Extract").Cells(Rows.Count, "A").End(xlUp).Row

'Loop through all rows and paste values from A into K2
For r = 2 To lr
Sheets("Extract").Range("K2").Value = Sheets("Extract").Range(Cells(r, "A")).Value
Next r
 
Upvote 0
Managed to figure out the error and also included a print. It works!
- Supported with this thread: Loop to copy/paste next row using VBA

Thank you!


VBA Code:
Sub Macro2()

    Dim lr As Long
    Dim r As Long
   
    'Find last row in column A with data
    lr = Sheets("Extract").Cells(Rows.Count, "A").End(xlUp).Row
    'Loop through all rows and paste values from A into K2
    For r = 2 To lr
        Sheets("Extract").Range("K2").Value = Sheets("Extract").Cells(r, "A").Value
    'Print
    Sheets("SO").Select
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
    Next r

End Sub
 
Last edited by a moderator:
Upvote 0
Glad you got it resolved.

For the future: When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug. (Compare code in posts 1 & 2 v. post 3 where I have added the tags for you this time. 😊)
My signature block below has more details.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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