# My Dir is not working



## Eric Penfold (Jan 5, 2023)

When i change the sheet i need to go to a different path. But the Dir does not work??
This line shows ""

```
strFolder = Dir(strPath, vbDirectory)
```



```
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
```


----------



## RoryA (Jan 5, 2023)

That would suggest the folder doesn't exist.

By the way, this seems pointless:


```
If strFolder = strFolder Then
```


----------



## Eric Penfold (Jan 5, 2023)

It does exist i have taken the path of vba code and it does match the folder?

I`ve taken the

```
If strFolder = strFolder Then
```
 away


----------



## RoryA (Jan 5, 2023)

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.


----------



## Eric Penfold (Jan 5, 2023)

The code seems to stop working i have to reset it.


----------



## RoryA (Jan 5, 2023)

Eric Penfold said:


> The code seems to stop working


What *exactly* does that mean?


----------



## Eric Penfold (Jan 5, 2023)

RoryA said:


> What *exactly* does that mean?


The code does not go any further so i have to use the reset button.


----------



## RoryA (Jan 5, 2023)

Again, what does that actually mean? Either you get an error or the code completes.

You haven't addressed either of my other points?


----------



## Eric Penfold (Jan 5, 2023)

There is no error massage it stops running until i reset it.
Other points are addressed see below


```
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
```


----------



## RoryA (Jan 5, 2023)

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.


----------



## Eric Penfold (Jan 5, 2023)

When i change the sheet i need to go to a different path. But the Dir does not work??
This line shows ""

```
strFolder = Dir(strPath, vbDirectory)
```



```
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
```


----------



## Eric Penfold (Jan 5, 2023)

RoryA said:


> If the code stops then it has completed, so I don't know what you mean about resetting it? Sorry i was getting confused
> 
> Your code also tries to follow a link to the folder _before_ checking if it exists, which seems a little odd to me. The checking folder exist is now in the correct order


It`s still the

```
strFolder = Dir(strPath, vbDirectory)
```
 which shows ""


----------



## RoryA (Jan 5, 2023)

I can't think of any other reason than that the folder path doesn't exist.


----------



## Eric Penfold (Jan 5, 2023)

But i copied the path from the code and it found it?


----------



## RoryA (Jan 5, 2023)

Just to check: how did you do that? The full path is not included your code, it's calculated at runtime.


----------



## Eric Penfold (Jan 5, 2023)

Sorry me being an an idiot the sub path was not right all working fine now.


----------

