Verweisen auf Zeilen und Spalten
Verwenden Sie die Rows-Eigenschaft oder die Columns-Eigenschaft , um mit ganzen Zeilen oder Spalten zu arbeiten. Diese Eigenschaften geben ein Range -Objekt zurück, das einen Zellbereich darstellt. Im folgenden Beispiel gibt Rows(1)
die erste Zeile auf Sheet1 zurück. Anschließend wird die Bold -Eigenschaft des Font -Objekts für diesen Bereich auf True festgelegt.
Sub RowBold()
Worksheets("Sheet1").Rows(1).Font.Bold = True
End Sub
Die folgende Tabelle verdeutlicht, wie Zeilen- und Spaltenbezüge mit den Eigenschaften Rows und Columns verwendet werden können.
Referenz | Bedeutung |
---|---|
Rows(1) |
Zeile eins |
Rows |
Alle Zeilen des Arbeitsblatts |
Columns(1) |
Spalte eins |
Columns("A") |
Spalte eins |
Columns |
Alle Spalten des Arbeitsblatts |
Um mit mehreren Zeilen oder Spalten gleichzeitig zu arbeiten, erstellen Sie eine Objektvariable, und verwenden Sie die Union-Methode , indem Sie mehrere Aufrufe der Rows- oder Columns-Eigenschaft kombinieren. Im folgenden Beispiel wird das Format der Zeilen 1, 3 und 5 auf arbeitsblatt 1 in der aktiven Arbeitsmappe fett formatiert.
Sub SeveralRows()
Worksheets("Sheet1").Activate
Dim myUnion As Range
Set myUnion = Union(Rows(1), Rows(3), Rows(5))
myUnion.Font.Bold = True
End Sub
Beispielcode bereitgestellt von: Dennis Wallentin, VSTO & .NET & Excel In diesem Beispiel werden die leeren Zeilen aus einem ausgewählten Bereich gelöscht.
Sub Delete_Empty_Rows()
'The range from which to delete the rows.
Dim rnSelection As Range
'Row and count variables used in the deletion process.
Dim lnLastRow As Long
Dim lnRowCount As Long
Dim lnDeletedRows As Long
'Initialize the number of deleted rows.
lnDeletedRows = 0
'Confirm that a range is selected, and that the range is contiguous.
If TypeName(Selection) = "Range" Then
If Selection.Areas.Count = 1 Then
'Initialize the range to what the user has selected, and initialize the count for the upcoming FOR loop.
Set rnSelection = Application.Selection
lnLastRow = rnSelection.Rows.Count
'Start at the bottom row and work up: if the row is empty then
'delete the row and increment the deleted row count.
For lnRowCount = lnLastRow To 1 Step -1
If Application.CountA(rnSelection.Rows(lnRowCount)) = 0 Then
rnSelection.Rows(lnRowCount).Delete
lnDeletedRows = lnDeletedRows + 1
End If
Next lnRowCount
rnSelection.Resize(lnLastRow - lnDeletedRows).Select
Else
MsgBox "Please select only one area.", vbInformation
End If
Else
MsgBox "Please select a range.", vbInformation
End If
'Turn screen updating back on.
Application.ScreenUpdating = True
End Sub
In diesem Beispiel werden die leeren Spalten aus einem ausgewählten Bereich gelöscht.
Sub Delete_Empty_Columns()
'The range from which to delete the columns.
Dim rnSelection As Range
'Column and count variables used in the deletion process.
Dim lnLastColumn As Long
Dim lnColumnCount As Long
Dim lnDeletedColumns As Long
lnDeletedColumns = 0
'Confirm that a range is selected, and that the range is contiguous.
If TypeName(Selection) = "Range" Then
If Selection.Areas.Count = 1 Then
'Initialize the range to what the user has selected, and initialize the count for the upcoming FOR loop.
Set rnSelection = Application.Selection
lnLastColumn = rnSelection.Columns.Count
'Start at the far-right column and work left: if the column is empty then
'delete the column and increment the deleted column count.
For lnColumnCount = lnLastColumn To 1 Step -1
If Application.CountA(rnSelection.Columns(lnColumnCount)) = 0 Then
rnSelection.Columns(lnColumnCount).Delete
lnDeletedColumns = lnDeletedColumns + 1
End If
Next lnColumnCount
rnSelection.Resize(lnLastColumn - lnDeletedColumns).Select
Else
MsgBox "Please select only one area.", vbInformation
End If
Else
MsgBox "Please select a range.", vbInformation
End If
'Turn screen updating back on.
Application.ScreenUpdating = True
End Sub
Über den Autor
Dennis Wallentin ist Autor des Blogs VSTO & .NET & Excel, dessen Schwerpunkt auf .NET Framework-Lösungen für Excel und Excel Services liegt. Dennis entwickelt Excel-Lösungen seit über 20 Jahren und ist Co-Autor von "Professional Excel Development: The Definitive Guide to Developing Applications Using Microsoft Excel, VBA and .NET (2nd Edition)."
Support und Feedback
Haben Sie Fragen oder Feedback zu Office VBA oder zu dieser Dokumentation? Unter Office VBA-Support und Feedback finden Sie Hilfestellung zu den Möglichkeiten, wie Sie Support erhalten und Feedback abgeben können.