Tempo estimado de lectura: 3 minutos
O seguinte procedemento Sub le os valores das celas da columna A da folla de traballo activa ata que atopa unha cela en branco. Os valores almacénanse nunha matriz. Este sinxelo exemplo de macro de Excel ilustra o uso de:
Do Until
;Ubound
builtin (que devolve o índice máis alto dunha matriz).' 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
O procedemento almacena os valores na columna A da folla de traballo activa nunha matriz, teña en conta que:
Do Until
extrae os valores de cada cela na columna A da folla de traballo activa, ignorando as celas en brancoIf UBound(dCellValues) < iRow
” comproba que a matriz dCellValues é o suficientemente grande como para albergar a información, se non, usa ReDim para aumentar o tamaño da matriz en 10dCellValues(iRow) = Cells(iRow, 1).Value
” Almacena a cela actual na matriz CellValuesO seguinte procedemento Sub le os valores da columna A da folla de traballo chamada "Folla 2" e realiza operacións aritméticas sobre os valores. Os valores resultantes están impresos na columna A da folla de traballo activa actual.
Esta macro ilustra:
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
Escribamos unha macro VBA sinxela que se active cando se actualiza unha cela dun rango específico da nosa folla. Supoña que quere facer un seguimento dos cambios na columna B (B4 a B11) e rexistrar a data e a hora do cambio na columna A.
Procedamos así:
Developer
prema na opción "Visual Basic
” para abrir o editor de VBA. 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
Garda o libro de traballo coas macros activadas (por exemplo, como ficheiro .xlsm).
Agora, cada vez que actualizamos unha cela da columna B (da fila 1 á fila 10), a cela da columna A mostrará automaticamente a data e hora actual.
Ercole Palmeri
O sector naval é unha verdadeira potencia económica mundial, que navega cara a un mercado de 150 millóns...
O pasado luns, o Financial Times anunciou un acordo con OpenAI. FT licencia o seu xornalismo de clase mundial...
Millóns de persoas pagan por servizos de streaming, pagando taxas de subscrición mensuais. É unha opinión común que vostede...
Coveware by Veeam continuará ofrecendo servizos de resposta a incidentes de extorsión cibernética. Coveware ofrecerá capacidades forenses e de remediación...