首页  >  文章  >  电脑教程  >  使用VBA合并所有工作表

使用VBA合并所有工作表

王林
王林转载
2024-01-16 21:12:051028浏览

使用VBA合并所有工作表

vba合并全部表

Sub ADO 联合查询()

Dim cnn 作为对象,SQL$,MyPath$,MyFile$,m&,n&

Set cnn = CreateObject(“ADODB.Connection”)

[a:b].清除内容

MyPath = ThisWorkbook.Path & ””

MyFile = Dir(MyPath & ”*.xls”)

当我的文件“”时做

如果 MyFile ThisWorkbook.Name 那么

n = n + 1

如果 n = 1 则 cnn.Open ”Provider=Microsoft.Jet.OLEDB.4.0;Extended;Data Source=” & MyPath & MyFile

m = m + 1

如果 m >49 那么

Range(“a” & Rows.Count).End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)

m = 1

SQL=“”

结束如果

如果 Len(SQL) 那么 SQL = SQL & 「并集所有」

SQL = SQL & ”select f1,'” & Replace(MyFile, ”.xls”, ””) & ”' from [Excel 8.0;hdr=no;Database=” & MyPath & MyFile & ”].[Sheet1$ A2:A]”

结束如果

MyFile = Dir()

循环

If Len(SQL) then Range(“a” & Rows.Count).End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)

cnn.关闭

设置 cnn = Nothing

结束子

用vba插件填充单元格

私有子CommandButton1_Click()

'最后一行,上面插入一行,横向单元格n格,填充数据

Dim n、s、col、r、tmp

'----设置----

n = 2 '横向歌手单元格n格

s = ”XXXXXX” '正在填充数据

col = ”A” ' 以 col 操作的最左列

'----执行----

使用 ActiveSheet

r = .Cells(65536, col).End(xlUp).Row

.Rows(r).Insert xlShiftDown '插入行

使用 .Cells(r, col).Resize(1, n)

.合并'合并单元格

.Value = s '填充数据

结束于

结束于

结束子

以上是使用VBA合并所有工作表的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:docexcel.net。如有侵权,请联系admin@php.cn删除