Numatomas skaitymo laikas: 3 minuti
Ši antrinė procedūra nuskaito reikšmes iš aktyvaus darbalapio A stulpelio langelių, kol atsiranda tuščias langelis. Vertės saugomos masyve. Šis paprastas „Excel“ makrokomandos pavyzdys iliustruoja, kaip naudoti:
Do Until
;Ubound
builtin (kuris grąžina aukščiausią masyvo indeksą).' 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
Procedūra išsaugo aktyvaus darbalapio A stulpelio reikšmes masyve, atkreipkite dėmesį, kad:
Do Until
ištraukia kiekvieno aktyvaus darbalapio A stulpelio langelio reikšmes, nepaisydamas tuščių langeliųIf UBound(dCellValues) < iRow
“ patikrina, ar dCellValues masyvas yra pakankamai didelis, kad tilptų informacija, jei ne, naudokite ReDim, kad padidintumėte masyvo dydį 10dCellValues(iRow) = Cells(iRow, 1).Value
“ Saugo dabartinį langelį CellValues masyveŠi antrinė procedūra nuskaito reikšmes iš darbalapio „Sheet2“ stulpelio A ir atlieka aritmetines vertes. Gautos reikšmės atspausdinamos esamo aktyvaus darbalapio A stulpelyje.
Ši makrokomanda iliustruoja:
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
Parašykime paprastą VBA makrokomandą, kuri suaktyvinama, kai atnaujinamas konkretaus lapo diapazono langelis. Tarkime, kad norite stebėti pakeitimus B stulpelyje (B4–B11) ir įrašyti pakeitimo datą ir laiką A stulpelyje.
Tęskime taip:
Developer
spustelėkite parinktį "Visual Basic
“, kad atidarytumėte VBA redaktorių. 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
Išsaugokite darbaknygę su įjungtomis makrokomandomis (pavyzdžiui, kaip .xlsm failą).
Dabar kiekvieną kartą, kai atnaujiname B stulpelio langelį (nuo 1 eilutės iki 10 eilutės), A stulpelio langelyje automatiškai bus rodoma dabartinė data ir laikas.
Ercole Palmeri
„Coveware by Veeam“ ir toliau teiks reagavimo į kibernetinio turto prievartavimo incidentus paslaugas. „Coveware“ pasiūlys teismo ekspertizės ir ištaisymo galimybes…
Nuspėjama priežiūra keičia naftos ir dujų sektorių, taikant naujovišką ir iniciatyvų požiūrį į gamyklų valdymą.…
JK CMA paskelbė įspėjimą dėl Big Tech elgesio dirbtinio intelekto rinkoje. Ten…
„Žaliųjų namų“ dekretas, kurį Europos Sąjunga suformulavo siekiant padidinti pastatų energijos vartojimo efektyvumą, baigė teisėkūros procesą su…