Tempo estimado de leitura: 3 minutos
O procedimento Sub a seguir lê os valores das células na coluna A da planilha ativa, até encontrar uma célula em branco. Os valores são armazenados em um array. Este exemplo simples de macro do Excel ilustra o uso de:
Do Until
;Ubound
builtin (que retorna o índice mais alto de um array).' 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 procedimento armazena os valores da coluna A da planilha ativa em um array, observe que:
Do Until
extrai os valores de cada célula da coluna A da planilha ativa, ignorando as células em brancoIf UBound(dCellValues) < iRow
” verifica se o array dCellValues é grande o suficiente para conter as informações, caso contrário, use ReDim para aumentar o tamanho do array em 10dCellValues(iRow) = Cells(iRow, 1).Value
” Armazena a célula atual no array CellValuesO procedimento Sub a seguir lê os valores da coluna A da planilha chamada “Planilha2” e executa operações aritméticas nos valores. Os valores resultantes são impressos na coluna A da planilha ativa atual.
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
Vamos escrever uma macro VBA simples que seja acionada quando uma célula em um intervalo específico de nossa planilha for atualizada. Suponha que você queira acompanhar as alterações na coluna B (B4 a B11) e registrar a data e hora da alteração na coluna A.
Vamos proceder assim:
Developer
clique na opção “Visual Basic
”Para abrir o editor 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
Salve a pasta de trabalho com macros habilitadas (por exemplo, como um arquivo .xlsm).
Agora, toda vez que atualizarmos uma célula na coluna B (da linha 1 à linha 10), a célula na coluna A exibirá automaticamente a data e hora atuais.
Ercole Palmeri
O setor naval é uma verdadeira potência económica global, que navegou para um mercado de 150 mil milhões...
Na segunda-feira passada, o Financial Times anunciou um acordo com a OpenAI. O FT licencia seu jornalismo de classe mundial…
Milhões de pessoas pagam por serviços de streaming, pagando assinaturas mensais. É opinião comum que você…
A Coveware by Veeam continuará a fornecer serviços de resposta a incidentes de extorsão cibernética. A Coveware oferecerá recursos forenses e de remediação…