VBA to Paste until last cell in column A

Multim

New Member
Joined
Apr 17, 2024
Messages
14
Office Version
  1. 2021
Hello

I have the code below but cant workout how to code it to paste until the last data cell in column A.


VBA Code:
Sub Title()
    Dim srcWS As Worksheet, desWS As Worksheet
    Set srcWS = Workbooks("Inventory Import (Full) - Washout Template.xls").Worksheets("Routing Info")
    Set desWS = Workbooks("Inventory Import (Full) - Washout Template.xls").Worksheets("Routing Info")
    With desWS
        srcWS.Range("B2:AI5").Copy
        .Cells(.Rows.Count, "B").End(xlUp).Offset(1).PasteSpecial xlPasteValues
    End With
    Application.CutCopyMode = False
End Sub
Any help would be appreciated

Thank you
 
Sorry that's my fault

VBA Code:
Sub Title4()
    Dim srcWS As Worksheet, i As Long
    Application.ScreenUpdating = False

    Set srcWS = Workbooks("Inventory Import (Full) - Washout Template.xls").Worksheets("Routing Info")

    For i = 6 To srcWS.Cells(srcWS.Rows.Count, "A").End(xlUp).Row - 3 Step 4
        With srcWS.Range("B2:AI5")
            srcWS.Cells(i, "B").Resize(.Rows.Count, .Columns.Count).Value = .Value
        End With
    Next

End Sub
 
Upvote 0
Solution

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Thank you, Spot on Mark

I am still learning, does the "Step" part of the line instruct to paste every 4 lines?

Thanks again
 
Upvote 0
You're welcome

I am still learning, does the "Step" part of the line instruct to paste every 4 lines?
It tells it to increase the counter (i in this case) by 4 on each iteration, when it is omitted it defaults to 1.
i in this case is the row number
 
Upvote 0
So if i had 6 rows of data to paste (sometimes I do) I change the range data and the the step to 6?
 
Upvote 0
Yes, or if you are being clever use something like

VBA Code:
x = srcWS.Range("B2:B" & srcWS.Range("B" & srcWS.Rows.Count).End(xlUp).Row).Rows.Count
then base everything on x.

Something like

VBA Code:
Sub TitleNew()
    Dim srcWS As Worksheet, i As Long, x As Long
    Application.ScreenUpdating = False

    Set srcWS = Workbooks("Inventory Import (Full) - Washout Template.xls").Worksheets("Routing Info")

    x = srcWS.Range("B2:B" & srcWS.Range("B" & srcWS.Rows.Count).End(xlUp).Row).Rows.Count
 
    For i = x + 2 To srcWS.Cells(srcWS.Rows.Count, "A").End(xlUp).Row -(x-1) Step x
        With Range(srcWS.Cells(2, "B"), srcWS.Cells(x + 1, "AI"))
            srcWS.Cells(i, "B").Resize(.Rows.Count, .Columns.Count).Value = .Value
        End With
    Next

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,225,138
Messages
6,183,089
Members
453,147
Latest member
Bree2019

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