excel - 复制参数不适用于 Range 对象

标签 excel vba

我正在尝试复制表格并将其粘贴到工作表上的其他位置(用户选择的目标单元格):

enter image description here

但我在 table.copy 上收到错误消息部分:

Wrong number of arguments or invalid property assignment


   Sub PasteMacro()
     Dim target As Variant
     Dim i As Long
     Dim table As Range
     Dim weight As Double
     Dim thickness As Double
     Dim Stiff As Double

     Set shA = ActiveWorkbook.Worksheets("User sheet comp data")

     ' searh for the first non 0 ligne in the table

     i = 19
     Do Until Range("M" & i) <> 0
     i = i - 1
     Loop

     MsgBox Cells(i, 13).Address

     'the table to copy
     Set table = shA.Range(Cells(1, 2), Cells(i, 15))

     weight = shA.Range("O20")
     thickness = shA.Range("M20")
     Stiff = shA.Range("M21")


     On Error Resume Next
    ' ask the user to select the cell to paste table
    Set target = Application.InputBox(Prompt:="Please select a destination Cell where you want to 
    paste", Type:=8)

    On Error GoTo 0

    If Not answ Is Nothing Then
    'copy the table and paste it
    table.Copy , shA.target
    End If

  End Sub

最佳答案

table.Copy , shA.target

应该
:
table.Copy target

自从:
  • Destination参数是 Copy() 中的第一个方法,而需要逗号来分隔后续(如果有)参数
  • targetRange对象,因此它已经完全符合其父工作表和工作簿对象

  • 此外,我猜
    If Not answ Is Nothing Then
    

    应该
    :
    If Not Target Is Nothing Then
    

    所以这是修改后的代码
    Sub PasteMacro()
    
        Dim target As Range
        Dim table As Range
        Dim i As Long
        Dim weight As Double
        Dim thickness As Double
        Dim Stiff As Double
    
        Dim shA As Worksheet
    
        With ActiveWorkbook.Worksheets("User sheet comp data")
    
            ' search for the first non 0 ligne in the table
            i = 19
            Do Until .Range("M" & i) <> 0
                i = i - 1
            Loop
    
            MsgBox .Cells(i, 13).Address
    
            'the table to copy
            Set table = .Range(Cells(1, 2), Cells(i, 15))
    
            weight = .Range("O20").Value2
            thickness = .Range("M20").Value2
            Stiff = .Range("M21").Value2
    
    
            On Error Resume Next
            Set target = Application.InputBox(Prompt:="Please select a destination Cell where you want to Paste ", Type:=8)
            On Error GoTo 0
    
            If Not target Is Nothing Then table.Copy target  'copy the table and paste it
    
        End With
    
    End Sub
    

    如您所见,所有范围对象(范围、单元格)都在引用 ActiveWorkbook.Worksheets("User sheet comp data") ("MilestoneDueDate")通过他们前面的那个点 (.)

    这样你就可以起诉你不会失去对在哪个工作表中考虑的范围的控制

    关于excel - 复制参数不适用于 Range 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61118150/

    相关文章:

    python - 如何在 Python 中从 My XLS 文件中读取特定工作表

    excel - Microsoft Office 互操作程序集引用

    vba - 如何调暗 getElementsBy* 结果

    regex - 从列中提取模式

    excel - 如何使用 VBA 将一张纸上的 3 个单元格组合成另一张纸上的 1 个单元格?

    excel - 将 CSV 文件导入 Excel

    excel - 无法将类型 'System.__ComObject' 的 COM 对象转换为接口(interface)类型 'Excel._Workbook'

    Excel 公式结果不是单元格值

    arrays - 在 Excel 中将类型字符串数组复制到范围 - 限定符无效

    excel - ExportAsFixedFormat 有时会失败