我正在设置一个宏以打开目录中的所有文件并将每个选项卡中的某个选项卡复制到一个组合文件中(将它们合并到一个工作簿中)。我有两个问题。首先,文件受密码保护 - 所以当我使用这一行时,它会打开文件。
Set gwkbInputdata = Workbooks.Open(gRnCT_File_Loc & gsInputFileName)
但是,当它受密码保护时,它会失败。所以我在最后添加了以下内容,但它仍然失败。
Set gwkbInputdata = Workbooks.Open(gRnCT_File_Loc & gsInputFileName),Password = "openfile"
第二个问题 - 当我复制标签(工作表)时,我想用我从中获取它们的文件名来命名它们。但是文件名太长了 - 所以我想把名字放到第一个空格(例如“测试文件 5 月 13 日”= 工作表名称“测试”)。我如何让这段代码工作。
非常感谢任何帮助。
完整代码如下:
* gRnCT_File_Loc = 目录位置。* gsInputFileName = 文件名。
Code to date:
Sub Pulldata()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set gRwksconfigeration = Sheets(gcsConfigSheetName)
gRnCT_File_Loc = gRwksconfigeration.Range(CT_File_Loc)
gRnCT_Tab_Search = gRwksconfigeration.Range(CT_Tab_Search)
gsInputFileName = Dir(gRnCT_File_Loc)
Set gwkscurrent = ActiveWorkbook
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> gcsConfigSheetName Then ws.Delete
Next ws
Do
On Error GoTo err:
Set gwkbInputdata = Workbooks.Open(gRnCT_File_Loc & gsInputFileName)
On Error GoTo 0
On Error GoTo err1:
With Sheets(gRnCT_Tab_Search)
On Error GoTo 0
End With
gsInputFileName = Dir
Loop Until gsInputFileName = vbNullString
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Exit Sub
err:
MsgBox ("No files or files in incorrect format in " & gRnCT_File_Loc)
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Exit Sub
err1:
MsgBox ("Sheet " & gRnCT_Tab_Search & " doesn't exist in file " & gsInputFileName)
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Resume Next
End Sub