VBA to get a folder path using msofiledialogfolderpicker

Meghanakusuma

New Member
Joined
Apr 10, 2018
Messages
11
hi, please help me to find the error.
when i run this code, it says the path not found


Code:
Public Sub process()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'DECLARE AND SET VARIABLES
reset
Dim wbk As Workbook
Dim Filename As String
Dim Path As String
Dim sheet_four As Worksheet
Dim sheet_five As Worksheet
Set sheet_four = ThisWorkbook.Worksheets("Sheet4 - Part List&Rel Data")
Set sheet_five = ThisWorkbook.Worksheets("Sheet5 - FMEA")


Application.FileDialog(msoFileDialogFolderPicker).Show
Path = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1) & ""




'Path = Application.FileDialog(msoFileDialogFolderPicker)
'Path
'Path = "C:\Users\yatisha.arehally\Desktop\ALL Documents"


Filename = Dir(Path & "*.xls*")


'--------------------------------------------
'OPEN EXCEL FILES
 Do While Filename <> ""  'IF NEXT FILE EXISTS THEN
    Set wbk = Workbooks.Open(Path & Filename)
    '
    ' CODE GOES HERE
    '
     ActiveWorkbook.Sheets(7).Select
     lr4 = sheet_four.Cells(Rows.Count, 1).End(xlUp).Row
     lr5 = sheet_five.Cells(Rows.Count, 1).End(xlUp).Row
     lra = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
     ActiveSheet.Range(Cells(7, 1), Cells(lra, 11)).Copy
     sheet_four.Cells(lr4 + 1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False


     ActiveWorkbook.Sheets(8).Select
     lrb = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
     ActiveSheet.Range(Cells(8, 1), Cells(lrb, 16)).Copy
     sheet_five.Cells(lr5 + 1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
     'MsgBox Filename & " has opened"
    wbk.Close True
    Filename = Dir
Loop




Sheets(1).Select
MsgBox "Its done!"
Application.ScreenUpdating = True
Application.DisplayAlerts = False
End Sub





Sub reset()
For i = 1 To ActiveWorkbook.Sheets.Count
lri = Sheets(i).Cells(Rows.Count, 2).End(xlUp).Row
lci = Sheets(i).Cells(1, Columns.Count).End(xlToLeft).Column
    If lri >= 2 Then
    Sheets(i).Select
    Range(Cells(2, 1), Cells(lri, lci)).Clear
    End If
Next i
Sheets(1).Select
End Sub
 
Last edited by a moderator:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Welcome to the forum.

You need to add a path separator (\ on Windows) to the end of your path variable before using it with Dir and Workbooks.Open
 
Last edited:
Upvote 0
Rich (BB code):
Path = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1) & "\"
 
Upvote 0


its showing this like
 
Upvote 0
Stop the code and run it again.
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,820
Members
449,469
Latest member
Kingwi11y

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