• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

how can i record the file path of a directory i just created as a variable?

jbaich

Member
Hi everybody, I am using MkDir to create folders and I would like to be able to assign the file path of the folder that was just created to a variable...

so for example, in the code below I'm getting the user to choose the FldrRoot location and then creating several new folders here.

I've just commented out some of the stuff that doesn't pertain to this specific question :)

Code:
Sub MergeWbks()

'Dim fsoMain As New FileSystemObject
Dim FldrRoot As String
'Dim myFldr As Folder
'Dim myWB As Workbook, tempWB As Workbook
'Dim fl As File
Dim FldrLvl1 As String
'Dim FldrLvl2 As String
'Dim FldrLvl3 As String
Dim xDirectRoot$, InitialFoldr$
Dim AMDExt As String
Dim PAABMgmt As String
Dim Jones As String
Dim FComp As String
'
InitialFoldr$ = "H:\"
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select the folder containing the PVS's for the Roll Year under Appeal"
.InitialFileName = InitialFoldr$
.Show
If .SelectedItems.Count <> 0 Then
xDirectRoot$ = .SelectedItems(1) & "\"
Else

End If
End With

    Application.ScreenUpdating = False
 
    FldrRoot = xDirectRoot$

    MkDir (FldrRoot & "\" & "Amendments Extract") '//\\
'//How do I assign the file path created above to the variable AMDExt? \\

    MkDir (FldrRoot & "\" & "PAAB Mgmt Reports") '//\\
'//How do I assign the file path created above to the variable PAABMgmt? \\

    MkDir (FldrRoot & "\" & "Jones Reports") '//same as above\\
    MkDir (FldrRoot & "\" & "Formatted Reports") '//same as above\\
    On Error GoTo 0 '//resume errors\\

'I will then save some files to these folders...

'Next I will attempt to loop through each folder and merge these files... it seems so simple, but I just can't figure it out

  Set myFldr = fsoMain.GetFolder("I want to use each of the MkDir file path variables here so that I can merge all the files I will have saved in them")
  Set myWB = ActiveWorkbook
  For Each fl In myFldr.Files

'There will be more code below...

I would also like to use these file path variables in other subs later on so do I need to make this Sub public or just move those variables above the Sub() line?

Thanks all!

Cheers,
Joe
__________________________________________________________________
Mod edit : post moved to appropriate forum …
 
Last edited:
I have only used the first two variables to demonstrate the solution

Code:
Public AMDExt As String
Public PAABMgmt As String

Sub MergeWbks()

'Dim fsoMain As New FileSystemObjectDim FldrRoot As String
'Dim myFldr As Folder'Dim myWB As Workbook, tempWB As Workbook'Dim fl As FileDim FldrLvl1 As String
'Dim FldrLvl2 As String'Dim FldrLvl3 As StringDim xDirectRoot$, InitialFoldr$
Dim Jones As String
Dim FComp As String
'InitialFoldr$ = "H:\"
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select the folder containing the PVS's for the Roll Year under Appeal"
.InitialFileName = InitialFoldr$
.Show
If .SelectedItems.Count <> 0 Then
xDirectRoot$ = .SelectedItems(1) & "\"
Else

End If
End With

    Application.ScreenUpdating = False

    FldrRoot = xDirectRoot$

AMDExt = FldrRoot & "\" & "Amendments Extract"

    MkDir (AMDExt ) '//\\'//How do I assign the file path created above to the variable AMDExt? \\

PAABMgmt= FldrRoot & "\" & "PAAB Mgmt Reports"
    MkDir (PAABMgmt) '//\\'//How do I assign the file path created above to the variable PAABMgmt? \\


    MkDir (FldrRoot & "\" & "Jones Reports") '//same as above\\    MkDir (FldrRoot & "\" & "Formatted Reports") '//same as above\\    On Error GoTo 0 '//resume errors\\
'I will then save some files to these folders...
'Next I will attempt to loop through each folder and merge these files... it seems so simple, but I just can't figure it out
  Set myFldr = fsoMain.GetFolder("I want to use each of the MkDir file path variables here so that I can merge all the files I will have saved in them")
  Set myWB = ActiveWorkbook
  For Each fl In myFldr.Files

'There will be more code below...

Note the placement of the two Dim statements infront of the sub
This means all subroutines can access them
 
Back
Top