import a sheet from a closed workbook from location in cell

KlausW

Active Member
Joined
Sep 9, 2020
Messages
453
Office Version
  1. 2016
Platform
  1. Windows
Hi I am using this VBA code to import a sheet from a closed workbook. It works. But I would like that the location and file name came from cell D1.
I've tried redoing this but it doesn't work.

Set sourceWorkbook = Workbooks.Open("D:\DNBR 2024\Delinger 2024\Delingsliste MASTER.xlsm") to Set sourceWorkbook = Workbooks.Open Range ("D1").Value.

Some who can help, any help would be appreciated.

Best regarts Klaus W

VBA Code:
Sub Rektangelafrundedehjørner5_Klik()

 ' Declare variables
    Dim sourceWorkbook As Workbook
    Dim targetWorkbook As Workbook
    Dim ws As Worksheet
    
    ' Set the destination workbook to the active workbook
    Set targetWorkbook = ThisWorkbook
    
    ' Disbales screen updating
    Application.ScreenUpdating = False
    
    ' Open the source workbook
    Set sourceWorkbook = Workbooks.Open("D:\DNBR 2024\Delinger 2024\Delingsliste MASTER.xlsm")
    
    ' Set the worksheet that you want to copy
    Set ws = sourceWorkbook.Sheets("SkibsNr")
    
    ' Copy the worksheet to the destination workbook
    ws.Copy After:=targetWorkbook.Sheets(targetWorkbook.Sheets.Count)
    
    ' Close the source workbook without saving changes
    sourceWorkbook.Close SaveChanges:=False
    
    ' Enables screen updating
    Application.ScreenUpdating = True
    
    ' Release the object variables to free up memory
    Set ws = Nothing
    Set sourceWorkbook = Nothing
    Set targetWorkbook = Nothing

   
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Change sheet_name_here to the actual sheet name and try the line below:
VBA Code:
Set sourceWorkbook = Workbooks.Open (ThisWorkbook.Worksheets("sheet_name_here").Range ("D1").Value)
Depending on how you start the procedure, this may also work:
VBA Code:
Set sourceWorkbook = Workbooks.Open (ActiveSheet.Range ("D1").Value)
 
Upvote 0
Solution
Change sheet_name_here to the actual sheet name and try the line below:
VBA Code:
Set sourceWorkbook = Workbooks.Open (ThisWorkbook.Worksheets("sheet_name_here").Range ("D1").Value)
Depending on how you start the procedure, this may also work:
VBA Code:
Set sourceWorkbook = Workbooks.Open (ActiveSheet.Range ("D1").Value)
Hi bobsan42
Just as it should be.
Many thanks.
Best regards Klaus W
 
Upvote 0

Forum statistics

Threads
1,223,944
Messages
6,175,554
Members
452,652
Latest member
eduedu

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