.End(xlright) syntax

Thomas Hamilton 0 Reputation points
2026-12-02T16:28:43.71+00:00

How do you code for finding the column of the last used cell to the right of a referenced cell? This code does not work for the virtual array eLookUp: MsgBox eLookUp(1, 1).End(xlRight).Column

Developer technologies | Visual Basic for Applications

1 answer

Sort by: Most helpful
  1. AI answer

    2026-12-02T16:29:15.6333333+00:00

    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

    1. UBound function
    2. Range.Column property (Excel)
    3. Range.End property (Excel)
    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.