VBA: Copy to last used row in another sheet

tl01092

New Member
Joined
Jan 10, 2020
Messages
4
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi, I am trying to paste from the sheet output to the input sheet in a different workbook, but only after the last used row. My code doesnt seem to work.

Sub CopynPasteWrkBk()

Dim InputFile As Workbook
Dim OutputFile As Workbook
Dim Inputpath As String
Dim Outputpath As String '
Dim ws1 As Worksheet, ws2 As Worksheet


'## Open both workbooks first:
Set InputFile = ThisWorkbook
Set OutputFile = Workbooks.Open("Z:\Datbase.xlsx")
Set ws1 = InputFile.Sheets("Output")
Set ws2 = OutputFile.Sheets("Input")

'Now, copy what you want from InputFile:
InputFile.Sheets("Output").Activate
InputFile.Sheets("Output").Range("A3:T12").Select
Selection.Copy


'Now, paste to OutputFile worksheet:
OutputFile.Sheets("Input").Activate

Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

'Close InputFile & OutputFile:
'OutputFile.Close savechanges:=True


'MsgBox "Data Successfully Logged"


End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Yes that couldnt work properly. Where is it you want it pasted? Column A?
 
Upvote 0
Ok this will paste special into the cell after the last used cell in column A of the target worksheet:

VBA Code:
Sub CopynPasteWrkBk()

Dim InputFile As Workbook
Dim OutputFile As Workbook
Dim Inputpath As String
Dim Outputpath As String '
Dim ws1 As Worksheet, ws2 As Worksheet, lr As Long

'## Open both workbooks first:
Set InputFile = ThisWorkbook
Set OutputFile = Workbooks.Open("Z:\Datbase.xlsx")
Set ws1 = InputFile.Sheets("Output")
Set ws2 = OutputFile.Sheets("Input")

With ws2
    lr = .Range("A" & .Rows.Count).End(xlUp).Row
    ws1.Range("A3:T12").Copy
    .Range("A" & lr + 1).PasteSpecial xlValues
End With

'Close InputFile & OutputFile:
'OutputFile.Close savechanges:=True

'MsgBox "Data Successfully Logged"

End Sub
 
Upvote 0
Hi, thanks for your help. The code works but sometimes I have data in column C and not in column A of the workbook the data is imported to. When I run the code, the data pastes over the data in column c. Can the code be modified so that it only pastes the data after the last used row from column A to C?
 
Upvote 0
Try changing the lr line to:

VBA Code:
lr = .Columns("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 
Upvote 0

Forum statistics

Threads
1,214,790
Messages
6,121,607
Members
449,037
Latest member
Arbind kumar

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