import a sheet from a closed workbook from location in cell

KlausW

Active Member
Joined
Sep 9, 2020
Messages
449
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

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
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,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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