简体   繁体   中英

Subscript out of range when copying worksheet

I would like to create a command button which imports the selected file into the current workbook.
I'm getting Subscript out of range error and I cannot find out the solution.
Here is what I have:

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

Error :

The error occurs on the line
Workbooks(file).Worksheets(ActiveSheet.Name).Copy... because the Workbooks(<argument>) is expecting just the name of the file, without full path. You are parsing a full path.

Fixed code

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  

Notes

The main change is the Dim strFileName As String and strFileName = Dir(strFilePath) and it's usage in the code after opening the new book. I changed the variables names for test purposes, I can read it more easily this way. You can use rename tool to revert the changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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