Excel import sheets with dialog box

KlausW

Active Member
Joined
Sep 9, 2020
Messages
386
Office Version
  1. 2016
Platform
  1. Windows
I use this code to import sheets with, it runs optimally, but I wish I could open a dialog box and select the folder where I want to import the sheets from.

Any help will be much appreciated.
Many Thanks

Klaus W
VBA Code:
Sub Rektangelafrundedehjørner1_Klik() (It is a button)
Dim ws As Worksheet
   Dim Wbk As Workbook
   Dim Pth As String, Fname As String
      Pth = Range("o1").Value
   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
 

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.
You could add this function:

VBA Code:
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
End Function

then use:

Code:
Pth = GetFolder()
 
Upvote 0
Sorry for the late reply, was just hit by work. I do not quite understand, I put the code in, it went fine but "Pth = GetFolder ()" I can not figure out where to insert.
KW
 
Upvote 0
Instead of this line:

Code:
Pth = Range("o1").Value
 
Upvote 0
Hello again, the dialog box appears and I can easily select folders, but I do not include any files above?
KW
 
Upvote 0
You asked to be able to choose the folder. The code already finds any xlsm files in the folder.
 
Upvote 0
Solution
When you open the same question on other Sites, remember to make a link to this, according to Forum rules here.
 
Upvote 0

Forum statistics

Threads
1,215,659
Messages
6,126,068
Members
449,286
Latest member
Lantern

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