VBA to copy data from "Active workbook" to another sheet

delpollard1982

New Member
Joined
Apr 27, 2021
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I'm struggling with getting the VBA code correct for the following.

The requirement is to copy data from an Active workbook via a command button trigger to a data register which you can see in the code below is named "Disposal Register".

The Disposal Register name will stay constant.
The Active workbook, where the data is exported from will be variable and will have no constants, however in the code below it is named "Disposal Form - With Macros".

Thankyou in advance.
Del

VBA Code:
Private Sub CommandButton8_Click()

Workbooks.Open "X:\Departments\ALL DEPARTMENTS\New Disposal Process\Disposal Register.xlsx"

Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long

 
  Set wsCopy = Workbooks("Disposal Form - With Macros.xlsm").Worksheets("Data")
  Set wsDest = Workbooks("Disposal Register.xlsx").Worksheets("Data")
   

  lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
   

  lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row

Workbooks("Disposal Form - With Macros.xlsm").Worksheets("Data").Range("A1:X1" & lCopyLastRow).Copy _

Workbooks("Disposal Register.xlsx").Worksheets("Data").Range("A" & lDestLastRow).PasteSpecial Paste:=xlPasteValues

Workbooks("Disposal Register.xlsx").Close SaveChanges:=True

End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
In that case replace
VBA Code:
Workbooks("Disposal Form - With Macros.xlsm").Worksheets("Data")
with
VBA Code:
ThisWorkbook.Worksheets("Data")
 
Upvote 0
Solution
In that case replace
VBA Code:
Workbooks("Disposal Form - With Macros.xlsm").Worksheets("Data")
with
VBA Code:
ThisWorkbook.Worksheets("Data")

Brilliant - This has worked. Thanks for your speedy help.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,213,556
Messages
6,114,284
Members
448,562
Latest member
Flashbond

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