用品

使用 VBA 編寫的 Excel 巨集範例

以下簡單的 Excel 巨集範例是使用 VBA 編寫的 

預計閱讀時間: 3 minuti

使用數組的 VBA 範例

以下 Sub 程序會從活動工作表 A 列的儲存格讀取值,直到遇到空白儲存格。值儲存在數組中。這個簡單的 Excel 巨集範例說明了以下內容的用法:

  • 變數宣告;
  • 動態數組;
  • 一個循環 Do Until;
  • 引用目前Excel工作表中的儲存格;
  • VBA 函數 Ubound 內建(傳回數組的最高索引)。
' 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

此程序將活動工作表 A 列中的值儲存在陣列中,請注意:

  • 循環 Do Until 提取活動工作表 A 列中每個儲存格的值,忽略空白儲存格
  • 條件 ”If UBound(dCellValues) < iRow” 檢查 dCellValues 數組是否足夠大以容納信息,如果沒有,則使用 ReDim 將數組的大小增加 10
  • 最後是教育​​dCellValues(iRow) = Cells(iRow, 1).Value” 將目前儲存格儲存在CellValues陣列中

數學運算的 VBA 範例

以下 Sub 程序會從名為「Sheet2」的工作表的 A 欄位中讀取值,並對這些值執行算術運算。結果值列印在目前活動工作表的 A 欄位中。

該巨集說明:

創新通訊
不要錯過有關創新的最重要新聞。 註冊以通過電子郵件接收它們。
  • 變數宣告;
  • Excel 物件(具體來說,Set 關鍵字的使用以及如何從「Sheets」物件存取「Columns」物件);
  • 一個循環 Do Until;
  • 存取目前 Excel 工作簿中的工作表和儲存格區域。
' 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

帶有修改日期記錄的 VBA 範例

讓我們編寫一個簡單的 VBA 宏,當更新工作表特定範圍內的儲存格時觸發該巨集。假設您要追蹤 B 列(B4 到 B11)中的變更並在 A 列中記錄變更的日期和時間。
讓我們像這樣繼續:

  • 在選項卡中 Developer 按一下選項“Visual Basic” 開啟 VBA 編輯器。
  • 在VBA編輯器中,雙擊與Sheet2相關的程式碼編輯器。
  • 從右側(或左側)標籤中選擇“工作表”,然後選擇“變更”選項。
  • 新增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

儲存啟用巨集的工作簿(例如,作為 .xlsm 檔案)。


現在,每次我們更新 B 列中的儲存格(從第 1 行到第 10 行)時,A 列中的儲存格將自動顯示目前日期和時間。

Ercole Palmeri

創新通訊
不要錯過有關創新的最重要新聞。 註冊以通過電子郵件接收它們。

最近的文章

Veeam 為勒索軟體提供最全面的支持,從保護到回應和恢復

Veeam 的 Coveware 將繼續提供網路勒索事件回應服務。 Coveware 將提供取證和修復功能…

23月2024

綠色與數位革命:預測性維護如何改變石油和天然氣產業

預測性維護正在透過創新和主動的工廠管理方法徹底改變石油和天然氣行業。

22月2024

英國反壟斷監管機構對 GenAI 向 BigTech 發出警報

英國 CMA 對大型科技公司在人工智慧市場的行為發出了警告。那裡…

18月2024

Casa Green:義大利永續未來的能源革命

歐盟為提高建築物能源效率而製定的「綠色案例」法令已結束立法程序…

18月2024