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
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Your destination sheet is the same as your source sheet.... correct?
 
Upvote 0
So are you just converting the cells to values? if yes then try

VBA Code:
Sub Title()
    Dim srcWS As Worksheet
 
    Set srcWS = Workbooks("Inventory Import (Full) - Washout Template.xls").Worksheets("Routing Info")
 
    With srcWS.Range("A2:BI" & srcWS.Range("A" & Rows.Count).End(xlUp).Row)
        .Value = .Value
    End With

End Sub

If something else then please elaborate as at the moment it looks like your code could be overwriting the cells in column B.

Literally going by what your code looks like it would be

VBA Code:
Sub Title2()
    Dim srcWS As Worksheet
  
    Set srcWS = Workbooks("Inventory Import (Full) - Washout Template.xls").Worksheets("Routing Info")
 
    With srcWS.Range("A2:BI" & srcWS.Range("A" & Rows.Count).End(xlUp).Row)
        srcWS.Cells(srcWS.Rows.Count, "B").End(xlUp).Offset(1).Resize(.Rows.Count, .Columns.Count).Value = .Value
    End With

End Sub
 
Last edited:
Upvote 0
Please note I have edited the last code as I missed a period / full stop
 
Upvote 0
Hi Mark

Thank you,

To explain further I have a list of part numbers in column A, the part number is repeated 3 times - so A2 to A4 and so on for the next part number.

The above code is on a button in the <span>"Routing Info"</span> worksheet and copies the range <span>"B2:AI5"</span> then pastes it into the next empty cell in column B (so 4 rows of data line up to the 4 part numbers), I then repeatedly push this button until the last set of part numbers in A.

I am trying to change the code so that it pastes the range <span>"B2:AI5"</span> repeatedly until the last set of part numbers, this is where i am getting lost.

Hope this makes sense
 
Upvote 0
If I've read it right try the code below on a copy of your data

VBA Code:
Sub Title3()
    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
        With srcWS.Range("B2:AI5")
            srcWS.Cells(i, "B").Resize(.Rows.Count, .Columns.Count).Value = .Value
        End With
    Next

End Sub
 
Upvote 0
Hi Mark

Tried the code - see result below, it is pasting the first row multiple times then the rest on the last 3 rows.

1735046424492.png
 
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