excel vbaselect最后一行和最后一列

我试图从A9selectlastrow & lastcolumn

我有这个select最后一个单元格,但它不会select从A9 to Last它只是selectlastrow / lastcolumn。 这也是不理想的,因为如果我将来有空白的话。

我已经search,找不到任何东西从一个单元格selectlastrow & lastcolumn

 Sub FindLast() Application.ScreenUpdating = False Range("A9").End(xlToRight).End(xlDown).Select Application.ScreenUpdating = True End Sub 

在我的文件中的search顺序将是列A和第8行,如果有所帮助。

代码以下是我用于处理活动工作表的内容

 Sub SelectAll() Application.ScreenUpdating = False Dim lastRow As Long, lastCol As Long Dim Rng As Range Dim WholeRng As Range With ActiveWorksheet Set Rng = Cells 'last row lastRow = Rng.Find(What:="*", After:=Rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row 'last column lastCol = Rng.Find(What:="*", After:=Rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column Set WholeRng = Range(Cells(9, "A"), Cells(lastRow, lastCol)) WholeRng.Select End With Application.ScreenUpdating = True End Sub 

或者你可以利用UsedRange

 Sub FindLast() With Activesheet .Range(.Range("A9"), .UsedRange.Cells(.UsedRange.Rows.Count, .UsedRange.Columns.Count)).Select End With End Sub 

最安全的方法是使用Findfunction:

 Option Explicit Sub LastRow_Col_Find() ' Safest way to ensure you got the last row: Dim lastRow As Long, lastCol As Long Dim Rng As Range Dim WholeRng As Range With Worksheets("report") Set Rng = .Cells ' Safest way to ensure you got the last row lastRow = Rng.Find(What:="*", After:=Rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row 'MsgBox lastRow ' <-- for DEBUG ' Safest way to ensure you got the last column lastCol = Rng.Find(What:="*", After:=Rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column 'MsgBox lastColumn ' <-- for DEBUG ' set the Range for the entire UsedRange in "YourSheetName" sheet Set WholeRng = .Range(.Cells(9, "A"), .Cells(lastRow, lastCol)) WholeRng.Select '<-- ONLY IF YOU MUST End With End Sub