|
楼主 |
发表于 2011-4-15 15:31
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
原帖由 gohrfang 于 2011-4-15 14:50 发表
弱弱的问一下,2007的怎么使用这个做?
1楼已经上传,第3个附件
2007(不兼容2003):
Sub 历遍本文件夹()
Dim cnn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim cat As New ADOX.Catalog, tb1 As Table
Dim SQL$, MyFile$, m%, i%, temp$, strField$, s$, n%
Mypath = ThisWorkbook.Path & "\"
MyFile = Dir(Mypath & "*.xlsx")
Do While MyFile <> ""
If MyFile <> ThisWorkbook.Name Then
n = n + 1
If n = 1 Then cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & Mypath & MyFile '连接第一个工作簿
cat.ActiveConnection = "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=No';Data Source=" & Mypath & MyFile '连接工作簿以利用ADOX取得工作表名
For Each tb1 In cat.Tables
If tb1.Type = "TABLE" Then
s = Replace(tb1.Name, "'", "") '表名含有“1月”等时有多余的单引号
If Right(s, 1) = "$" Then '排除无效表名
If n > 1 Then SQL = "select * from [Excel 12.0;Database=" & Mypath & MyFile & "].[" & s & "]" Else SQL = "[" & s & "]"
Set rs = cnn.Execute(SQL)
If rs.Fields(0).Name <> "F1" Then '第一列没有字段名就认为是空表
dic(rs.Fields.Count) = "" '各表字段数不一致,dic.Count将大于1
m = m + 1
strField = ""
For i = 0 To rs.Fields.Count - 1 '历遍每个工作表的每个字段(判断列数不等的依据)
temp = rs.Fields(i).Name
If Left(temp, 1) <> "F" And IsNumeric(Mid(temp, 2)) = False Then '排除其他可能的空字段
If Not d.Exists(temp) Then d(temp) = "" '字段名写入字典
strField = strField & temp & "," '字段名用逗号连接
End If
Next
ds(MyFile & s) = strField & "," '工作簿名与工作表名连接添加到字典ds键值,字段名连接字符串添加到字典条目
UserForm1.ListView1.ListItems.Add , , MyFile 'ListView控件第一列添加工作簿名
' ds(Replace(MyFile, ".xlsx", "") & s) = strField & "," '工作簿名与工作表名连接添加到字典ds键值,字段名连接字符串添加到字典条目
' UserForm1.ListView1.ListItems.Add , , Replace(MyFile, ".xlsx", "") 'ListView控件第一列添加工作簿名
UserForm1.ListView1.ListItems(m).SubItems(1) = s 'ListView控件第二列添加工作表名
End If
End If
End If
Next
End If
MyFile = Dir()
Loop
If n = 0 Then
MsgBox "没有发现可以汇总的文件!", vbInformation, "提示"
Exit Sub
End If
cnn.Close
Set rs = Nothing
Set cnn = Nothing
Set cat = Nothing
Set tb1 = Nothing
UserForm1.Show
End Sub
2007(兼容2003):
Sub 历遍本文件夹()
Dim cnn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim cat As New ADOX.Catalog, tb1 As Table
Dim SQL$, MyFile$, m%, i%, temp$, strField$, s$, n%
Mypath = ThisWorkbook.Path & "\"
MyFile = Dir(Mypath & "*.xls*") '20032007混用,但是不汇总xlsm文件
Do While MyFile <> ""
If Right(MyFile, 4) <> "xlsm" Then
' If MyFile <> ThisWorkbook.Name Then
n = n + 1
If n = 1 Then cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & Mypath & MyFile '连接第一个工作簿
cat.ActiveConnection = "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=No';Data Source=" & Mypath & MyFile '连接工作簿以利用ADOX取得工作表名
For Each tb1 In cat.Tables
If tb1.Type = "TABLE" Then
s = Replace(tb1.Name, "'", "") '表名含有“1月”等时有多余的单引号
If Right(s, 1) = "$" Then '排除无效表名
If n > 1 Then SQL = "select * from [Excel 12.0;Database=" & Mypath & MyFile & "].[" & s & "]" Else SQL = "[" & s & "]"
Set rs = cnn.Execute(SQL)
If rs.Fields(0).Name <> "F1" Then '第一列没有字段名就认为是空表
dic(rs.Fields.Count) = "" '各表字段数不一致,dic.Count将大于1
m = m + 1
strField = ""
For i = 0 To rs.Fields.Count - 1 '历遍每个工作表的每个字段(判断列数不等的依据)
temp = rs.Fields(i).Name
If Left(temp, 1) <> "F" And IsNumeric(Mid(temp, 2)) = False Then '排除其他可能的空字段
If Not d.Exists(temp) Then d(temp) = "" '字段名写入字典
strField = strField & temp & "," '字段名用逗号连接
End If
Next
ds(MyFile & s) = strField & "," '工作簿名与工作表名连接添加到字典ds键值,字段名连接字符串添加到字典条目
UserForm1.ListView1.ListItems.Add , , MyFile 'ListView控件第一列添加工作簿名
' ds(Replace(MyFile, ".xls*", "") & s) = strField & "," '工作簿名与工作表名连接添加到字典ds键值,字段名连接字符串添加到字典条目
' UserForm1.ListView1.ListItems.Add , , Replace(MyFile, ".xls*", "") 'ListView控件第一列添加工作簿名
UserForm1.ListView1.ListItems(m).SubItems(1) = s 'ListView控件第二列添加工作表名
End If
End If
End If
Next
End If
MyFile = Dir()
Loop
If n = 0 Then
MsgBox "没有发现可以汇总的文件!", vbInformation, "提示"
Exit Sub
End If
cnn.Close
Set rs = Nothing
Set cnn = Nothing
Set cat = Nothing
Set tb1 = Nothing
UserForm1.Show
End Sub
[ 本帖最后由 zhaogang1960 于 2011-4-19 09:32 编辑 ] |
评分
-
1
查看全部评分
-
|