简体   繁体   English

从另一个工作表复制等于单元格值的单元格

[英]Copy cell that equals cell value from another worksheet

I have 3 worksheets: Worksheet A, B, and C.我有 3 个工作表:工作表 A、B 和 C。

Worksheet B has cells that are pulling information from Worksheet A and placing in specific visual format.工作表 B 具有从工作表 A 中提取信息并以特定视觉格式放置的单元格。

I then alter the color of the cells on Worksheet B (the same cells whose value is equal to Worksheet A) and trying to copy both values and colors to Worksheet 3 in one column.然后我更改工作表 B 上单元格的颜色(值等于工作表 A 的相同单元格)并尝试将值和颜色复制到工作表 3 中的一列。

Sub copycellcolor1()
Dim rField As Range
Dim idCell As Range
Dim r1WS As Worksheet
Dim rrWS As Worksheet

Set r1WS = Worksheets("RACK 1")
Set rField = r1WS.Range("C6:N13")
Set rrWS = Worksheets("Reruns To Pull")

For Each idCell In rField

    If idCell.Interior.color = RGB(204, 204, 255) Then
        idCell.Copy rrWS.Range("A1").Offset(rrWS.Rows.Count - 1, 0).End(xlUp).Offset(1, 0)
    End If
    
Next idCell

rrWS.Columns.AutoFit

End Sub

I'm getting the correct color on Worksheet 3, but the value is #REF!我在工作表 3 上得到了正确的颜色,但值为#REF! because the actual value of the cell is referring to Worksheet A.因为单元格的实际值是指工作表 A。

Can someone please explain what I'm doing wrong?有人可以解释一下我做错了什么吗?

PasteSpecial特殊粘贴

You're using the wrong way to copy/paste .您使用错误的方式来复制/粘贴 Try the following:请尝试以下操作:

    If idCell.Interior.Color = RGB(204, 204, 255) Then
        With rrWS.Range("A1").Offset(rrWS.Rows.Count - 1, 0).End(xlUp).Offset(1, 0)
            idCell.Copy
            ' Paste values.
            .PasteSpecial xlPasteValues
            ' Paste formats.
            .PasteSpecial xlPasteFormats
            Application.CutCopyMode = False
        End With
    End If
    
    ' This is a little bit ridiculous, but it works.
    If idCell.Interior.Color = RGB(204, 204, 255) Then
        With rrWS.Range("A1").Offset(rrWS.Rows.Count - 1, 0).End(xlUp).Offset(1, 0)
            ' Paste values.
            .Value = idCell.Value
            ' Paste formats.
            idCell.Copy
            .PasteSpecial xlPasteFormats
            Application.CutCopyMode = False
        End With
    End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM