2017-04-11 15 views
1

Ich versuche, von A9 bis lastrow & lastcolumn zu wählen.Excel VBA wählen letzte Zeile und letzte Spalte

Ich habe dies zur Auswahl der letzten Zelle, aber es wählt nicht aus A9 to Last es wählt nur die Lastrow/lastcolumn. Auch das ist nicht ideal, wenn ich in Zukunft Blanks habe.

Ich habe gesucht und nichts für die Auswahl von einer Zelle in den lastrow & lastcolumn

Sub FindLast() 
Application.ScreenUpdating = False 

Range("A9").End(xlToRight).End(xlDown).Select 

Application.ScreenUpdating = True 
End Sub 

Suchauftrag in meiner Datei 8 wären Spalte A & Row finden könnte, wenn das überhaupt hilft.

Code unten ist, was ich auf aktive Blätter

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 
+0

Zuerst möchten Sie wahrscheinlich nicht wirklich 'wählen Sie es aus, finden Sie unter [Wie vermeidet man die Verwendung' .Select'/'.Activate'] (https://stackoverflow.com/questions/10714251/how-to-avoid -use-select-in-excel-vba-Makros). Wo haben Sie auch gesucht? Was hast du probiert? Diese Frage wurde oft gestellt (siehe "verwandte" Beiträge auf der rechten Seite). – BruceWayne

Antwort

2

Oder Sie könnten Usedrange ausnutzen

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

Der sicherste Weg zur Arbeit bin mit verwenden, um die Find Funktion:

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 
+0

Ihr aktualisierter Code funktioniert perfekt, vielen Dank! –