An implementation of Visual Basic that is built into Microsoft products.
Use UBound(eLookUp, 2) to obtain the rightmost column index of the two-dimensional array; .End cannot be called on eLookUp(1, 1) because that expression returns an array value, not an Excel Range.
Dim lastCol As Long
lastCol = UBound(eLookUp, 2)
MsgBox lastCol
UBound(array, dimension) returns the largest available subscript, and dimension 2 represents the columns of a two-dimensional array.^1^
If the array is not based at column 1
To get the number of columns regardless of whether the array is zero-based or one-based:
Dim columnCount As Long
columnCount = UBound(eLookUp, 2) - LBound(eLookUp, 2) + 1
MsgBox columnCount
If the array was loaded from a worksheet range and an absolute worksheet column number is required, retain the source range:
Dim source As Range
Dim eLookUp As Variant
Dim lastSheetCol As Long
Set source = Worksheets("Sheet1").Range("D2:J20")
eLookUp = source.Value2
lastSheetCol = source.Column + UBound(eLookUp, 2) - LBound(eLookUp, 2)
MsgBox lastSheetCol
The Column property returns the worksheet column number, where A is 1 and B is 2.^2^
If trailing array elements can be empty
UBound returns the allocated rightmost index, not necessarily the last non-empty element. Search the required array row backward:
Dim col As Long
Dim lastUsedCol As Long
For col = UBound(eLookUp, 2) To LBound(eLookUp, 2) Step -1
If Not IsEmpty(eLookUp(1, col)) Then
lastUsedCol = col
Exit For
End If
Next col
MsgBox lastUsedCol
Adjust 1 if the target array row has a different lower bound.
For an actual worksheet cell
When the starting expression is a Range, the direction constant is xlToRight, not xlRight:
Dim lastCol As Long
lastCol = Worksheets("Sheet1").Range("B4").End(xlToRight).Column
MsgBox lastCol
Range.End returns the cell at the end of the current region, equivalent to pressing End and then the appropriate arrow key.^3^ Consequently, it stops at a region boundary and is not necessarily the last populated cell anywhere in that row.
References