使用宏从另一个工作簿复制某些数据,然后使用宏将其粘贴到工作簿中的工作表上。我正在使用这个函数,我从另一个线程改编,以获取外部工作簿的文件路径:
Public Function RetrieveFileName() As String
'Show the open dialog and pass the selected file name to the String variable "sFileName"
RetrieveFileName = Application.GetOpenFilename
'They have cancelled.
If RetrieveFileName = "False" Then
Exit Function
End If
End Function
它可以工作,我可以打开工作簿,但是当我尝试存储外部工作簿中的范围(下面的代码段中的最后一行)时,vba会抛出运行时错误“13”:输入不匹配。
Sub CreateSystemExtracts()
Dim thisRow As Long, wbkCheck As Long
Dim countryFilter As String, systemFilter As String
Dim sourceBook As Workbook
Dim sourceHeaders As Range
'Ask user where source data sits
Set sourceBook = Workbooks.Open(RetrieveFileName())
'Make sure they provided a source
If sourceBook Is Nothing Then
MsgBox "You must select a source file to continue, macro halted."
Exit Sub
End If
'Check if number of rows in the selected file matches what we expect
sourceHeaders = Workbooks(sourceBook).Worksheets(Sheet1).Range("A2:X2")
你能告诉我哪里出错吗?我认为它与我在最后一行声明范围的方式有关,但我尝试了一些不同的方法,并且都给出了运行时错误13。
提前致谢!
答案 0 :(得分:0)
分配对象变量时,需要使用Set
'Check if number of rows in the selected file matches what we expect
Set sourceHeaders = Workbooks(sourceBook).Worksheets("Sheet1").Range("A2:X2")