繁体   English   中英

复制工作表时下标超出范围

[英]Subscript out of range when copying worksheet

我想创建一个命令按钮,将所选文件导入到当前工作簿中。
我收到下Subscript out of range error ,无法找到解决方案。
这是我所拥有的:

Private Sub CmdBrowseFile_Click()
Dim intChoice, total As Integer
Dim file, ControlFile As String

ControlFile = ActiveWorkbook.Name
'only allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'Remove all other filters
Call Application.FileDialog(msoFileDialogOpen).Filters.Clear
'Add a custom filter
Call Application.FileDialog(msoFileDialogOpen).Filters.Add( _
    "Text Files Only", "*.xlsx")
'make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
If intChoice <> 0 Then
    'get the file path selected by the user
    file = Application.FileDialog( _
        msoFileDialogOpen).SelectedItems(1)
    'open file
    Workbooks.Open fileName:=file
    total = Workbooks(ControlFile).Worksheets.Count
    Workbooks(file).Worksheets(ActiveSheet.Name).Copy _
    after:=Workbooks(ControlFile).Worksheets(total)
    Windows(file).Activate
    ActiveWorkbook.Close SaveChanges:=False
    Windows(ControlFile).Activate
End If

错误:

错误发生在网上
Workbooks(file).Worksheets(ActiveSheet.Name).Copy...因为Workbooks(<argument>)仅期望文件名,而没有完整路径。 您正在解析完整路径。

固定码

Private Sub CmdBrowseFile_Click()

    Dim intChoice As Integer, total As Integer 'note the correct declaring, for each variable As Type
    Dim strFilePath As String, strControlFile As String
    Dim strFileName As String

    strControlFile = ActiveWorkbook.Name
    'only allow the user to select one strFilePath
    Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
    'Remove all other filters
    Call Application.FileDialog(msoFileDialogOpen).Filters.Clear
    'Add a custom filter
    Call Application.FileDialog(msoFileDialogOpen).Filters.Add( _
        "Text Files Only", "*.xlsx")
    'make the strFilePath dialog visible to the user
    intChoice = Application.FileDialog(msoFileDialogOpen).Show
    'determine what choice the user made
    If intChoice <> 0 Then
        'get the strFilePath path selected by the user
        strFilePath = Application.FileDialog( _
            msoFileDialogOpen).SelectedItems(1)
        'get the file name
        strFileName = Dir(strFilePath)
        'open strFilePath
        Workbooks.Open fileName:=strFilePath
        total = Workbooks(strControlFile).Worksheets.Count
        Workbooks(strFileName).Worksheets(ActiveSheet.Name).Copy _
        after:=Workbooks(strControlFile).Worksheets(total)
        Windows(strFileName).Activate
        ActiveWorkbook.Close SaveChanges:=False
        Windows(strControlFile).Activate
    End If

End Sub  

笔记

主要更改是Dim strFileName As StringstrFileName = Dir(strFilePath)并且在打开新书后在代码中使用了它。 我出于测试目的更改了变量名称,这样我可以更轻松地读取它。 您可以使用重命名工具来还原更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM