簡體   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