1

我正在设置一个宏以打开目录中的所有文件并将每个选项卡中的某个选项卡复制到一个组合文件中(将它们合并到一个工作簿中)。我有两个问题。首先,文件受密码保护 - 所以当我使用这一行时,它会打开文件。

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
4

2 回答 2

3

对于关于获取文件名第一部分的第二个问题,请尝试:

if(instr(gsInputFileName," ") > 0
   then short_file_name=left(gsInputFileName,instr(gsInputFileName," ")-1)
   else short_file_name=gsInputFileName
   endif
于 2013-05-02T14:44:43.063 回答
2

您缺少:密码后

Set gwkbInputdata = Workbooks.Open(gRnCT_File_Loc & gsInputFileName),Password = "openfile"

应该:

Set gwkbInputdata = Workbooks.Open(gRnCT_File_Loc & gsInputFileName,Password:="openfile")

您可以使用split空格作为分隔符来解决第二个问题,例如

Split(str, " ")(0)

文件的原始名称在哪里str,例如“Test file May 13”

于 2013-05-02T14:40:48.513 回答