Option Base 1 But LBound Is 0

wsnyder

Board Regular
Joined
Sep 23, 2018
Messages
223
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Using Excel 365.
I have Option Base 1 at the top of my Modules

However my code is returning Subscript Out of Range error
And when I hover over the array variable for LBound it shows as 0.

thanks,
-w

Error here:
VBA Code:
 Debug.Print vCriteria(i), vColumns(i)

Full code:

Code:
Option Explicit
Option Base 1

Sub Foo()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim rng As Range
    Dim s As String
    Dim i As Long
    Dim vCriteria As Variant
    Dim vColumns As Variant
    
    Set wb = Thisworkbook
    Set ws = wb.Worksheets("Data")
    Set rng = ws.Range("1:1")
    
    s = "cat","dog","mouse"
        
    vCriteria = GetCriteriaArray(s:=s)                              'Private Function
    vColumns = GetColumnArray(v:=vCriteria, _
                              rng:=rng)                             'Private Function
    Debug.Print "===================================="
    Debug.Print "vCriteria & vColumns"
    Debug.Print "------------------------------------"
    For i = LBound(vCriteria) To UBound(vCriteria)
        Debug.Print vCriteria(i), vColumns(i)
    Next i
    Debug.Print "===================================="
 
    'Tidy up
        Erase vColumns
        Erase vCriteria
        Set rng = Nothing
        Set ws = Nothing
        Set wb = Nothing
    
End Sub

Private Function GetCriteriaArray(s As String) As Variant

    GetCriteriaArray = Split(s, ",")

End Function


Private Function GetColumnArray(v As Variant, _
                                rng As Range) As Variant

    Dim i As Long
    Dim x As Long
    Dim crit As String
    Dim tempColumnArray() As Long
    x = 1
    
    Debug.Print "Range Address: ", rng.Address
    
    For i = LBound(v) To UBound(v)
        ReDim Preserve tempColumnArray(x)
        crit = CStr(v(i))
        Debug.Print x, crit
        tempColumnArray(x) = FindColumnHeader(rng:=rng, _
                                              SearchTerm:=crit)
        Debug.Print x, tempColumnArray(x)
        x = x + 1
    Next i
    
    GetColumnArray = CVar(tempColumnArray)

End Function
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
The Split function will always return a 0 based array regardless of your settings.
 
Upvote 0
Solution
As Fluff mentioned, the Split function always returns a zero-based array no matter what the Option Base setting is. In a similar manner, the lower bound for both dimensions of an array created by assigning a range to a Variant variable will always be one no matter what the Option Base setting is.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,937
Members
449,094
Latest member
teemeren

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