Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA代码中的特殊粘贴不起作用_Vba_Excel - Fatal编程技术网

VBA代码中的特殊粘贴不起作用

VBA代码中的特殊粘贴不起作用,vba,excel,Vba,Excel,我一直在阅读和尝试代码,但它仍然无法工作。有人能告诉我我做错了什么吗?我的excel文件在一个选项卡上有公式,我试图复制并粘贴到另一个选项卡上,但我尝试的一切都是粘贴公式,而不是我需要的文本输出 Dim rowCount2 As Long, shtSrc As Worksheet Dim shtDest As Worksheet Dim rng2 As Range Dim currentRow As Long Set shtSrc = Sheets("Data") Set shtDest =

我一直在阅读和尝试代码,但它仍然无法工作。有人能告诉我我做错了什么吗?我的excel文件在一个选项卡上有公式,我试图复制并粘贴到另一个选项卡上,但我尝试的一切都是粘贴公式,而不是我需要的文本输出

Dim rowCount2 As Long, shtSrc As Worksheet
Dim shtDest As Worksheet
Dim rng2 As Range
Dim currentRow As Long

Set shtSrc = Sheets("Data")
Set shtDest = Sheets("Audit")

rowCount2 = shtSrc.Cells(Rows.Count, "A").End(xlUp).Row

Set rng2 = shtSrc.Range("A1:A" & rowCount2)

currentRow = 1

For Each cell2 In rng2.Cells
    If cell2.Value <> "" Then
          cell2.Copy shtDest.Range("B" & currentRow).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
          cell2.Offset(0, 1).Copy ("C" & currentRow)
          cell2.Offset(0, 2).Copy ("G" & currentRow)
        currentRow = currentRow + 1

    End If
Next cell2
Dim rowCount2为长,shtSrc为工作表
Dim shtDest作为工作表
变暗rng2 As范围
与当前行一样长
设置shtSrc=图纸(“数据”)
Set shtDest=表格(“审核”)
rowCount2=shtSrc.Cells(Rows.Count,“A”).End(xlUp).Row
设置rng2=shtSrc.Range(“A1:A”和rowCount2)
currentRow=1
对于rng2中的每个单元格2。单元格
如果cell2.Value为“”,则
cell2.Copy shtDest.Range(“B”¤tRow).粘贴特殊粘贴:=xlPasteValuesAndNumberFormats,操作:=xlNone,SkipBlanks:=False,转置:=False
单元格2.偏移量(0,1).复制(“C”和当前行)
单元格2.偏移量(0,2).复制(“G”和当前行)
currentRow=currentRow+1
如果结束
下一单元2

您不能将
PasteSpecial
Copy
方法的destination参数一起使用-您必须使用单独的操作,但由于您仅复制单个单元格,您可以只分配值:

shtDest.Range("B" & currentRow).Value2 = cell2.Value2
shtDest.Range("C" & currentRow).Value2 = cell2.Offset(0, 1).Value2
shtDest.Range("G" & currentRow).Value2 = cell2.Offset(0, 2).Value2

当我这样尝试时,我得到了“复制方法范围失败”错误。不可能-在该代码中没有
copy
方法。啊!明白了!我把副本忘在里面了。谢谢你的澄清,这就解决了它!