我正在尝试复制表格并将其粘贴到工作表上的其他位置(用户选择的目标单元格):
但我在 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()
中的第一个方法,而需要逗号来分隔后续(如果有)参数target
是 Range
对象,因此它已经完全符合其父工作表和工作簿对象 此外,我猜
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/