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

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,265
Office Version
  1. 365
Platform
  1. Windows
Will the code always be in the workbook that you are copying from?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,265
Office Version
  1. 365
Platform
  1. Windows
In that case replace
VBA Code:
Workbooks("Disposal Form - With Macros.xlsm").Worksheets("Data")
with
VBA Code:
ThisWorkbook.Worksheets("Data")
 
Solution

delpollard1982

New Member
Joined
Apr 27, 2021
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,265
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Forum statistics

Threads
1,141,129
Messages
5,704,448
Members
421,350
Latest member
jake9951

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
Top