我想創建一個宏來複制其中有公式的範圍(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正試圖在不覆蓋現有公式的情況下複製值 –