Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically determine current setting for Option Base in VBA

Tags:

excel

vba

How can I programmatically determine the current setting for Option Base in VBA? The Option Base can be set to 0 or 1, and this determines whether array indices start at 0 or 1 (see MSDN).

However, I can't see any easy way to find out what the current setting is. I was hoping there might be an Option() function that I could pass a parameter to, something like:

Debug.Print Option("Base")

and it would tell me, but it doesn't seem to be there.

like image 473
Caltor Avatar asked Apr 01 '15 12:04

Caltor


People also ask

How do you use an option base in VBA?

Option Base is used to declare the default lower bound of array elements. It is declared at module level and is valid only for the current module. By default (and thus if no Option Base is specified), the Base is 0. Which means that the first element of any array declared in the module has an index of 0.

What is option base used for in vb6?

Used at the beginning of a module to specify the default lower bound for arrays dimensioned within the module.

How do you call a VBA module in Excel?

Open your workbook in Excel. Press Alt + F11 to open Visual Basic Editor (VBE). Right-click on your workbook name in the "Project-VBAProject" pane (at the top left corner of the editor window) and select Insert -> Module from the context menu.


2 Answers

Function GetBaseOption() As Long

    Dim arr

    arr = Array("a")
    GetBaseOption = LBound(arr)

End Sub
like image 106
Tim Williams Avatar answered Sep 20 '22 05:09

Tim Williams


While I agree with @jonsharpe that if you're using Lbound(arr) and Ubound(arr), you don't need to know this at runtime, but I think it's an interesting question.

First, an example of properly looping through an array.

For i = LBound(arr) To Ubound(arr)
    ' do something
Next

Now, in order to do exactly what you asked, you'll need to access use the VBIDE library. Otherwise known as the "Microsoft Visual Basic for Applications Extensibility" library. It provides access to the IDE and the code with-in it. You can use it to discover if Option Base 1 has been declared. I don't recommend trying to do it at runtime though. This would be more useful as a kind of static code analysis during development.

First, you'll need to add a reference to the library and grant the code access to itself. Once you've done that, the following code should do what you would like.

Public Sub FindOptionBase1Declarations()
    
    Dim startLine As Long
    startLine = 1
    
    Dim startCol As Long
    startCol = 1
    
    Dim endLine As Long
    endLine = 1 ' 1 represents the last line of the module
    
    Dim endCol As Long
    endCol = 1 ' like endLine, 1 designates the last Col
    
    Dim module As CodeModule
    Dim component As VBComponent
    For Each component In Application.VBE.ActiveVBProject.VBComponents ' substitute with any VBProject you like
        Set module = component.CodeModule
        
        If module.Find("Option Base 1", startLine, startCol, endLine, endCol, WholeWord:=True, MatchCase:=True, PatternSearch:=False) Then
            
            Debug.Print "Option Base 1 turned on in module " & component.Name
            
            ' variables are passed by ref, so they tell us the exact location of the statement
            Debug.Print "StartLine: " & startLine
            Debug.Print "EndLine: " & endLine
            Debug.Print "StartCol: " & startCol
            Debug.Print "EndCol: " & endCol
            
            ' this also means we have to reset them before we look in the next module
            startLine = 1
            startCol = 1
            endLine = 1
            endCol = 1
        End If
        
    Next 
    
End Sub

See the CodeModule.Find documentation for more information.


If using an add-in is an option, @Mat'sMug and I's open source project Rubberduck has a Code Inspection that will show you all instances of this throughout the active project.

Option Base 1 Code Inspection

See this for more information on that particular inspection.

like image 20
RubberDuck Avatar answered Sep 22 '22 05:09

RubberDuck