【问题标题】:Type Mismatch - Copy range from one workbook to another类型不匹配 - 将范围从一个工作簿复制到另一个工作簿
【发布时间】:2018-03-08 05:51:18
【问题描述】:

我正在尝试使用以下代码将范围从一个工作簿复制到另一个工作簿。此处和其他地方与此问题类似的其他帖子似乎仅限于与我的特定情况(我的代码的最后一行)无关的特定语法错误(据我所知)。对于通常尝试在工作簿之间复制和粘贴给定范围(硬编码)的任何人,这可能是相关的:

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) 直接替换为.Range("A9:A159").Range("B9:B159"),但得到相同的错误。

我尝试的另一个方法是:

Workbooks(wb1).Worksheets(ws1).Range(rng1).Copy 
Destination:=Workbooks(wb0).Worksheets(ws0).Range(rng0)

但这给了我同样的错误。

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

【问题讨论】:

  • 试试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':对象不支持此属性或方法”。通过快速研究,我认为这与不能以这种方式适用的范围对象有关 - 但是我真的不知道我在这里说的是什么。即便如此,这确实让我没有比我最初发布时更好。我接受了您的第二个建议:使用实际范围(我在原始帖子“A9:A159”中提到的那些),而不是范围变量,但这也没有什么区别......
  • @BruceWayne,关于您的“也...”第二个帖子,我也这样做了 - 这似乎很有意义,但我收到另一个错误:“运行时错误 244:需要对象" 在这个新行上......如果范围对象分配了工作簿,不确定为什么会引发此错误。我将假设设置 range = wb.ws.range(abc123) 不适用于此目的?

标签: excel vba range copy-paste type-mismatch


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多