2
好吧,所以我的問題是無論NumberFormat被設置,我的公式仍顯示爲文本,直到我進入單元格並按回車。我試圖做的是動態地創建一個新的列,並用公式填充第5行 - > 500的範圍。我的完整代碼如下,我嘗試了不同的變體,例如將公式的輸出專門設置爲格式,在公式設置後將.NumberFormat =
移至With
塊內。我也嘗試.Calculate
範圍內嘗試並重新計算。這個公式並不簡單,它仍然不喜歡它。公式顯示爲文本,無論NumberFormat設置爲一般
Sub AddHaulageRows()
If ActiveSheet.Name Like "WK*" Or ActiveSheet.Name = "WTemplate" Then
If ActiveSheet.Cells(3, 16).Value <> "Haulage Rate" Then
Columns("P:P").Insert Shift:=xlToRight
', CopyOrigin:=xlFormatFromLeftOrAbove
ActiveSheet.Cells(3, 16).Value = "Haulage Rate"
ActiveSheet.Range("P5:P500").NumberFormat = "General"
With ActiveSheet.Range("P5:P500")
'Setting formula to the most simplest thing I can do.
.FormulaR1C1 = "=TRUE"
End With
Else
Columns("P:P").Delete Shift:=xlToLeft
End If
Else
MsgBox "Cannot add row as this is not a valid weekly sheet.", vbCritical
End If
End Sub
奇怪的是,它在我的工作簿中的一些工作表上,但不是其他人。在一些表上,它只是給出了一個1004 Application-defined or object-defined
錯誤。這些表都符合相同的模板。
這可能是[這]一個更邪惡的表現(http://superuser.com/q/299437/ 52365)。您可能需要先執行'value'技巧,然後設置公式。 – GSerg
該代碼對我來說工作正常,但您也有時會說它適合您。我同意@GSerg強調單元格的「價值」將是快速簡便的解決方案。 –