Artikel

Conto Excel Macro sing ditulis nganggo VBA

Conto makro Excel prasaja ing ngisor iki ditulis nggunakake VBA 

Perkiraan wektu maca: 3 menit

Tuladha VBA nggunakake Array

Prosedur Sub ngisor iki maca nilai saka sel ing kolom A saka lembar kerja sing aktif, nganti nemoni sel kosong. Nilai kasebut disimpen ing larik. Conto makro Excel sing prasaja iki nggambarake panggunaan:

  • Pranyatan variabel;
  • Susunan dinamis;
  • A siklus Do Until;
  • Deleng sel ing lembar kerja Excel saiki;
  • Fungsi VBA Ubound builtin (sing ngasilake indeks paling dhuwur saka 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

Prosedur nyimpen nilai ing kolom A saka lembar kerja aktif ing larik, cathet yen:

  • Siklus Do Until ngekstrak nilai saben sel ing kolom A saka lembar kerja aktif, ora nggatekake sel kosong
  • Syarat"If UBound(dCellValues) < iRow" Priksa manawa array dCellValues ​​cukup gedhe kanggo nahan informasi kasebut, yen ora, gunakake ReDim kanggo nambah ukuran array kanthi 10
  • Akhire, pendidikan​​dCellValues(iRow) = Cells(iRow, 1).Value” Nyimpen sel saiki ing array CellValues

Tuladha VBA karo operasi matematika

Sub prosedur ing ngisor iki maca nilai saka kolom A saka lembar kerja sing jenenge "Sheet2" lan nindakake operasi aritmetika ing nilai kasebut. Nilai asil dicithak ing kolom A saka lembar kerja aktif saiki.

Makro iki nggambarake:

newsletter inovasi
Aja kantun warta paling penting babagan inovasi. Mlebu kanggo nampa wong-wong mau liwat email.
  • Pranyatan variabel;
  • Obyek Excel (khusus, nggunakake tembung kunci Setel lan carane ngakses obyek 'Kolom' saka obyek 'Sheets');
  • A siklus Do Until;
  • Akses lembar kerja lan rentang sel ing buku kerja Excel saiki.
' 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

Tuladha VBA kanthi rekaman tanggal modifikasi

Ayo nulis makro VBA prasaja sing murub nalika sel ing sawetara tartamtu saka sheet kita dianyari. Upamane sampeyan pengin nglacak owah-owahan ing kolom B (B4 nganti B11) lan ngrekam tanggal lan wektu owah-owahan ing kolom A.
Ayo nerusake kaya iki:

  • Ing tab Developer klik pilihan"Visual Basic"kanggo mbukak editor VBA.
  • Ing editor VBA, klik kaping pindho editor kode sing ana gandhengane karo Sheet2.
  • Pilih Lembar Kerja saka tab tengen (utawa kiwa) banjur pilih opsi Ganti.
  • Tambah kode 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

Simpen buku kerja kanthi makro aktif (contone, minangka file .xlsm).


Saiki, saben kita nganyari sel ing kolom B (saka baris 1 nganti baris 10), sel ing kolom A bakal kanthi otomatis nampilake tanggal lan wektu saiki.

Ercole Palmeri

newsletter inovasi
Aja kantun warta paling penting babagan inovasi. Mlebu kanggo nampa wong-wong mau liwat email.

Artikel anyar

Veeam nduweni dhukungan paling lengkap kanggo ransomware, saka proteksi nganti respon lan pemulihan

Coveware dening Veeam bakal terus nyedhiyakake layanan respon insiden pemerasan cyber. Coveware bakal nawakake kemampuan forensik lan remediasi…

23 April 2024

Revolusi Ijo lan Digital: Kepiye Pangopènan Prediktif Ngowahi Industri Minyak & Gas

Pangopènan prediktif ngrevolusi sektor minyak & gas, kanthi pendekatan inovatif lan proaktif kanggo manajemen pabrik.…

22 April 2024

Regulator antitrust Inggris mundhakaken weker BigTech liwat GenAI

CMA UK wis ngetokake bebaya babagan prilaku Big Tech ing pasar intelijen buatan. Ana…

18 April 2024

Casa Green: revolusi energi kanggo masa depan sing lestari ing Italia

Keputusan "Case Green", sing dirumusake dening Uni Eropa kanggo ningkatake efisiensi energi bangunan, wis rampung proses legislatif kanthi…

18 April 2024