【问题标题】:Copy cell values and cell comments when another cell contains certain values当另一个单元格包含某些值时复制单元格值和单元格注释
【发布时间】:2017-09-09 03:43:30
【问题描述】:

我之前在此代码方面获得了很大帮助。 如果 C 列中的单元格包含值列表(Value1、Value2、Value3、Value4),它将公式从 A 列复制到 D 列,并将值从 B 列复制到 E 列。

我现在还需要将单元格 cmets 以及单元格值从 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

【问题讨论】:

  • 你必须付出一些努力。你在网上看过吗?你发现/尝试了什么?
  • 你已经有了从 B 复制值到 E 的代码 .... 只需添加复制 cmets 的代码 .... 你必须自己弄清楚
  • 大家好,感谢您的反馈。我是 VBA 的新手,但看了很多页面:stackoverflow.com、msdn.microsoft.com、mrexcel.com、excelforum.com、excel.tips.net、ozgrid.com、experts-exchange.com涉及使用“Range.PasteSpecial xlPasteComments”或“Range.PasteSpecial Paste:=xlPasteComments”的方法,但是当我尝试将其合并到现有代码中时,它不起作用。感谢您的反馈,以后我会添加我之前尝试过的内容以帮助排除这些问题。

标签: vba excel


【解决方案1】:

替换这个:

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)

这应该可以解决问题。

【讨论】: