Geschätzte Lesezeit: 3 minuti
Die folgende Sub-Prozedur liest Werte aus Zellen in Spalte A des aktiven Arbeitsblatts, bis sie auf eine leere Zelle trifft. Die Werte werden in einem Array gespeichert. Dieses einfache Excel-Makrobeispiel veranschaulicht die Verwendung von:
Do Until
;Ubound
eingebaut (gibt den höchsten Index eines Arrays zurück).' Sub procedure store values in Column A of the active Worksheet
' into an array
Sub GetCellValues()
Dim iRow As Integer ' stores the current row number
Dim dCellValues() As Double ' array to store the cell values
iRow = 1
ReDim dCellValues(1 To 10)
' Do Until loop to extract the value of each cell in column A
' of the active Worksheet, as long as the cell is not blank
Do Until IsEmpty(Cells(iRow, 1))
' Check that the dCellValues array is big enough
' If not, use ReDim to increase the size of the array by 10
If UBound(dCellValues) < iRow Then
ReDim Preserve dCellValues(1 To iRow + 9)
End If
' Store the current cell in the CellValues array
dCellValues(iRow) = Cells(iRow, 1).Value
iRow = iRow + 1
Loop
End Sub
Die Prozedur speichert die Werte in Spalte A des aktiven Arbeitsblatts in einem Array. Beachten Sie Folgendes:
Do Until
Extrahiert die Werte jeder Zelle in Spalte A des aktiven Arbeitsblatts und ignoriert leere ZellenIf UBound(dCellValues) < iRow
„Überprüft, ob das dCellValues-Array groß genug ist, um die Informationen aufzunehmen. Wenn nicht, verwenden Sie ReDim, um die Größe des Arrays um 10 zu erhöhendCellValues(iRow) = Cells(iRow, 1).Value
„Speichert die aktuelle Zelle im CellValues-ArrayDie folgende Sub-Prozedur liest die Werte aus Spalte A des Arbeitsblatts mit dem Namen „Sheet2“ und führt arithmetische Operationen an den Werten durch. Die resultierenden Werte werden in Spalte A des aktuell aktiven Arbeitsblatts gedruckt.
Dieses Makro veranschaulicht:
Do Until
;' Sub procedure to loop through the values in Column A of the Worksheet
' "Sheet2", perform arithmetic operations on each value, and write the
' result into Column A of the current Active Worksheet ("Sheet1")
Sub Transfer_ColA()
Dim i As Integer
Dim Col As Range
Dim dVal As Double
' Set the variable 'Col' to be Column A of Sheet 2
Set Col = Sheets("Sheet2").Columns("A")
i = 1
' Loop through each cell of the column 'Col' until
' a blank cell is encountered
Do Until IsEmpty(Col.Cells(i))
' Apply arithmetic operations to the value of the current cell
dVal = Col.Cells(i).Value * 2 + 1
' The command below copies the result into Column A
' of the current Active Worksheet - no need to specify
' the Worksheet name as it is the active Worksheet.
Cells(i, 1) = dVal
i = i + 1
Loop
End Sub
Schreiben wir ein einfaches VBA-Makro, das ausgelöst wird, wenn eine Zelle in einem bestimmten Bereich unseres Blattes aktualisiert wird. Angenommen, Sie möchten Änderungen in Spalte B (B4 bis B11) verfolgen und Datum und Uhrzeit der Änderung in Spalte A aufzeichnen.
Gehen wir so vor:
Developer
Klicken Sie auf die Option „Visual Basic
”, um den VBA-Editor zu öffnen. Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B1:B10")) Is Nothing Then
Target.Range("A1:A1").Value = Now
End If
End Sub
Speichern Sie die Arbeitsmappe mit aktivierten Makros (z. B. als XLSM-Datei).
Jedes Mal, wenn wir nun eine Zelle in Spalte B aktualisieren (von Zeile 1 bis Zeile 10), zeigt die Zelle in Spalte A automatisch das aktuelle Datum und die aktuelle Uhrzeit an.
Ercole Palmeri
Coveware by Veeam wird weiterhin Dienstleistungen zur Reaktion auf Cyber-Erpressungsvorfälle anbieten. Coveware wird Forensik- und Sanierungsfunktionen anbieten …
Die vorausschauende Wartung revolutioniert den Öl- und Gassektor mit einem innovativen und proaktiven Ansatz für das Anlagenmanagement.…
Die britische CMA hat eine Warnung zum Verhalten von Big Tech auf dem Markt für künstliche Intelligenz herausgegeben. Dort…
Das „Green Houses“-Dekret, das von der Europäischen Union zur Verbesserung der Energieeffizienz von Gebäuden erlassen wurde, hat seinen Gesetzgebungsprozess mit… abgeschlossen.