【发布时间】:2017-04-15 10:34:45
【问题描述】:
要检查是否有与文件同名的工作表。
目前我让 filnam 将文件作为数组打开,但希望它循环浏览代码并查看是否有同名的工作表。
我已使用拆分来删除路径名和扩展名,但无法对其进行检查。
对于代码的混乱,我深表歉意。一直在努力整理,然后我会整理一下。还有更多代码,但这不是必需的,因为如果没有匹配项,我希望它运行该代码。
你能帮忙吗?
Sub sort_it_out()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim Sheet As Worksheet
Dim filnam As Variant
On Error GoTo errorhandler
Set wb1 = ActiveWorkbook
ChDir Application.ActiveWorkbook.path
'get files
filnam = Application.GetOpenFilename(FileFilter:="2D Table Formats (*.htm;*.xlsm;*.html),*.htm;*.xlsm;*.html", Title:="Select 2D Table", MultiSelect:=True)
'set the array
If IsArray(filnam) Then 'if at least one file is selected, this will be an Array
'define j as the array
For j = LBound(filnam) To UBound(filnam)
'remove path and extension
Dim s As String, a() As String, p As String
s = filnam(j)
a() = Split(s, "\")
p = Split(a(UBound(a)), ".")(0)
MsgBox "p " & p
'check if worksheet exists
For Each ws_check In ThisWorkbook.Worksheets()
If ws_check.Name = p Then
MsgBox "Its there"
Exit Sub
Else
End If
Next ws_check
'continue code from here
然后运行代码......但由于某种原因它没有循环数组。一次只有一个文件。你能帮忙吗?
【问题讨论】: