簡體   English   中英

VBA將公式插入現有單元格

[英]VBA insert a formula to existing cell

單元格A1指的是單元格A5,其值為5.5。 我想向Cell A1插入一個圓形函數,讀取= round(A5,)。 我在下面嘗試了下面的代碼,但它沒有按照我的意圖工作。

Sub roundmacro()
   Dim n As String
   n = Range("A1").Value 'returns the value but I need reference from that range object instead  
   Range("A1").Formula = "=round(" & n & ",)"
End Sub

提前致謝

Range("A1").Formula = "=Round(" & Mid(Range("A1").Formula, 2) & ",)"

這會將任何現有公式轉換為該公式的rounding 因此,如果初始公式為=A5 ,則新公式變為=Round(A5,)

ps適用於任何返回數字的初始公式(當然,您不能對字符串進行舍入)。

那么你想讓A1中的公式顯示“A5”嗎?

Range("A1") = "=Round(A5,)"

在VBA中進行計算不是更好,如下所示:

Sub formulas()

Range("A1").Value = Application.WorksheetFunction.Round(Range("A5").Value, 0) ' You can put any formula after the 'Application.WorksheetFunction.'

End Sub

對我來說很好,A1 = 6

Sub roundmacro()
   Dim n As String
   n = Range("A5").Value
   Range("A1").Formula = "=round(" & n & ",)"
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM