Artikoli

Ihe atụ nke Excel Macros nke ejiri VBA dee

Edere ihe atụ macro Excel ndị a site na iji VBA 

Oge agụpụtara echere: 3 minuti

Ihe atụ VBA na-eji Array

Usoro Sub na-esonụ na-agụ ụkpụrụ sitere na sel na kọlụm A nke akwụkwọ ọrụ na-arụ ọrụ, ruo mgbe ọ zutere cell oghere. A na-echekwa ụkpụrụ ndị ahụ n'usoro. Ihe atụ macro Excel a dị mfe na-egosi iji:

  • Nkwupụta ndị dị iche iche;
  • Usoro dị ike;
  • okirikiri Do Until;
  • Rụtụ aka na mkpụrụ ndụ na akwụkwọ ọrụ Excel ugbu a;
  • Ọrụ VBA Ubound builtin (nke na-eweghachi ndepụta kachasị elu nke n'usoro).
' 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

Usoro a na-echekwa ụkpụrụ na kọlụm A nke akwụkwọ ọrụ na-arụ ọrụ n'usoro, rịba ama na:

  • okirikiri Do Until na-ewepụta ụkpụrụ nke cell ọ bụla na kọlụm A nke akwụkwọ ọrụ na-arụ ọrụ, na-eleghara sel oghere anya
  • Ọnọdụ ahụ"If UBound(dCellValues) < iRow" na-enyocha na dCellValues ​​array buru ibu iji jide ozi ahụ, ọ bụrụ na ọ bụghị, jiri ReDim mee ka nhazi ahụ dịkwuo 10.
  • N'ikpeazụ, agụmakwụkwọ​​dCellValues(iRow) = Cells(iRow, 1).Value" Na-echekwa cell ugbu a na CellValue n'usoro

Ihe atụ VBA nwere arụmọrụ mgbakọ na mwepụ

Usoro nke a na-esote na-agụ ụkpụrụ sitere na kọlụm A nke akwụkwọ ọrụ akpọrọ "Sheet2" ma na-arụ ọrụ mgbakọ na mwepụ na ụkpụrụ. A na-ebipụta ụkpụrụ ndị si na kọlụm A nke akwụkwọ ọrụ na-arụ ọrụ ugbu a.

Nke a macro na-egosi:

Akwụkwọ akụkọ ọhụrụ
Agbagharala akụkọ kacha mkpa maka ime ọhụrụ. Debanye aha iji nweta ha site na email.
  • Nkwupụta ndị dị iche iche;
  • Ihe Excel (kpọmkwem, iji isiokwu Set na otu esi enweta ihe 'Column' site na ihe 'Ibé akwụkwọ');
  • okirikiri Do Until;
  • Nweta akwụkwọ ọrụ na nkeji cell na akwụkwọ ọrụ Excel ugbu a.
' 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

Ihe atụ VBA nwere ndekọ ụbọchị mgbanwe

Ka anyị dee nnukwu VBA dị mfe nke na-ere ọkụ mgbe emelitere cell n'ụdị mpempe akwụkwọ anyị. Ka e were ya na ịchọrọ soro mgbanwe dị na kọlụm B (B4 ruo B11) wee dekọọ ụbọchị na oge mgbanwe na kọlụm A.
Ka anyị gaba n'ihu dịka nke a:

  • Na taabụ Developer pịa nhọrọ"Visual Basic” ka imepe VBA editọ.
  • Na VBA nchịkọta akụkọ, pịa koodu nchịkọta akụkọ ugboro abụọ metụtara Sheet2.
  • Họrọ mpempe akwụkwọ n'aka nri (ma ọ bụ aka ekpe) taabụ wee họrọ nhọrọ Gbanwee.
  • Tinye koodu 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

Chekwaa akwụkwọ ọrụ site na macro agbanyere (dịka ọmụmaatụ, dị ka faịlụ .xlsm).


Ugbu a, oge ọ bụla anyị na-emelite cell na kọlụm B (site na ahịrị 1 ruo n'ahịrị 10), cell na kọlụm A ga-egosipụta ụbọchị na oge dị ugbu a ozugbo.

Ercole Palmeri

Akwụkwọ akụkọ ọhụrụ
Agbagharala akụkọ kacha mkpa maka ime ọhụrụ. Debanye aha iji nweta ha site na email.

Ihe odide ndị a

Veeam nwere nkwado zuru oke maka ransomware, site na nchedo ruo nzaghachi na mgbake

Coveware nke Veeam ga-aga n'ihu na-enye ọrụ nzaghachi ihe omume cyber. Coveware ga-enye forensics na ike mmezi…

23 April 2024

Mgbanwe Green na Dijitalụ: Otu amụma amụma si agbanwe ụlọ ọrụ mmanụ & gas

Ndozi amụma na-agbanwe mpaghara mmanụ & gas, na-enwe usoro ọhụrụ na ngwa ngwa maka njikwa ihe ọkụkụ.…

22 April 2024

Onye na-ahụ maka mgbochi ntụkwasị obi UK na-ewelite mkpu BigTech na GenAI

UK CMA enyela ịdọ aka ná ntị gbasara omume Big Tech na ahịa ọgụgụ isi. Ebe ahụ…

18 April 2024

Casa Green: mgbanwe ike maka ọdịnihu na-adigide na Italy

Iwu “Case Green”, nke European Union chepụtara iji kwalite nrụpụta ike nke ụlọ, ejirila…

18 April 2024