2010-11-03 17 views
5

Je voudrais sélectionner au bas de la feuille de calcul, mais pas en dessous de ce qui est utilisé/stocké. Je pourrais avoir 10 000 lignes, mais je n'en ai certainement pas 65 536. Je ne saurai pas à l'avance combien de lignes.Trouver le bas de la feuille de calcul Excel dans VBA

Dans Excel lui-même (dans les versions récentes, de toute façon, Excel 97 n'était pas si gentil), vous pouvez appuyer sur Ctrl + End pour être pris à la dernière ligne et la colonne. Je voudrais la même fonctionnalité.

Répondre

3

Le le plus simple est de commencer par le bas et de travailler jusqu'à trouver la dernière rangée qui contient quelque chose:
Range ("a65536"). end (xlup) .row

+0

Sauf si la feuille est remplie à la 65536e rangée .. ce qui dans 99% des cas, n'est pas le cas. Je ferais ceci sur la gamme ("A1) .end (xlDown) ou similaire –

+3

Si vous descendez XL s'arrête à la première cellule vide: souvent il y a plus de données après une cellule vide. vous n'avez pas ce problème. –

2

C'est élémentaire:

Selection.End(xlDown).Select 

(à cet en appuyant sur Ctrl + fin lors de l'enregistrement d'une macro.)

+0

Vous devez cependant faire attention, si vous avez une ligne vide, elle ne sélectionnera que jusqu'à la fin et ignorera tout ensuite. J'attrapais habituellement la rangée qui s'y trouvait, vérifiais les 2-3 prochaines cellules et si aucune correspondance n'était trouvée, acceptais-la, sinon je trouverais la 'fin' de cette zone et la boucle jusqu'à ce que j'aie 2 ou 3 blancs consécutifs. – pinkfloydx33

2
Public Sub Blank_Row_Remover() ' Start of Macro Code 

'Deletes the Entire Row within the Selection if _ 
Some of the Cells Within the Selection Contain No Data. 

Dim Start_Cell, End_Cell, Data_Info, End_Column, This_Column As Variant 

Application.ScreenUpdating = False 
Application.StatusBar = "Please Stand By, ('Removing Blank Rows...' ~ Macro In Progress)..." 

    Call Data_Info_Selection(Start_Cell, End_Cell, Data_Info, End_Column, This_Column) ' Direct Method 

    For Each Cell In Selection 
     Cell.Formula = Replace(Cell.Formula, Cell.Formula, Trim(Cell.Formula)) {Rids Extra Spaces} 
     'If InStr(Cell.Value, "Labels:") Then Cell.EntireRow.Clear 'Searching for a Particular String to Remove a Row 
     'If InStr(Cell.Value, " ") Then Cell.EntireRow.Clear 'Searching for another Particular String to Remove a Row {Like 4 Spaces in a Cell that Keeps it from Reading as a Blank} 
    Next 
     On Error Resume Next 
      Selection.SpecialCells(xlBlanks).EntireRow.Delete 
     On Error GoTo 0 

    'Call Data_Info_Selection(Start_Cell, End_Cell, Data_Info, End_Column, This_Column) ' Direct Method 

Application.ScreenUpdating = True 
End Sub 
Public Function Data_Info_Selection(ByRef Start_Cell, End_Cell, Data_Info, End_Column, This_Column As Variant) 

Application.ScreenUpdating = False 
Application.StatusBar = "Please Stand By, ('Selecting Partial Columns' ~ Macro In Progress)..." 

Start_Cell = ActiveCell.Address 
Start_Cell_Text = Range(Start_Cell).Text 

Orginal_Start_Cell = Range(Start_Cell).Address 

If Start_Cell_Text = "" Then 
    Dim Cells As Range 
     For Each Cell In Selection.Cells 
      If Cell = "" Then 
       Start_Cell = Cell.Address 
      Else 
       Start_Cell = Cell.Address 
       Exit For 
      End If 
     Next 
End If 

    This_Column = Mid(Start_Cell, 2, 1) 'ColumnNum = ActiveCell.Column 
     If Range(Start_Cell).Text = "" Then 
      End_Column = This_Column & ActiveCell.Row 
      End_Cell = Range(End_Column).Address 
     Else 
      End_Column = This_Column + "65536" 
      End_Cell = Range(End_Column).End(xlUp).Address 
      Start_Cell = Range(Orginal_Start_Cell).Address 
     End If 

    Data_Info = Range(Start_Cell, End_Cell).Address 
    Range(Data_Info).Select 

End Function 
Public Sub Select_Partial_Data_Start_Cell() ' (This Seems to Work and is Cleaner and Simplier) 

Application.ScreenUpdating = False 
Application.StatusBar = "Please Stand By, ('Selecting Partial Data' ~ Macro In Progress)..." 

Dim myLastRow As Long 
Dim myLastColumn As Long 

    Start_Cell = ActiveCell.Address 
    On Error Resume Next 
     myLastRow = Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row 
     myLastColumn = Cells.Find("*", [A1], , , xlByColumns, xlPrevious).Column 
     myLast_Cell = Cells(myLastRow, myLastColumn).Address 
    myRange = Start_Cell & ":" & myLast_Cell 
    'If InStr(1, myLast_Cell, "104876", 1) Then myLast_Cell = "$F$1105" 

Range(myRange).Select 
Application.ScreenUpdating = True 

End Sub' End of Macro Code 
0

Je l'ai utilisé ce précédemment

'This finds the last row in thr worksheet 
LR = Cells(Rows.Count, 18).End(xlUp).Row 
Range(Cells(LR, 1), Cells(LR, 35)).Select 
0

Cette fonction retourne pas de la ligne et de la colonne max max dans la feuille de calcul avec un certain contenu. Peut-être que ce sera utile pour sbdy. Bien sûr, cela est très lent, mais généralement, nous n'avons pas besoin de vérifier toutes les lignes et les colonnes, il faut donc ajuster les boucles.

Public Function content_area(shName As String) As Variant 
Dim res(1 To 2) As Integer 
Dim ark As Worksheet 

Set ark = ThisWorkbook.Sheets(shName) 
nCol = 0 
nRow = 0 

For i = 1 To ark.Columns.Count 
    temp = ark.Cells(ark.Cells(1, i).EntireColumn.Rows.Count, i).End(xlUp).Row 
    If temp > nCol Then nCol = temp 
Next 
For i = 1 To ark.Rows.Count 
    temp = ark.Cells(i, ark.Cells(i, 1).EntireRow.Columns.Count).End(xlToLeft).Column 
    If temp > nRow Then nRow = temp 
Next 

res(1) = nCol 
res(2) = nRow 

content_area = res 
End Function