简体   繁体   English

如何使用VBA在Excel单元格中实现公式

[英]How to Implement Formula into Excel cells using VBA

I want to implement the following formula in to Excel cells 我想在Excel单元格中实现以下公式

IF(OR(D12>0,C13=""),"",MAX(SUM($C$12:C13)-$D$9,0))

(once formula is applied I should get following result (formula implemented manually): (应用公式后,我应该得到以下结果(手动实现的公式):

在此处输入图片说明

so i wrote simple macro as below , but unable to implement formula , formula retuns value as "true" 所以我写了下面的简单宏,但是无法实现公式,公式将值重新调整为“ true”

Sub adjustoldbills()

lastRow_sht4 = Sheet4.Range("A" & Rows.Count).End(xlUp).Row
Sheet4.Cells(12, 11) = ""

For i = 1 To lastRow_sht4 - 10
    If Sheet4.Cells(11 + i, 1) <> "" Then
        '=MAX(SUM($C$12:C15)-$D$9,0)
        Sheet4.Cells(12, 4).Formula = "=MAX(SUM($C$12:C" & 12 & ")-$D$9,0)"
        Sheet4.Cells(11 + i, 4).Formula = "=(if(or(D" & 11 + i > 0 & ",C" & 12 + i & "=" & Chr(34) & Chr(34) & ")," & Chr(34) & Chr(34) & "," & "MAX(SUM($C$12:C" & 12 & i & ")-$D$9,0)"
    End If
Next i

End Sub

i am getting wrong result after implemented formula via vba macro as in this image: 通过VBA宏实现公式后,我得到错误的结果,如下图所示:

在此处输入图片说明

how to implement formula and get value as expected. 如何实现公式并获得预期的价值。

When trying to convert long formulas to VBA try using a String variable to help you test it. 当尝试将长公式转换为VBA时,请尝试使用String变量来帮助您对其进行测试。

 Dim FormulaStr              As String

 FormulaStr = "=IF(OR(D" & 11 + i & ">0,C" & 12 + i & "=" & Chr(34) & Chr(34) & ")," & _
      Chr(34) & Chr(34) & ",MAX(SUM($C$12:C" & 12 + i & ")-$D$9,0))"
 Debug.Print FormulaStr

Then, in the immediate window you will get: 然后,在立即窗口中,您将获得:

=IF(OR(D12>0,C13=""),"",MAX(SUM($C$12:C13)-$D$9,0))

which is the formula you want to convert to VBA. 您要转换为VBA的公式。

Now all you need to do is to add the line below: 现在,您需要做的就是添加以下行:

 Sheet4.Cells(11 + i, 4).Formula = FormulaStr

If you want to skip the String variable, you can just replace your line of 如果要跳过String变量,则只需替换以下行即可

Sheet4.Cells(11 + i, 4).Formula = "=(if(or(D" & 11 + i > 0 & ",C" & 12 + i & "=" & Chr(34) & Chr(34) & ")," & Chr(34) & Chr(34) & "," & "MAX(SUM($C$12:C" & 12 & i & ")-$D$9,0)"

to: 至:

Sheet4.Cells(11 + i, 4).Formula = "=IF(OR(D" & 11 + i & ">0,C" & 12 + i & "=" & Chr(34) & Chr(34) & ")," & Chr(34) & Chr(34) & ",MAX(SUM($C$12:C" & 12 + i & ")-$D$9,0))"

I'd go another way, with no loops and exploiting SpecialCells() method of Range object 我会采取另一种方式,没有循环并利用Range对象的SpecialCells()方法

Option Explicit

Sub adjustoldbills()

    With Sheet4
        .Range("D12").FormulaR1C1 = "=MAX(RC[-1]-R9C4,0)" 
        With .Range("A13", .Cells(.Rows.Count, 1).End(xlUp)).SpecialCells(xlCellTypeConstants)
            With .Offset(, 2).SpecialCells(xlCellTypeConstants)
                .Offset(, 1).FormulaR1C1 = "=IF(R[-1]C>0,"""",MAX(SUM(R12C[-1]:RC[-1])-R9C4,0))"
                With .Areas(.Areas.Count)
                    .Offset(, 1).Cells(.Rows.Count).Offset(1).FormulaR1C1 = "=IF(R[-1]C>0,"""",MAX(SUM(R12C[-1]:RC[-1])-R9C4,0))"
                End With
            End With
        End With
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM