[英]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. 我收到下Subscript out of range error
,无法找到解决方案。
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
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. Workbooks(file).Worksheets(ActiveSheet.Name).Copy...
因为Workbooks(<argument>)
仅期望文件名,而没有完整路径。 You are parsing a full path. 您正在解析完整路径。
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
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. 主要更改是Dim strFileName As String
和strFileName = Dir(strFilePath)
并且在打开新书后在代码中使用了它。 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. 您可以使用重命名工具来还原更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.