如何從函式回傳結果?
例如:vba:我想要函式
Function xy2cell(i, f)
xy2cell = "=" & "?????????????????????????????"
End Function
Sub aaa_main()
ActiveSheet.Cells.Clear
f = "5^4*x-2^4*y-1"
For i = 1 To 2
Cells(i, 3) = xy2cell(i, f)
Next
End Sub
'I want
'Cells(1, 3) = "=5^4*" & Cells(1, 1).Address & "-2^4*" & Cells(1, 2).Address & "-1"
'Cells(2, 3) = "=5^4*" & Cells(2, 1).Address & "-2^4*" & Cells(2, 2).Address & "-1"
(20220328)
原版的
日文如下↓↓↓↓↓↓------------------------------------------ -
2022年數學1A Q4 < 大學入學通用考試是日本大學的通用入學考試
https://cdn.mainichi.jp/item/jp/etc/kyotsu-2022/pdf/MTAP.pdf#page=20
我試試
https://qiita.com/mrrclb48z/items/af08059157cfbce8f0fe
日語向上↑↑↑↑↑-------------------------------------------
uj5u.com熱心網友回復:
一種更簡單的方法是使用范圍的 formual2R1C1 屬性。這使您可以使用將單元格稱為與目標單元格的偏移量的符號來指定公式。這樣,可以使用單個運算式在區域的每個目標單元格中??創建不同的公式。
Sub aaa_main_2()
Dim f As String
f = "=5^4*x-2^4*y-1"
f = Replace(f, "x", "RC[-2]")
f = Replace(f, "y", "RC[-1]")
ActiveSheet.Cells.Clear
Range("C1:C2").Formula2R1C1 = f
End Sub
或者,更直接地
Sub aaa_main_3()
ActiveSheet.Cells.Clear
Range("C1:C2").Formula2R1C1 = "=5^4*RC[-2]-2^4*RC[-1]-1"
End Sub
uj5u.com熱心網友回復:
從活動作業表中清除所有單元格似乎是不尋常的,因為這將洗掉函式將在其上運行的任何輸入。盡管如此,這是您的代碼轉換為按照您的要求執行的操作。我添加了 Dim 陳述句來宣告代碼使用的變數。
Function xy2cell(i As Long, f As String)
Dim formula As String
formula = Replace(f, "x", Cells(i, 1).Address(False, False))
formula = Replace(formula, "y", Cells(i, 2).Address(False, False))
xy2cell = "=" & formula
End Function
Sub aaa_main()
Dim f As String
Dim i As Long
ActiveSheet.Cells.Clear
f = "5^4*x-2^4*y-1"
For i = 1 To 2
Cells(i, 3).Formula = xy2cell(i, f)
Next
End Sub
此代碼使用“替換”函式在公式字串 (f) 中查找“x”并將其替換為適當的單元格參考。結果存盤在名為“公式”的變數中,然后將其用作輸入以將 y 替換為適當的單元格參考。
但是,使用 formula2R1C1 屬性有一個更簡單的方法。我將發布有關該技術的單獨解決方案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/451475.html
