当另一个单元格包含某些值时,复制单元格值和单元格注释

时间:2017-09-08 17:38:10

标签: excel vba excel-vba

我之前收到过这段代码的大力帮助。 如果C列中的单元格包含值列表(Value1,Value2,Value3,Value4),它会将公式从A列复制到D列,将值从B列复制到E列。

我现在还需要复制单元格注释以及从B列到E列的单元格值。

有谁知道我怎么能这样做?

Sub RangeCopyPaste()

Dim cell As Range

Range("D6:E1000").Clear

Set OcRange = Range("D6") 'Set the first destination cell

For Each cell In Worksheets("OverviewTest").Range("C6:C1000") 'Loop through your Status column
    Select Case cell.Value 'Select Case is an alternative to writing multiple If, ElseIf statements, particularly if you want it to run the same code when it is true.
        Case "Value1", "Value2", "Value3", "Value4" 'Specify all the values which would consitute a "True" result
            OcRange.Formula = Range(cell.Offset(0, -2), cell.Offset(0, -2)).Formula 'Copies the formula from Column A
            OcRange.Offset(0, 1).Value = Range(cell.Offset(0, -1), cell.Offset(0, -1)).Value ' Copies the value from Column B
            Set OcRange = OcRange.Offset(1, 0) 'Setup the new destination cell ready for the next "True" result
    End Select
Next cell

End Sub

1 个答案:

答案 0 :(得分:0)

替换它:

OcRange.Formula = Range(cell.Offset(0, -2), cell.Offset(0, -2)).Formula
OcRange.Offset(0, 1).Value = Range(cell.Offset(0, -1), cell.Offset(0, -1)).Value

有了这个:

Range(cell.Offset(0, -2), cell.Offset(0, -2)).Copy
OcRange.PasteSpecial (xlPasteFormulas)
OcRange.PasteSpecial (xlPasteComments)

Range(cell.Offset(0, -1), cell.Offset(0, -1)).Copy
OcRange.Offset(0, 1).PasteSpecial (xlPasteValues)
OcRange.Offset(0, 1).PasteSpecial (xlPasteComments)

这应该可以解决问题。