gpt4 book ai didi

vba - 使用 vba 在范围内的依赖单元格上添加公式

转载 作者:行者123 更新时间:2023-12-02 18:34:00 24 4
gpt4 key购买 nike

我想使用 VBA 在单元格上插入计算。这是我现在插入它的方法。它工作得很好,但我无法修改发票上的百分比。我希望在插入行后我可以修改百分比,它将自动更新售价。

Private Sub CommandButton1_Click()
Dim wsInvoice As Worksheet, wsRange As Worksheet, wsPrice As Worksheet
Dim nr As Integer, lr As Integer
With ThisWorkbook
Set wsInvoice = .Worksheets("Invoice")
Set wsRange = .Worksheets("Range")
Set wsPrice = .Worksheets("Price")
End With
nr = wsInvoice.Cells(Rows.Count, 1).End(xlUp).Row + 1
Select Case Me.ComboBox1
Case "Paper"
wsRange.Range("Paper").Copy wsInvoice.Cells(nr, 1)
lr = wsInvoice.Cells(Rows.Count, 1).End(xlUp).Row
For i = nr To lr
wsInvoice.Cells(i, 2) = Application.VLookup(Cells(i, 1), wsPrice.Range("A:B"), 2, 0)
wsInvoice.Cells(i, 3) = (".3")
wsInvoice.Cells(i, 4).Formula = FormatCurrency(wsInvoice.Cells(i, 2).Value / (1 - (wsInvoice.Cells(i, 3))), 2, vbFalse, vbFalse, vbTrue)
Next i

这是下载我的文档的链接。 https://drive.google.com/file/d/0By_oZy042nKWdTVmX0Fid3JVSHM/edit?usp=sharing

最佳答案

我认为这里的FormatCurrency有点无用,这可以通过格式化列一次并保持原样来完成。表单函数内的 Formula 和 FormulaLocal 似乎存在问题。这是我的修复:

删除行wsInvoice.Cells(i,4).Formula ...

在 CommandButton1_Click() 的末尾添加此行 FormulaCorrection

在模块内,写下这个非常简单的函数,它将执行您想要的操作:

Sub FormulaCorrection()

Sheets("Invoice").Activate
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To lastRow
Cells(x, 4).FormulaLocal = "=B" & x & "/(1-C" & x & ")"
Next x

End Sub

关于vba - 使用 vba 在范围内的依赖单元格上添加公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20979858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com