|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
yuk_yu 发表于 2011-8-16 16:32
版主,可以给个实例吗?谢谢
第一种方法其实就是打开工作簿获取工作表数
下面附件是第二种方法:
Sub 获取工作表名称() '引用Microsoft AD0 Ext 2.8 for DDL and Security
Dim cat As New ADOX.Catalog, i As Integer
Dim tb1 As Table, str1 As String, str2 As String
str1 = Application.GetOpenFilename(filefilter:="Excel文件(*.xls),*.xls", Title:="选择Excel工作簿")
If str1 = "False" Then Exit Sub
str2 = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=No';"
str2 = str2 & "Data Source=" & str1
On Error Resume Next
cat.ActiveConnection = str2
i = 1
With ActiveSheet
.Cells(i, 1) = "指定工作簿的工作表名称:"
i = i + 1
For Each tb1 In cat.Tables
If tb1.Type = "TABLE" Then
.Cells(i, 1) = Replace(tb1.Name, "$", "")
i = i + 1
End If
Next
End With
Set cat = Nothing
Set tb1 = Nothing
End Sub
Sub 获取工作表名称不引用()
Dim cat, tb1, i&, s$, Filename$
Filename = Application.GetOpenFilename(filefilter:="Excel文件(*.xls),*.xls", Title:="选择Excel工作簿")
If Filename = "False" Then Exit Sub
Set cat = CreateObject("ADOX.Catalog")
On Error Resume Next
cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=No';Data Source=" & Filename
i = 1
Cells(i, 1) = "指定工作簿的工作表名称:"
i = i + 1
For Each tb1 In cat.Tables
If tb1.Type = "TABLE" Then
s = Replace(tb1.Name, "'", "")
If Right(s, 1) = "$" Then
Cells(i, 1) = Replace(s, "$", "")
i = i + 1
End If
End If
Next
Set cat = Nothing
Set tb1 = Nothing
End Sub
ADOX不打开工作簿获取工作表名(含引用和不引用两种方式).rar
(10.68 KB, 下载次数: 15)
|
|