Search code examples
excelvbams-accessrangecell

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.


Solution

  • 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