简体   繁体   中英

How do I select the last cell in my active range?

I need to select the last cell in my specified range.

I am pulling data from a database. I successfully go to the right sheet, and find & select the variable number of managers (starting two rows down from "Number of Managers". Now I just need to select the last line of the active cells and run my If statement below.

Windows("MCS Cumulative_Data.xls").Activate
Sheets("Sheet 12").Select
Range("A1").Select
Cells.Find(What:="Number of Managers", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(2, 0).Select
Range(ActiveCell, ActiveCell.End(xlDown)).Select

' Part of Code, but not Important to question.

ActiveCell.Offset(0, 1).Select
If ActiveCell.Value = "" Then
ActiveCell.FormulaR1C1 = "0"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "0"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "0"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "0"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "0"  
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "0"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "0"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "0"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "0"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "0"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "0"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "0"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "0"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "0"
Else
End If

I've tried about 15 different Dim statements from other questions. They all seem like be close to asking what I am, but not exactly.

How about this:

    lastrow = Range(ActiveCell, ActiveCell.End(xlDown)).Cells.Count 'gets the last row from active cell
    NumRow = ActiveCell.Row 'gets the colum the active cell is in
    Cells(lastrow, NumRow).Select 'selects that cell

a possible example of "how to avoid select" (and some other little tips), would "collapse" your code to the following:

With Workbooks("MCS Cumulative_Data.xls").Sheets("Sheet 12")
    With .Cells.Find(What:="Number of Managers", After:=.Cells(1, 1), _
                     LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
                     SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Offset(2, 0).End(xlDown).Offset(0, 1)            
        If .Value = vbNullString Then .Resize(1, 13).Value = "0"                        
    End With
End With

ActiveCell.End(xlDown).Select

Does not select the last sell in a selected group, but does grab the cell I need.

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