如何将带有动态值的函数插入到列中的每个单元格中

huangapple go评论65阅读模式
英文:

How to Insert function w/ dynamic value into every cell in column

问题

我试图在未知行数的“F”列的每个单元格中粘贴并执行一个函数。

函数是=QM_Stream_Delta(Value)

其中Value是相应行的“A”列单元格内容。

这是我的VBA脚本:

Sub GetDelta()
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
    Dim i As Integer
    myFormula = "=QM_Stream_Delta(Value)"
    For i = 9 To FinalRow
        OptionSymbol = Cells(i, "A")
        Cells(i, "F").Value = Evaluate(Replace(myFormula, "Value", OptionSymbol))
    Next i
End Sub

但我不确定如何实际将函数放入其中——对于Cells(i, "F").Value(myFormula(OptionSymbol)我得到了语法错误。

英文:

I'm trying to paste and execute a function in every cell of column "F" over an unknown number rows.

The function is =QM_Stream_Delta(Value)

where Value is the cell contents of column "A" of the corresponding row.

如何将带有动态值的函数插入到列中的每个单元格中

Here's is my VBA script so far:

Sub GetDelta()
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
    Dim i As Integer
    myFormula = "=QM_Stream_Delta(Value)"
    For i = 9 To FinalRow
        OptionSymbol = Cells(i, "A")
        Cells(i, "F").Value(myFormula(OptionSymbol)
    Next i
End Sub

I'm not sure how to actually get the function in there though -- I'm getting a syntax error for

Cells(i, "F").Value(myFormula(OptionSymbol)

答案1

得分: 2

这里是关于Formula2R1C1的另一个用例。

Sub GetDelta()
    Dim Target As Range
    
    Set Target = Range("F1").Resize(Cells(Rows.Count, 1).End(xlUp).Row)
    Set Target = Intersect(Target, Target.Offset(8))
    
    Target.Formula2R1C1 = "=@QM_Stream_Delta(RC1)"
End Sub

我们应该养成完全限定引用的习惯。这里我将目标范围限定到Delta工作表。

Sub GetDelta()
    Dim Target As Range
    
    With ThisWorkbook.Worksheets("Delta")
        Set Target = .Range("F1").Resize(.Cells(Rows.Count, 1).End(xlUp).Row)
        Set Target = Intersect(Target, Target.Offset(8))
    End With
    Target.Formula2R1C1 = "=@QM_Stream_Delta(RC1)"
End Sub
英文:

Here is another use case for Formula2R1C1.

Sub GetDelta()
    Dim Target As Range
    
    Set Target = Range("F1").Resize(Cells(Rows.Count, 1).End(xlUp).Row)
    Set Target = Intersect(Target, Target.Offset(8))
    
    Target.Formula2R1C1 = "=@QM_Stream_Delta(RC1)"
End Sub

We should be in the habit of fully qualifying our references. Here I qualified the Target range to the Delta worksheet.

Sub GetDelta()
    Dim Target As Range
    
    With ThisWorkbook.Worksheets("Delta")
        Set Target = .Range("F1").Resize(.Cells(Rows.Count, 1).End(xlUp).Row)
        Set Target = Intersect(Target, Target.Offset(8))
    End With
    Target.Formula2R1C1 = "=@QM_Stream_Delta(RC1)"
End Sub

答案2

得分: 0

使用公式中的单元格引用。

Sub GetDelta()
    Const StartRow As Long = 9

    Dim FinalRow As Long
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row

    Range("F" & StartRow & ":F" & FinalRow).Formula = "=QM_Stream_Delta(A" & StartRow & ")"
End Sub
英文:

Use a reference to a cell within your formula.

Sub GetDelta()
    Const StartRow As Long = 9

    Dim FinalRow As Long
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row

    Range("F" & StartRow & ":F" & FinalRow).Formula = "=QM_Stream_Delta(A" & StartRow & ")"
End Sub

答案3

得分: 0

我注意到的一个问题是,你的循环从行9开始,而不是从行1开始。你的截图显示你想要插入的公式应该从第1行开始。

我修改了你的宏以构建你想要的“函数”,然后将它放入单元格。

这个函数:

Sub GetDelta()
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
    Dim i As Integer
    Dim myFormula As String
    For i = 1 To FinalRow
        myFormula = "=QM_Stream_Delta(A" & i & ")"
        Cells(i, "F").Formula = myFormula
    Next i
End Sub

我运行它,它会在列F中填充函数。我不知道QM_Stream_Delta()函数是什么,但VBA宏会将公式放入单元格中:

如何将带有动态值的函数插入到列中的每个单元格中

所以现在你可以运行VBA函数,在你想要的行和列中自动添加对QM_Stream_Delta()的调用:

A B C D E F
A =QM_Stream_Delta(A1)
B =QM_Stream_Delta(A2)
C =QM_Stream_Delta(A3)

就个人而言,我不太明白这样做的好处;Excel已经有能力快速填充一列值了。 (Ctrl+D)

英文:

One issue i noticed is that you're starting your loop at row 9 rather than row 1. Your screenshot shows you want the inserted formulas to start at row 1.

I changed your macro to build the "function" you want, and then put it in the cell.

The function:

Sub GetDelta()
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
    Dim i As Integer
    Dim myFormula As String
    For i = 1 To FinalRow
        myFormula = "=QM_Stream_Delta(A" & i & ")"
        Cells(i, "F").Formula = myFormula
    Next i
End Sub

I run it and it populates column F with the function. I don't know what the function QM_Stream_Delta() is, but the VBA macro places the formula in the cells:

如何将带有动态值的函数插入到列中的每个单元格中

So now you can run the VBA function, and automatically add the call to QM_Stream_Delta() in the rows you want, in the column you want:

A B C D E F
A =QM_Stream_Delta(A1)
B =QM_Stream_Delta(A2)
C =QM_Stream_Delta(A3)

Personally, I don't quite understand the virtue of this; Excel already has the ability to quickly populate a column of values. (<kbd>Ctrl</kbd>+<kbd>D</kbd>)

huangapple
  • 本文由 发表于 2023年6月22日 20:43:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76532047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定