Excel 类型不匹配-从一个工作簿复制到另一个工作簿的范围

Excel 类型不匹配-从一个工作簿复制到另一个工作簿的范围,excel,vba,range,copy-paste,type-mismatch,Excel,Vba,Range,Copy Paste,Type Mismatch,我正在尝试使用下面的代码将一个范围从一个工作簿复制到另一个工作簿。在这里和其他地方,与本期类似的其他帖子似乎局限于特定的语法错误,而这些错误(据我所知)与我的特定案例(代码的最后一行)无关。对于通常试图在工作簿之间复制和粘贴给定范围(硬编码)的任何人,这可能与以下内容有关: Sub ImportT12Accounts() ' ' ImportT12Accounts Macro ' Pulls in the list of account numbers from a report of the

我正在尝试使用下面的代码将一个范围从一个工作簿复制到另一个工作簿。在这里和其他地方,与本期类似的其他帖子似乎局限于特定的语法错误,而这些错误(据我所知)与我的特定案例(代码的最后一行)无关。对于通常试图在工作簿之间复制和粘贴给定范围(硬编码)的任何人,这可能与以下内容有关:

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
但这给了我同样的错误


我感觉不匹配是由工作簿或工作表变量之一引起的,但是,我不明白为什么会出现这种情况。据我所知,可以将工作簿、工作表和范围变量传递到各自的方法中。

这似乎是对对象的误解。发生此错误的原因是将对象传递到字符串字段,这会导致“类型不匹配”。可以直接调用这些对象,并且它们完全符合声明的条件。你不需要像那样堆叠它们

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()
'
'导入12帐户宏
'从用户选择的报告中提取帐号列表。
'
'
将文件选项设置为整数
将文件路径设置为字符串
作为变体的名称
将ws0调整为工作表“此工作簿的第二个选项卡”
将ws1调整为工作表“打开工作簿的第二个选项卡”
“将wb0设置为工作簿”此工作簿(日志)
将wb1标注为工作簿“打开的T12工作表”
Dim rng0 As Range'此工作簿第二张工作表中要复制到的单元格范围
Dim rng1 As Range“打开的工作簿中要从中复制的单元格范围”
'设置wb0=ActiveWorkbook
设置ws0=ActiveSheet
设置rng0=ws0.范围(“B9:B159”)
'查找所需的T12工作簿文件路径
'仅允许用户选择一个文件
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect=False
'使文件对话框对用户可见
fileChoice=Application.FileDialog(msoFileDialogOpen).Show
'确定用户所做的选择
如果文件选项为0,则
'获取用户选择的文件路径
filePath=Application.FileDialog(msoFileDialogOpen)。选择editems(1)
如果结束
'使用新打开的工作簿设置变量
设置wb1=工作簿。打开(文件路径)
设置ws1=ActiveSheet
设置rng1=ws1.范围(“A9:A159”)
'使用用户在公式中选择的文件路径将帐号拉入本手册,如第2页所示
rng1.Value=rng0.Value
端接头

只需尝试
ws0.rng1.Value=ws1.rng0.Value
。我认为问题在于如何设置范围,然后调用它们。按照这种方式,变量应该是字符串,但实际上您使用的是
Range
变量,因此您可以直接使用它们。另外,如果您使用了
Set wb0=ActiveWorkbook//Set ws0=wb0.Sheets(“SheetX”)
,那么我假设您想要执行
Set rng0=ws0.Range(“B9:B159”)
(对
ws1/rng1
)也可以这样做。然后您可以只做
rng1.Value=rng2.Value
查找带有F5、特殊、错误的任何工作表错误。@BruceWayne,我注释掉了旧行,并将建议的一行放进去。现在我有一个错误:“运行时错误'438':对象不支持此属性或方法”在这一点上。通过快速研究,我认为这与range对象不适用于这种方式有关-但是我真的不知道我在这里说的是什么。即使如此,这也不会让我比最初发布时更好。我接受了你的第二个建议:设置实际范围(我在最初的帖子中提到的范围)“A9:A159”),而不是范围变量,但这也没有什么区别…@BruceWayne,关于你的“还…”第二篇文章,我也这么做了-这似乎很有意义,但我得到了另一个错误:“运行时错误244:需要对象”“在这一新行中…不确定如果范围对象分配了工作簿,它为什么会抛出此错误。我将假设设置range=wb.ws.range(abc123)不适用于此目的?
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