Question

I've been teaching myself VBA for the last 2 months and I finally found something that I can't find an answer for. Kudos to stack overflow for bringing me through thus far! :)

I'm trying to make a sub that takes the last folder out of the current document's file path if that folder is named "toolbox".

Sub mOpenFile()

    aVar() As String


    swbDir = ThisWorkbook.Path
    aVar = Split(swbDir, "\")

    'test and change sWbDir
    If aVar(UBound(aVar)) = "Toolbox" Then
    '--------------------------------------------------
    N = 1
    swbDir = aVar(0)
    Do Until aVar(N) = "Toolbox"
    swbDir = swbDir & "\" & aVar(N)
    '--------------------------------------------------
    Loop
    MsgBox (swbDir)
    End If

    'open file in the folder below "toolbox"
    ChDir swbDir
    Workbooks.Open Filename:=swbDir & "\" & sWbRead
    ActiveCell.Offset(2, 1).Range("A1").Select
End Sub

I think my code itself is right, but it keeps popping up with a "statement invalid outside type block" error. I've read up on type blocks but it's all Greek to me. How do I dimension an array without knowing how many elements there are. Can I do it without a type... end type? I've been able to slide by without using any arrays thus far but It's something I'd like to learn anyways, dimensioning my arrays. :/

Was it helpful?

Solution

You may find it easier to use Join to build your string from the array rather than looping.

Sub mOpenFile()

    Dim aVar() As String

    'split the path into an array
    aVar = Split(ThisWorkbook.Path, Application.PathSeparator)

    'if the last element of the array is toolbox
    If aVar(UBound(aVar)) = "Toolbox" Then
        'redim the array to get rid of the last element
        'Preserve is used to keep all the rest of the data intact
        ReDim Preserve aVar(LBound(aVar) To UBound(aVar) - 1)
    End If

    'join the elements of the array into a string
    Debug.Print Join(aVar, Application.PathSeparator)

End Sub

Update

I turned it into a function to test.

Function mOpenFile(ByVal sPath As String) As String

    Dim aVar() As String

    'split the path into an array
    aVar = Split(sPath, Application.PathSeparator)

    'if the last element of the array is toolbox
    If aVar(UBound(aVar)) = "Toolbox" Then
        'redim the array to get rid of the last element
        'Preserve is used to keep all the rest of the data intact
        ReDim Preserve aVar(LBound(aVar) To UBound(aVar) - 1)
    End If

    'join the elements of the array into a string
    mOpenFile = Join(aVar, Application.PathSeparator)

End Function

results of test in immediate window

OTHER TIPS

Just to let y'all know, after looking some more on the subject. I've come up with this:

Dim aVar() As string

'Dimensioning
Public sWbSelf, sWbRead, sWbDir  As String

Type PathArray
aVar() As string
End Type


Sub mOpenFile()



'listing variables
    sWbDir = ThisWorkbook.Path
    MsgBox (TypeName(aVar))
    ReDim aVar(UBound(Split(sWbDir, "\")))
    aVar = Split(sWbDir, "\")

'test and change sWbDir
    If aVar(UBound(aVar)) = "Toolbox" Then
    '--------------------------------------------------
    N = 1
    sWbDir = aVar(0)
    Do Until aVar(N) = "Toolbox"
    sWbDir = sWbDir & "\" & aVar(N)
    Loop
    MsgBox (sWbDir)
    End If

 'open file in the folder below "toolbox"
    ChDir sWbDir
    Workbooks.Open Filename:=sWbDir & "\" & sWbRead
    ActiveCell.Offset(2, 1).Range("A1").Select
End Sub

This way worked as well, but Dick's code took just a little less time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top