import a sheet with name standing in a cell from another Workbook

KlausW

Active Member
Joined
Sep 9, 2020
Messages
380
Office Version
  1. 2016
Platform
  1. Windows
Hi

I use this VBA code to import sheets from another Workbook. There is only one sheet so it is easy. Now I would like to import a sheet with name standing in a sheet “Stamdata” cell A1 from another Workbook that contains many sheets with different names. Some who can help.

Any help will be appreciated

Best regards Klaus W

VBA Code:
Sub Rektangelafrundedehjørner1_Klik()

Dim Path As String
Dim Filename As String
Dim Sheet As Worksheet

Path = Range("a1").Value
Filename = Dir(Path & "*.xlsm")
Do While Filename <> ""

Workbooks.Open Filename:=Path & Filename, ReadOnly:=True

    For Each Sheet In ActiveWorkbook.Sheets
        Sheet.Copy After:=ThisWorkbook.Sheets(1)
    Next Sheet

Workbooks(Filename).Close

Filename = Dir()
Loop

End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi,
I'm not sure if I can help you .but you explanation is not clear for me :confused:
I suppose the path in A1 and you tell us the sheet name is in A1, how is it?!
I think you should say copy specific sheet from file to another based on path into cell .
this is my try
VBA Code:
Sub Rektangelafrundedehjørner1_Klik()

Dim Path As String
Dim Filename As String


Path = Range("a1").Value
Filename = Dir(Path & "*.xlsm")
Do While Filename <> ""

Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
'On Error Resume Next
    With ActiveWorkbook.Sheets("stock")
        .Copy After:=ThisWorkbook.Sheets(1)
     End With

Workbooks(Filename).Close

Filename = Dir()
Loop

End Sub
if there are multiple files , then will show error . in this case you should enable the disabled line to skip error.
I believe there is better way to avoid error , but I have no idea how do that . maybe experts guide us .
anyway you said one file so the error doesn't show .;)
good luck
 
Upvote 0
Solution
Hi,
I'm not sure if I can help you .but you explanation is not clear for me :confused:
I suppose the path in A1 and you tell us the sheet name is in A1, how is it?!
I think you should say copy specific sheet from file to another based on path into cell .
this is my try
VBA Code:
Sub Rektangelafrundedehjørner1_Klik()

Dim Path As String
Dim Filename As String


Path = Range("a1").Value
Filename = Dir(Path & "*.xlsm")
Do While Filename <> ""

Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
'On Error Resume Next
    With ActiveWorkbook.Sheets("stock")
        .Copy After:=ThisWorkbook.Sheets(1)
     End With

Workbooks(Filename).Close

Filename = Dir()
Loop

End Sub
if there are multiple files , then will show error . in this case you should enable the disabled line to skip error.
I believe there is better way to avoid error , but I have no idea how do that . maybe experts guide us .
anyway you said one file so the error doesn't show .;)
good luck
Thank allot
 
Upvote 0
sorry it's a mistake the Path is in cell A1 an sheets name in C1.
Is it possible to insert a Range cell C1 in this line.

VBA Code:
With ActiveWorkbook.Sheets("stock")
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,215,093
Messages
6,123,067
Members
449,090
Latest member
fragment

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