Dynamic Range in VBA offest issues

Viewed 32

tried many examples and followed many videos but no luck please help I am trying to create a dynamic range starting at E2

try 1

Set StatusCol = Sheet1.Range("E2:E10")

try 2

Set StatusCol = OFFSET(Sheet1!$E$2,0,0,COUNTA(Sheet1!E2:E500),1)
3 Answers

Try this

set statuscol = Sheet1.Range("E2:E" & Sheet1.cells(Rows.count,1).End(xlUp).row)

You can then run statuscol.select to make sure your desired range is selected

Note: the 1 assumes you have data in every row of column 1 (A). change it to a different column number if that is not the case.

If you want to use built in excel function to vba then have to use Application.WorkSheetFunction method. Otherwise can try below sub.

Sub TillLastCol()
Dim StatusCol As Range
Dim sh As Worksheet
Dim lr As Long

    Set sh = Sheets("Sheet1")
    lr = sh.Cells(sh.Rows.Count, "E").End(xlUp).Row
    
    Set StatusCol = sh.Range("E2:E" & lr)
    'Debug.Print StatusCol.Address
End Sub

Reference Dynamic One-Column Range

Option Explicit

Sub ReferenceColumnRangeSHORT()
' No Constants
' No With Statement
' Ultra Short Message
' One Variable

    ' Reference the one-column range ('StatusCol').
    Dim StatusCol As Range: Set StatusCol _
        = Sheet1.Range("E2", Sheet1.Cells(Sheet1.Rows.Count, "E").End(xlUp))

    ' Continue using 'StatusCol'...

    ' Inform using the Immediate window ('Ctrl+G').
    Debug.Print StatusCol.Address(0, 0)
    ' Inform using a message box.
    MsgBox StatusCol.Address(0, 0), vbInformation, _
        "Reference Dynamic One-Column Range"

End Sub

Sub ReferenceColumnRange()
' No Constants
' No With Statement
' Short Message

    ' Reference the first cell ('FirstCell').
    Dim FirstCell As Range: Set FirstCell = Sheet1.Range("E2")
    ' Reference the last cell ('LastCell').
    Dim LastCell As Range
    Set LastCell = Sheet1.Cells(Sheet1.Rows.Count, FirstCell.Column).End(xlUp)
    ' Reference the one-column range ('StatusCol').
    Dim StatusCol As Range: Set StatusCol = Sheet1.Range(FirstCell, LastCell)

    ' Continue using 'StatusCol'...

    ' Store a message in a string variable.
    Dim MsgString As String: MsgString = _
        "The Status column range's address is '" _
        & StatusCol.Address(0, 0) & "'."
    
    ' Inform using the Immediate window ('Ctrl+G').
    Debug.Print MsgString
    ' Inform using a message box.
    MsgBox MsgString, vbInformation, "Reference Dynamic One-Column Range"

End Sub

Sub ReferenceColumnRangeEDU()

' What you know:
'     1.) The worksheet is in the workbook containing this code
'         and its code name is 'Sheet1'.
'     2.) The first (top-most) cell of your one-column range is cell 'E2'.
'     3.) Your range variable's name will be 'StatusCol'
' What you don't know:
'     1.) How to reference the last (bottom-most) non-empty cell
'         in column 'E'?
'     2.) How to reference the range from the first to the last cell?

    ' Store the address of the first cell
    ' in a constant string variable ('FirstCellAddress').
    Const FirstCellAddress As String = "E2"
    
    ' Declare the range variables.
    Dim StatusCol As Range
    Dim FirstCell As Range
    Dim LastCell As Range
    
    ' Using the With statement, reference the worksheet
    ' by its code name ('Sheet1').
    With Sheet1
        ' Reference the first cell ('FirstCell').
        Set FirstCell = .Range(FirstCellAddress)
        ' Reference the last cell ('LastCell').
        Set LastCell = .Cells(.Rows.Count, FirstCell.Column).End(xlUp)
        ' Reference the one-column range ('StatusCol').
        Set StatusCol = .Range(FirstCell, LastCell)
    End With

    ' Continue using 'StatusCol'...

    ' Store some stats in a string variable.
    Dim MsgString As String: MsgString = vbLf & "Stats" & vbLf & vbLf & _
        "The Status column range's address is '" _
        & StatusCol.Address(0, 0) & "'." & vbLf _
        & "It is located in the worksheet '" & Sheet1.Name & "' (tab name)." _
        & vbLf & "The worksheet is located in the workbook (file) '" _
        & ThisWorkbook.Name & "'." & vbLf _
        & "The file is located in the folder '" & ThisWorkbook.Path & "'."
    
    ' Inform using the Immediate window ('Ctrl+G').
    Debug.Print MsgString
    
    ' Inform using a message box.
    MsgBox MsgString, vbInformation, "Reference Dynamic One-Column Range"

End Sub
Related