Get Folder Path as String - excel

I'm relatively new with VBA and currently working on a macro that will change from PC to PC, for test purposes I'm using the direct path:
Sub VIP()
'Define Folder Paths & Workbooks
Workbooks.Open ("C:\Users\j.lopez\Documents\AdHoc Reports\Serrano\Daily VIP Report Master.xlsx")
to open the workbooks, but eventually that path will change, so i was thinking to make the user select the folder path with:
Application.FileDialog(msoFileDialogFolderPicker)
But im lost, how can i properly:
1.- Ask for a user to select the folder containing the necessary files for the macro to work with
2.- Trap that path
3.- Replace it in the WorkBooks.Open

1. Ask for a user to select the folder containing the necessary files for the macro to work with:
I would recommend you to use FolderDialog and Show the Dialog to the user, and let him/her choose the folder. Next, check whether the required files exist in the selected directory. Use System.IO.File.Exists(<path>)=<boolean> .
2. Trap that path:
Just after validating the folder path, you can save the folder path in a variable.
Then do an assignment statement,
Let's say you created a variable 'path', so, path = path & "\" & <file_name> .
And there you have it, stored in 'path'.
3. Replace it in the WorkBooks.Open:
Then use the following code:
Workbooks.Open ("C:\Users\j.lopez\Documents\AdHoc Reports\Serrano\" & path)

Selecting file or folder with Browse File Option with VBA
' To Select File
sub select_file()
selected_file = Application.GetOpenFilename(, , "Select File", , False)
End sub
' To Select Folder
Sub selectfolder()
zhr_folder = GetFolder()
End sub
Function GetFolder() As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = ""
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function
Post that you can combine both file and folder names to adapt your needs
Source: https://play.google.com/store/apps/details?id=com.vbausefulcodes.dp

EDIT-
This code was adapted to my needs, and i found it on this YouTube Video
Dim diaFolder As FileDialog
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
diaFolder.AllowMultiSelect = False
diaFolder.Show
fle = diaFolder.SelectedItems(1)
Range("A15") = fle
Set diaFolder = Nothing
'Opening WorkBooks
Workbooks.Open (fle & "\Daily Sports VIP Report.xlsx")
video for Reference
- https://www.youtube.com/watch?v=Y4PG2qr9tRM

Related

Do something while in VBA

I have a lot of files in one folder. What I want to do is add a column with the name of the file. I found a solution to this, but I want to optimize. Currently I am just adding the values from J2 to J1000 to make sure I cover all rows. This is not ideal as the amount of rows in each file differ. What I want to do is find a way to add the value matching the amount of rows that exists in the sheet.
I want to find a way to check if there is data in column A for each row and then add the value as long as there is some data in column A for each row.
My thoughts would be to do a while statement to check if each row in column A is different from an empty string and add the value as long as it is different from an empty string. However I am not sure how to implement this.
Here is my code:
Sub AllWorkbooks()
Dim MyFolder As String 'Path collected from the folder picker dialog
Dim MyFile As String 'Filename obtained by DIR function
Dim wbk As Workbook 'Used to loop through each workbook
On Error Resume Next
Application.ScreenUpdating = False
'Opens the folder picker dialog to allow user selection
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please select a folder"
.Show
.AllowMultiSelect = False
If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
MsgBox "You did not select a folder"
Exit Sub
End If
MyFolder = .SelectedItems(1) & "\" 'Assign selected folder to MyFolder
End With
MyFile = Dir(MyFolder) 'DIR gets the first file of the folder
'Loop through all files in a folder until DIR cannot find anymore
Do While MyFile <> ""
'Opens the file and assigns to the wbk variable for future use
Set wbk = Workbooks.Open(FileName:=MyFolder & MyFile)
'Replace the line below with the statements you would want your macro to perform
Sheets(1).Range("j1").Value = "Date"
Sheets(1).Range("j2:j1000").Value = Mid(ActiveWorkbook.Name, 10, 10)
wbk.Close savechanges:=True
MyFile = Dir 'DIR gets the next file in the folder
Loop
Application.ScreenUpdating = True
End Sub

create multiple sheets as pdf

I have this section of code that asks for a folder to save PDFs to. If you cancel or close the application, the path of the file is still selected. I want to select the file from the path only if you don't select a folder, and I don't want any action to be taken if you press cancel or close the application.
Dim folder_path as string
Dim sh as worksheet
Dim filename as string
with application.filedialog(msofiledialogfolderpicker)
.title = "Select the folder path"
if .show = -1 then
Folder_path = .selectedItems(1)
else
Folder_path = Thisworkbook.path
end if
end with```

Viewing the internal files through folder picker in VBA

I have a macro through which I can select a folder by double clicking. The path of this folder is later used to open some files (in the folder).
The code for the macro to select the folder is:
Sub GetFolder()
Dim folderselected As String
Set myFolder = Application.FileDialog(msoFileDialogFolderPicker)
With myFolder
.Title = "Choose Folder"
.AllowMultiSelect = False
If .Show <> -1 Then
Exit Sub
End If
folderselected = .SelectedItems(1)
End With
DataPath = folderselected & "\"
End Sub
Now when I click the folder, the explorer interface shows ."No items match your search", although there are files in the folder.
Is there a way to extend this code, where I can select the folder, retrieve the datapath and also get to view the files in the explorer display (but of course not select it) ??

browse button in input box to find file Excel2007 Vba

I need the browse button in input box to find file - VB A - EXCEL Macro][1]
need to find the folder path via browse button instead of typing in input box
is it possible?
|-------------------|
|-------------------| Browse
by clicking a cell it should ask for file browse.
should not be edited manually. i mean , i want to lock the particular cell locked. and only able to edit via macro.
Alternately:
Sub tgr()
Dim strFilePath As String
strFilePath = Application.GetOpenFilename
If strFilePath = "False" Then Exit Sub 'Pressed cancel
MsgBox strFilePath
End Sub
You can use this to find a file. Modify the filter if you need to. the variable fldr will have your data. Then you can set your textbox to that value.
Sub File_Picker()
With Application.FileDialog(msoFileDialogFilePicker)
.Filters.Clear
.Filters.Add "Text", "*.txt", 1
.InitialFileName = ActiveWorkbook.Path & "\"
.Show
If .SelectedItems.Count = 0 Then GoTo 1
fldr = .SelectedItems(1)
End With
End Sub
or:
Sub Folder_Picker()
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = ActiveWorkbook.Path & "\"
.Show
If .SelectedItems.Count = 0 Then GoTo 1
fldr = .SelectedItems(1)
End With
End Sub
I have more helpful pieces of code like this at My GitHub

Importing folder to Excel (FileDialogFolderPicker) using VBA

I'm using the next code in order to select a folder from a certain path and import all the files inside it:
Function GetFolder()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
fd.Title = "Select Excel Workbook(s) Folder"
Dim vrtSelectedItem As Variant
With fd
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
GetFolder = vrtSelectedItem
Next vrtSelectedItem
Else
End If
End With
Set fd = Nothing
End Function
When the Folder Picker window opens it start on the desktop. Is there a way to make it go to a specific path upon opening? or open where the excel file itself is located?
You would update the InitialFileName property, and you could set it to use the ActiveWorkbook.Path
You'll need to make sure that you include the ending slash, or it will only display the previous folder instead of the folder you want.
Also, there is no reason to loop through the .SelectedItems collection because the FolderPicker FileDialog doesn't support mutliple selections.
In summary, I think this is the code you're looking for:
Function GetFolder()
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = ActiveWorkbook.Path & Application.PathSeparator
.Title = "Select Excel Workbook(s) Folder"
If .Show = True Then
GetFolder = .SelectedItems(1)
Else
GetFolder = False
End If
End With
End Function
add a line like this before .Show:
fd.InitialFileName = "c:\whateverInitialDirectory"

Resources