类型不匹配 - 将范围从一个工作簿复制到另一个
Type Mismatch - Copy range from one workbook to another
我正在尝试使用下面的代码将范围从一个工作簿复制到另一个工作簿。此处和其他地方与此问题类似的其他帖子似乎仅限于特定的语法错误,这些错误与我的特定案例(我的代码的最后一行)无关(据我所知)。对于通常尝试在工作簿之间复制和粘贴给定范围(硬编码)的任何人,这可能是相关的:
Sub ImportT12Accounts()
'
' ImportT12Accounts Macro
' Pulls in the list of account numbers from a report of the user's choice.
'
'
Dim fileChoice As Integer
Dim filePath As String
Dim sheetName As Variant
Dim ws0 As Worksheet 'this workbook's 2nd tab
Dim ws1 As Worksheet 'the opened workbook's 2nd tab
Dim wb0 As Workbook 'this workbook (the log)
Dim wb1 As Workbook 'the opened T12 sheet
Dim rng0 As Range 'the range of cells in this workbook's 2nd sheet to be copied to
Dim rng1 As Range 'the range of cells from the openeed workbook to be copied from
Set ws0 = ActiveSheet
Set wb0 = ActiveWorkbook
Set rng0 = Range("B9:B159")
'Find the desired T12 workbook filepath
'only allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'make the file dialog visible to the user
fileChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
If fileChoice <> 0 Then
'get the file path selected by the user
filePath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
End If
'Set variables using the newly-opened workbook
Set wb1 = Workbooks.Open(filePath)
Set ws1 = ActiveSheet
Set rng1 = Range("A9:A159")
'Use the filepath selected by User in formulas to pull the account numbers into this book, in Sheet 2
Workbooks(wb0).Worksheets(ws0).Range(rng1).Value = _
Workbooks(wb1).Worksheets(ws1).Range(rng0).Value
End Sub
当运行时,它在最后一行抛出"Run-time error '13': Type mismatch"
错误,"Workbooks(wb0)...Range(rng0).Value"
。
我试过将这种复制粘贴方法替换为其他几个方法,但无济于事。例如,我尝试直接替换范围变量 .Range(rng0)
和 .Range(rng1)
with/for .Range("A9:A159")
和 .Range("B9:B159")
,但得到相同的错误。
我尝试的另一个方法示例是:
Workbooks(wb1).Worksheets(ws1).Range(rng1).Copy
Destination:=Workbooks(wb0).Worksheets(ws0).Range(rng0)
但这给了我同样的错误。
我感觉不匹配是由工作簿或工作表变量之一引起的,但是,我不明白为什么会这样。据我所知,可以将工作簿、工作表和范围变量传递到它们各自的方法中。
这好像是对对象的误解。发生该错误是因为您将对象传递到导致 "type mismatch" 的字符串字段。这些对象可以直接调用,并且它们完全符合声明的要求。你不需要像那样堆叠它们。
Sub ImportT12Accounts()
'
' ImportT12Accounts Macro
' Pulls in the list of account numbers from a report of the user's choice.
'
'
Dim fileChoice As Integer
Dim filePath As String
Dim sheetName As Variant
Dim ws0 As Worksheet 'this workbook's 2nd tab
Dim ws1 As Worksheet 'the opened workbook's 2nd tab
'Dim wb0 As Workbook 'this workbook (the log)
Dim wb1 As Workbook 'the opened T12 sheet
Dim rng0 As Range 'the range of cells in this workbook's 2nd sheet to be copied to
Dim rng1 As Range 'the range of cells from the openeed workbook to be copied from
'Set wb0 = ActiveWorkbook
Set ws0 = ActiveSheet
Set rng0 = ws0.Range("B9:B159")
'Find the desired T12 workbook filepath
'only allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'make the file dialog visible to the user
fileChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
If fileChoice <> 0 Then
'get the file path selected by the user
filePath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
End If
'Set variables using the newly-opened workbook
Set wb1 = Workbooks.Open(filePath)
Set ws1 = ActiveSheet
Set rng1 = ws1.Range("A9:A159")
'Use the filepath selected by User in formulas to pull the account numbers into this book, in Sheet 2
rng1.Value = rng0.Value
End Sub
我正在尝试使用下面的代码将范围从一个工作簿复制到另一个工作簿。此处和其他地方与此问题类似的其他帖子似乎仅限于特定的语法错误,这些错误与我的特定案例(我的代码的最后一行)无关(据我所知)。对于通常尝试在工作簿之间复制和粘贴给定范围(硬编码)的任何人,这可能是相关的:
Sub ImportT12Accounts()
'
' ImportT12Accounts Macro
' Pulls in the list of account numbers from a report of the user's choice.
'
'
Dim fileChoice As Integer
Dim filePath As String
Dim sheetName As Variant
Dim ws0 As Worksheet 'this workbook's 2nd tab
Dim ws1 As Worksheet 'the opened workbook's 2nd tab
Dim wb0 As Workbook 'this workbook (the log)
Dim wb1 As Workbook 'the opened T12 sheet
Dim rng0 As Range 'the range of cells in this workbook's 2nd sheet to be copied to
Dim rng1 As Range 'the range of cells from the openeed workbook to be copied from
Set ws0 = ActiveSheet
Set wb0 = ActiveWorkbook
Set rng0 = Range("B9:B159")
'Find the desired T12 workbook filepath
'only allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'make the file dialog visible to the user
fileChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
If fileChoice <> 0 Then
'get the file path selected by the user
filePath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
End If
'Set variables using the newly-opened workbook
Set wb1 = Workbooks.Open(filePath)
Set ws1 = ActiveSheet
Set rng1 = Range("A9:A159")
'Use the filepath selected by User in formulas to pull the account numbers into this book, in Sheet 2
Workbooks(wb0).Worksheets(ws0).Range(rng1).Value = _
Workbooks(wb1).Worksheets(ws1).Range(rng0).Value
End Sub
当运行时,它在最后一行抛出"Run-time error '13': Type mismatch"
错误,"Workbooks(wb0)...Range(rng0).Value"
。
我试过将这种复制粘贴方法替换为其他几个方法,但无济于事。例如,我尝试直接替换范围变量 .Range(rng0)
和 .Range(rng1)
with/for .Range("A9:A159")
和 .Range("B9:B159")
,但得到相同的错误。
我尝试的另一个方法示例是:
Workbooks(wb1).Worksheets(ws1).Range(rng1).Copy
Destination:=Workbooks(wb0).Worksheets(ws0).Range(rng0)
但这给了我同样的错误。
我感觉不匹配是由工作簿或工作表变量之一引起的,但是,我不明白为什么会这样。据我所知,可以将工作簿、工作表和范围变量传递到它们各自的方法中。
这好像是对对象的误解。发生该错误是因为您将对象传递到导致 "type mismatch" 的字符串字段。这些对象可以直接调用,并且它们完全符合声明的要求。你不需要像那样堆叠它们。
Sub ImportT12Accounts()
'
' ImportT12Accounts Macro
' Pulls in the list of account numbers from a report of the user's choice.
'
'
Dim fileChoice As Integer
Dim filePath As String
Dim sheetName As Variant
Dim ws0 As Worksheet 'this workbook's 2nd tab
Dim ws1 As Worksheet 'the opened workbook's 2nd tab
'Dim wb0 As Workbook 'this workbook (the log)
Dim wb1 As Workbook 'the opened T12 sheet
Dim rng0 As Range 'the range of cells in this workbook's 2nd sheet to be copied to
Dim rng1 As Range 'the range of cells from the openeed workbook to be copied from
'Set wb0 = ActiveWorkbook
Set ws0 = ActiveSheet
Set rng0 = ws0.Range("B9:B159")
'Find the desired T12 workbook filepath
'only allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'make the file dialog visible to the user
fileChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
If fileChoice <> 0 Then
'get the file path selected by the user
filePath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
End If
'Set variables using the newly-opened workbook
Set wb1 = Workbooks.Open(filePath)
Set ws1 = ActiveSheet
Set rng1 = ws1.Range("A9:A159")
'Use the filepath selected by User in formulas to pull the account numbers into this book, in Sheet 2
rng1.Value = rng0.Value
End Sub