我正在尝试从“主”表中复制数据,然后将其粘贴到“ csv”表中。但是,尝试此操作时,出现运行时错误“'1004':Range类的PasteSpecial方法失败”。
请在下面查看我的代码。这是'.Cells(1,2).PasteSpecial xlPasteValues'引起的。谢谢。
With main
n = 1
For Each c In .Range("C1:C200")
If c.Value = "x" Then
c1 = Cells(c.Row, Columns.Count).End(xlToLeft).Column
If c1 > 2 Then c.Offset(0, 2).Resize(, c1 - 4).Copy
With csv
If n = 1 Then
.Cells(1, 1) = "ID"
.Cells(1, 2).PasteSpecial xlPasteValues
Else
.Cells(n, 1) = n - 1
.Cells(n, 2).PasteSpecial xlPasteValues
End If
End With
n = n + 1
End If
Next c
End With
答案 0 :(得分:1)
将副本和粘贴置于相同的条件下:
If c1 > 2 Then
c.Offset(0, 2).Resize(, c1 - 4).Copy
With csv
If n = 1 Then
.Cells(1, 1) = "ID"
.Cells(1, 2).PasteSpecial xlPasteValues
Else
.Cells(n, 1) = n - 1
.Cells(n, 2).PasteSpecial xlPasteValues
End If
End With
End If
然后更改此行:
c1 = Cells(c.Row, Columns.Count).End(xlToLeft).Column
确切地说(点很重要):
c1 = .Cells(c.Row, .Columns.Count).End(xlToLeft).Column