Home > database >  set range using string variable for column letter
set range using string variable for column letter

Time:10-28

I'd like to know how to write this range changing "G" to a string variable strColumn.

This is the code I want to change:

   Dim lastRowElemento As Integer
   lastRowElemento = Cells(Rows.Count, "G").End(xlUp).Row
   Set rngElemento = ws.Range("G2:G" & lastRowElemento)

CodePudding user response:

Reference a 'Non-Empty' Column Range

  • There are actually two requirements:

    • ColumnString = G (I prefer string since e.g. XFD are letters)
    • FirstRow = 2

    If you put them together, you get G2 (think one, instead of two variables).

  • Since using the Find method is more reliable than using the End property in finding the bottom-most (last) non-empty cell in a column, I used it to write the RefColumn function which in your case can be utilized in the following way:

    Set rngElemento = RefColumn(ws.Range("G2"))
    
  • I'll leave it up to you if you're going to test if there is data (usually you know there is), but I prefer to keep at least a 'simplified' test in the code:

    If rngElemento is Nothing Then Exit Sub ' no data
    ' Continue...
    

The Code

Option Explicit

Sub RefColumnTEST()
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet1")
    Dim rg As Range: Set rg = RefColumn(ws.Range("G2"))
    If rg Is Nothing Then ' the range 'G2:G1048576' is empty
        MsgBox "No data.", vbCritical
    Else
        MsgBox rg.Address(0, 0), vbInformation
    End If
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose:      Creates a reference to the one-column range from the first cell
'               of a range ('FirstCell') to the bottom-most non-empty cell
'               of the first cell's worksheet column.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function RefColumn( _
    ByVal FirstCell As Range) _
As Range
    If FirstCell Is Nothing Then Exit Function
    
    With FirstCell.Cells(1)
        Dim lCell As Range
        Set lCell = .Resize(.Worksheet.Rows.Count - .Row   1) _
            .Find("*", , xlFormulas, , , xlPrevious)
        If lCell Is Nothing Then Exit Function
        Set RefColumn = .Resize(lCell.Row - .Row   1)
    End With

End Function

Meeting Your Requirements

  • Similarly to my preferred way, you could use the RefData function:
Function RefData( _
    ByVal ws As Worksheet, _
    ByVal ColumnIndex As Variant) _
As Range
    On Error GoTo ClearError
    
    With ws.Columns(ColumnIndex).Resize(ws.Rows.Count - 1).Offset(1)
        Set RefData = _
            .Resize(.Find("*", , xlFormulas, , , xlPrevious).Row - 1)
    End With

ProcExit:
    Exit Function
ClearError:
    Resume ProcExit
End Function

which you can utilize in the following way:

Set rngElemento = RefData(ws, "G")
Set rngElemento = RefData(ws, 7)
' or:
Const strColumn As String = "G"
Set rngElemento = RefData(ws, strColumn)

CodePudding user response:

Applying OP's method, try this:

Sub TEST()
Dim ws As Worksheet, Rng As Range, sCol As String
    sCol = "G"
    Set ws = ThisWorkbook.Sheets("TEST")    'change as required
    With ws.Columns(sCol)
        Set Rng = Range(.Cells(2), .Cells(.Rows.Count).End(xlUp))
    End With
    End Sub
  • Related