Only to import the sheet with the name in Range Q1

KlausW

Active Member
Joined
Sep 9, 2020
Messages
378
Office Version
  1. 2016
Platform
  1. Windows
I use this code to import sheets with dialog box, it runs optimally, but I wish Excel only to import the sheets whit the name in Range Q1.

Any help will be much appreciated.
Many Thanks

Klaus W

VBA Code:
Sub Rektangelafrundedehjørner1_Klik()

 Dim ws As Worksheet
   Dim Wbk As Workbook
   Dim Pth As String, Fname As String
      Pth = GetFolder()
      Fname = Dir(Pth & "\*.xlsm")
   
   Do While Fname <> ""
     Set Wbk = Workbooks.Open(Pth & "\" & Fname)
      For Each ws In Wbk.Worksheets
         If Not ShtExists(ws.Name, ThisWorkbook) Then
            ws.Copy , ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
         End If
      Next ws
      Wbk.Close False
      Fname = Dir
   Loop

End Sub

Function GetFolder() As String
Dim dlg As FileDialog
Set dlg = Application.FileDialog(msoFileDialogFolderPicker)
'dlg.InitialFileName = "c:\"
'If dlg.Show = -1 Then
'GetFolder = dlg.SelectedItems(1)
'End If
With dlg

    .InitialFileName = "c:\"
    .Show
    GetFolder = .SelectedItems(1)
   
End With

End Function
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Do you mean to check each sheet of each workbook & then import that sheet if Q1= a certain value?
Or do you mean that you have a sheet somewhere where Q1 has the name of the sheet to be imported?
 
Upvote 0
I mean that I have a sheet somewhere where Q1 has the name of the sheet to be imported?
 
Upvote 0
In that case what is the name of the sheet & is it in the same workbook as the code?
 
Upvote 0
The Sheet I want to import is called "Address List Fast and VPL",
I will insert the name in cell Q1.
No it's not in the same workbook, that's why I use a dialog box to find the file. That does not need to be changed.
KW
 
Upvote 0
Is it the name of the Sheet where the code shut be(Name: Delingsliste 2020 med billeder 10-11-2020) , ore the name of the workbook where the sheet shout be importet from(Name: Adresse fastbesætning og VPL 2021)?
 
Upvote 0
If you are going to put the name of the sheet to be imported into Q1, I need to know which sheet that cell is located in.
 
Upvote 0
OK now I see, the Sheet the Q1 is located in is Named: Stamdata
 
Upvote 0
Ok, how about
VBA Code:
Sub Rektangelafrundedehjørner1_Klik()

 Dim ws As Worksheet
   Dim Wbk As Workbook
   Dim Pth As String, Fname As String, ShtName As String
      Pth = GetFolder()
      Fname = Dir(Pth & "\*.xlsm")
      ShtName = LCase(Sheets("Stamdata").Range("Q1").Text)
   
   Do While Fname <> ""
     Set Wbk = Workbooks.Open(Pth & "\" & Fname)
      For Each ws In Wbk.Worksheets
         If LCase(ws.Name) = ShtName Then
            ws.Copy , ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
         End If
      Next ws
      Wbk.Close False
      Fname = Dir
   Loop

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,653
Messages
6,120,752
Members
448,989
Latest member
mariah3

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