货物

使用 VBA 编写的 Excel 宏示例

以下简单的 Excel 宏示例是使用 VBA 编写的 

预计阅读时间: 3 分钟

使用数组的 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

创新通讯
不要错过有关创新的最重要新闻。 注册以通过电子邮件接收它们。

Articoli最新回应

卡塔尼亚综合诊所的 Apple 观众对增强现实进行创新干预

卡塔尼亚综合诊所使用 Apple Vision Pro 商业查看器进行了眼部整形手术……

3 2024五月

儿童涂色页的好处 - 适合所有年龄段的魔法世界

通过着色培养精细运动技能可以帮助孩子们为写作等更复杂的技能做好准备。填色…

2 2024五月

未来已来:航运业如何彻底改变全球经济

海军部门是真正的全球经济力量,已迈向 150 亿美元的市场……

1 2024五月

出版商和 OpenAI 签署协议以规范人工智能处理的信息流

上周一,英国《金融时报》宣布与 OpenAI 达成协议。英国《金融时报》授予其世界级新闻报道许可……

四月30 2024