我想创建一个宏来复制其中有公式的范围(C2:C22)并将它粘贴到列范围(D2:D22)上作为值。我的问题是每6个单元我有一个公式,我不希望宏覆盖它。 我一直在尝试这个宏,但它不复制公式,只有值,我需要粘贴在值,而不是公式。将IF值粘贴到目标中
谢谢!
Sub example()
Dim source As Range
Dim target As Range
Set source = ActiveSheet.Range("c2:c22")
Set target = ActiveSheet.Range("d2:d22")
copy_formulas source:=source, target:=target
End Sub
Public Sub copy_formulas(source As Range, target As Range)
'Assumes that all formulas start with '=' and all non formulas do not
Dim i As Long
Dim j As Long
Dim c As Range
For i = 1 To source.Rows.Count
For j = 1 To source.Columns.Count
Set c = source(RowIndex:=i, ColumnIndex:=j)
If Left(c.Formula, 1) <> "=" Then
target(RowIndex:=i, ColumnIndex:=j).Value = c.Value
End If
Next j
Next i
End Sub
你的问题说: “我一直在试图与这个宏,但它不会复制公式,只值,我需要粘贴值,而不是在公式上。“ 这听起来像是在做你需要的东西。你能澄清吗? –
@ChrisMoore OP正试图在不覆盖现有公式的情况下复制值 –