在VBA中,如何编写合并工作表的代码?

2025-09-29 04:00:08

1、实例【通过运行VBA代码将1至3月份工作表中的内容复制到“年度”工作表中?】

在VBA中,如何编写合并工作表的代码?

2、在工作表中,点击菜单栏【开发工具】,在其工具栏内,点击【visual basic】!

在VBA中,如何编写合并工作表的代码?

3、会进入VBA编辑界面!点击菜单栏【插入】,在其下拉菜单中,选择【模块】!

在VBA中,如何编写合并工作表的代码?

4、会弹出模块编辑窗口!

在VBA中,如何编写合并工作表的代码?

5、在代码编辑窗口输入以下代码:

Sub 合并工作表()

Dim i As Integer, j%, k%, mth As Worksheet, yer As Worksheet

Set yer = Sheets("年度")

For i = 1 To 3

  Set mth = Sheets(i & "月份")

  j = mth.UsedRange.Rows.Count

  m = mth.Cells(2, mth.Columns.Count).End(xlToLeft).Column

  k = yer.Cells(2, 1).CurrentRegion.Rows.Count

  mth.Range(mth.[a3], mth.Cells(j, m)).Copy

  yer.Paste Cells(k + 1, 2)

  yer.Cells(k + 1, 1).Resize(j - 2) = i & "月"

Next

End Sub

在VBA中,如何编写合并工作表的代码?

6、在工作表的【开发工具】菜单下,点击【插入】按钮,插入一【表单】按钮,并与编写的【合并工作表】代码关联,且进入命名为【合并工作表】!

在VBA中,如何编写合并工作表的代码?

7、点击【合并工作表】按钮!1至3月份工作表中的内容瞬间被复制到“年度”工作表中!

在VBA中,如何编写合并工作表的代码?

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢