My Dir is not working

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
431
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
When i change the sheet i need to go to a different path. But the Dir does not work??
This line shows ""
VBA Code:
strFolder = Dir(strPath, vbDirectory)


VBA Code:
Private Sub OpenFolder_Click()

    Dim SourcePath As String
    Dim SubPath      As String
    Dim strFolder     As String
    Dim strPath        As String
    Dim PDFFName As String
    Dim Answer        As VbMsgBoxResult
    Dim cmbdata

        cmbdata = Split(Me.OpenDrawing.Value, "-")
        cmbdata(0) = Replace(cmbdata(0), "-", "")
        
        If ActiveSheet.Name = "Frost Drains" Then
        SourcePath = "S:\R&D\Drawing Nos\Frost Grates"
        GoTo Path
        
        ElseIf ActiveSheet.Name = "DrNo Dic" Then
        SourcePath = "S:\R&D\Drawing Nos"
        GoTo Path
        
Path:
    If Val(cmbdata(0)) >= 10001 And Val(cmbdata(0)) <= 10050 Then
        SubPath = "10001-10050"
    ElseIf Val(cmbdata(0)) >= 10051 And Val(cmbdata(0)) <= 10100 Then
        SubPath = "10051-10100"
    ElseIf Val(cmbdata(0)) >= 10101 And Val(cmbdata(0)) <= 10150 Then
        SubPath = "10101-10150"
    ElseIf Val(cmbdata(0)) >= 10151 And Val(cmbdata(0)) <= 10200 Then
        SubPath = "10151-10200"
    End If
        
    strPath = SourcePath & "\" & SubPath & "\" & Int(cmbdata(0))
    strFolder = Dir(strPath, vbDirectory)
    
            On Error Resume Next
             If strFolder = strFolder Then
             ActiveWorkbook.FollowHyperlink Address:=strPath, NewWindow:=True
             End If
             
              If strFolder = vbNullString Then
                     Answer = MsgBox("Folder - Path does not exist. Would you like to create it?", vbYesNo, "Create Folder - Path")
                If Answer = vbNo Then
                    Exit Sub
                        ElseIf Answer = vbYes Then
                            VBA.FileSystem.MkDir (strPath)
                      End If
                End If
        End If
      
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
That would suggest the folder doesn't exist.

By the way, this seems pointless:

Code:
 If strFolder = strFolder Then
 
Upvote 0
It does exist i have taken the path of vba code and it does match the folder?

I`ve taken the
VBA Code:
 If strFolder = strFolder Then
away
 
Upvote 0
What happens if you allow the code to try and create the folder?

Also, there is no need for the Goto lines in your code. You should just move the last End If up to before where the Path: label is currently.
 
Upvote 0
Again, what does that actually mean? Either you get an error or the code completes.

You haven't addressed either of my other points?
 
Upvote 0
There is no error massage it stops running until i reset it.
Other points are addressed see below

VBA Code:
Private Sub OpenFolder_Click()

    Dim SourcePath As String
    Dim SubPath      As String
    Dim strFolder     As String
    Dim strPath        As String
    Dim PDFFName As String
    Dim Answer        As VbMsgBoxResult
    Dim cmbdata

        cmbdata = Split(Me.OpenDrawing.Value, "-")
        cmbdata(0) = Replace(cmbdata(0), "-", "")
        
        If ActiveSheet.Name = "Frost Drains" Then
          SourcePath = "\\DF-AZ-FILE01\Company\R&D\Drawing Nos\Frost Grates"
            ElseIf ActiveSheet.Name = "DrNo Dic" Then
                  SourcePath = "S:\R&D\Drawing Nos"
            End If
            
    If Val(cmbdata(0)) >= 10001 And Val(cmbdata(0)) <= 10050 Then
        SubPath = "10001-10050"
    ElseIf Val(cmbdata(0)) >= 10051 And Val(cmbdata(0)) <= 10100 Then
        SubPath = "10051-10100"
    ElseIf Val(cmbdata(0)) >= 10101 And Val(cmbdata(0)) <= 10150 Then
        SubPath = "10101-10150"
    ElseIf Val(cmbdata(0)) >= 10151 And Val(cmbdata(0)) <= 10200 Then
        SubPath = "10151-10200"
    End If
        
    strPath = SourcePath & "\" & SubPath & "\" & Int(cmbdata(0))
    strFolder = Dir(strPath, vbDirectory)
    
            On Error Resume Next

             ActiveWorkbook.FollowHyperlink Address:=strPath, NewWindow:=True

             
              If strFolder = vbNullString Then
                     Answer = MsgBox("Folder - Path does not exist. Would you like to create it?", vbYesNo, "Create Folder - Path")
                If Answer = vbNo Then
                    Exit Sub
                        ElseIf Answer = vbYes Then
                            VBA.FileSystem.MkDir (strPath)
                      End If
                End If
        
      
End Sub
 
Upvote 0
If the code stops then it has completed, so I don't know what you mean about resetting it?

Your code also tries to follow a link to the folder before checking if it exists, which seems a little odd to me.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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