Copy data from another workbook using cell value as parameter for sheet?

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
270
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have the following
VBA Code:
Dim wsCopy As Worksheet
Dim wsDest As Worksheet


Set wsCopy = Workbooks("New Data.xlsx").Worksheets("Export 2")
Set wsDest = Workbooks("Reports.xlsm").Worksheets("All Data")

In Reports.xlsm (which is where this code lives) I'd like to have a cell (A1 on a sheet named "Control") where the user types the sheet name we want to copy from in New Data - so say they type July then instead of looking in Sheet "Export 2", my macro would look in July.

Many thanks.
 

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.
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your "Control" sheet and click 'View Code'. Paste the macro into the empty code window that opens up. You would then have to place the rest of your code where indicated in red. Close the code window to return to your sheet. Enter a value in A1 in the "Control" sheet and press the RETURN key. The macro will run automatically.
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Address(0, 0) <> "A1" Then Exit Sub
    Application.ScreenUpdating = False
    Dim wsCopy As Worksheet, wsDest As Worksheet
    Set wsCopy = Workbooks("New Data.xlsx").Sheets(Target.Value)
    Set wsDest = ThisWorkbook.Sheets("All Data")
    'Your code here
    Application.ScreenUpdating = True
End Sub
Another alternative would be this macro in a regular module which you would have to run manually:
Rich (BB code):
Sub CopyData()
    Application.ScreenUpdating = False
    Dim wsCopy As Worksheet, wsDest As Worksheet
    Set wsDest = Workbooks("Reports.xlsm").Worksheets("All Data")
    Set wsCopy = Workbooks("New Data.xlsx").Sheets(wsDest.Sheets("Control").Range("A1").Value)
    'Your code here
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you for that but it's erroring at
VBA Code:
Set wsCopy = Workbooks("New Data.xlsx").Sheets(wsDest.Sheets("Control").Range("A1").Value)

"Method or data member not found"
 
Upvote 0
Oops. Try:
VBA Code:
Set wsCopy = Workbooks("New Data.xlsx").Sheets(Workbooks("Reports.xlsm").Sheets("Control").Range("A1").Value)
 
Upvote 0
Solution

Forum statistics

Threads
1,214,875
Messages
6,122,047
Members
449,064
Latest member
scottdog129

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