简体   繁体   中英

Selecting the last used cell in column A and then extend it to column H

Hi there I am trying to select a range "A2:H2" down to the last filled cell based on column A (so in this case it should select "A2:H59" ). The range is not fixed so it cannot be defined with exact numbers. I have the following code, but it selects everything down to the 402nd row even though there is no data beyond "A59" in the sheet. Any idea what is going on? Thanks for the help!

Global ssaw As Worksheet
Global trckr As Worksheet
Sub DataF()
Dim myRange As Range
Dim myCell As Range

    Set ssaw = Sheets("SSAW_DATA")
    Set trckr = Sheets("SQL_DATA_FEED")
    Set myRange = trckr.Range("A2:H2").end(xlDown)


        With myRange
            .SpecialCells(xlCellTypeBlanks).Interior.Color = RGB(255, 102, 102)
            .SpecialCells(xlCellTypeBlanks).Value = "#missing#"
        End With

End Sub

If we assume your last used cell in column A is A59 then …

… This

Set myRange = trckr.Range("A2", trckr.Range("A2").End(xlDown))

will select A2:A59 and this

.Resize(ColumnSize:=8)

will resize it to make it 8 columns width that is A2:H59 .

So together we get:

Set myRange = trckr.Range("A2", trckr.Range("A2").End(xlDown)).Resize(ColumnSize:=8)

Use this

trckr.Range("A" & trckr.Rows.Count).End(xlUp) 

alternatively to find the last used cell in column A if there can be empty cells in between:

Set myRange = trckr.Range("A2", trckr.Range("A" & trckr.Rows.Count).End(xlUp)).Resize(ColumnSize:=8)

exploit the fact that Range(cell1, cell2) is equivalent to Range(cell2, cell1)

Set myRange = trckr.Range("H2", trckr.Range("A2").End(xlDown))

while if you want to select a range from A2:H2 down to column A last not empty cell (ie included empty cells along column A in between the first and last not empty ones):

Set myRange = trckr.Range("H2", trckr.Cells(trckr.Rows.Count, 1).End(xlUp))

I would suggest to use the following code

Option Explicit

Function LastRowInColumn(colName As String)
    Dim lastRow As Long
    With ActiveSheet
        lastRow = .Cells(.Rows.Count, colName).End(xlUp).Row
    End With
    LastRowInColumn = lastRow
End Function


Sub SelectRg()
Dim rg As Range
Dim wks As Worksheet
Dim lastRow As Long

    lastRow = LastRowInColumn("A")
    Debug.Print lastRow

    If lastRow = 1 Then
        ' do nothing
    Else
        Set wks = ActiveSheet
        With wks
            Set rg = Range(.Cells(2, 1), .Cells(lastRow, "H"))
            rg.Select
        End With
    End If

End Sub

The code determins the last filled row in column A and select based on this information everything to column H

EDIT Improved function

Function LastRowInColumn(ByVal wks As Worksheet, ByVal colName As String) As Long
    With wks
        LastRowInColumn = .Cells(.Rows.Count, colName).End(xlUp).Row
    End With
End Function

EDIT2 And if one would not like to use an extra function you could do it like that

Sub SetRg()

Dim rg As Range
Dim wks As Worksheet
Dim lastRow As Long

    Set wks = ActiveSheet
    With wks
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        'lastRow = LastRowInColumn(wks, "A")
        If lastRow > 1 Then
            Set rg = Range(.Cells(2, 1), .Cells(lastRow, "H"))
        End If
    End With

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM