Leeward904
New Member
- Joined
- May 5, 2021
- Messages
- 12
- Office Version
- 2019
- Platform
- Windows
Hello all!
I need to write some VBA to copy specific columns from closed source book to open workbook. I have most of it completed i am stuck on how to add columns to copy and location to paste. I would like for the data in the destination workbook erase the data in the respective columns before pasting new data.
the copy column to paste column are
source column - destination column
B - E
H - H
I - I
J - J
L - K
N - O
O - G
Here is where i am right now:
Any help would be greatly appreciated!
I need to write some VBA to copy specific columns from closed source book to open workbook. I have most of it completed i am stuck on how to add columns to copy and location to paste. I would like for the data in the destination workbook erase the data in the respective columns before pasting new data.
the copy column to paste column are
source column - destination column
B - E
H - H
I - I
J - J
L - K
N - O
O - G
Here is where i am right now:
VBA Code:
Dim wrkMyWorkBook As Workbook
Workbooks.Open Filename:=(Sheets("Data").Range("C3").Value & "\" & "TEC-037 Shop Work Request Log WCD.xlsx")
'Find the last used row in both sheets
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
'Set variables for copy and destination sheets
Set wsCopy = Workbooks("TEC-037 Shop Work Request Log WCD.xlsx").Worksheets(1)
Set wsDest = Workbooks("WC Fabrication Tracker Template 2021.xlsm").Worksheets(1)
'1. Find last used row in the copy range based on data in column A
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
'3. Clear contents of existing data range
wsDest.Range("E2:E" & lDestLastRow).ClearContents
'4. Copy & Paste Data
wsCopy.Range("B2:B" & lCopyLastRow).Copy
wsDest.Range("E2").PasteSpecial (xlPasteValues)
'Close a Workbook
Workbooks("TEC-037 Shop Work Request Log WCD.xlsx").Close savechanges:=False
Any help would be greatly appreciated!
Last edited by a moderator: