简体   繁体   中英

Excel VBA Evaluate with String in Formula

I'm trying to get VBA to evaluate a formula as it goes over a loop. The portion that fails is the Evaluate() function itself, or at least the syntax I'm using.

Worksheets("Sheet2").Range("C2").Offset(All, 0) = _
     Evaluate("((SUMPRODUCT(SUBTOTAL(2,OFFSET(PercentMet!$I$2,ROW(PercentMet!$I$2:$I$27301)-ROW(PercentMet!$H$2),0)),PercentMet!$I$2:$I$27301,PercentMet!$G$2:$G$27301)/SUMPRODUCT(SUBTOTAL(9,OFFSET(PercentMet!$G$2,ROW(PercentMet!$G$2:$G$27301)-ROW(PercentMet!$G$2),0)),--(PercentMet!$I$2:$I$27301<>""NA""))))")

The portion that fails is the ""NA"" at the end of the formula. Using this formula each cell equates to #VALUE!

If I remove the Evaluate portion the formula works as I want, but I need Evaluate because I'm looping through various filters and each value is unique.

Entire Code is Below:

Sub EthFilter()

Application.ScreenUpdating = False

Dim EthName As Range, GradeName As Range, Rate As Variant, Grade As Variant
Dim One As Integer, Zero As Integer, All As Integer

Set EthName = Worksheets("Sheet2").Range("J1")
Set GradeName = Worksheets("Sheet2").Range("K1")

One = 0
All = 0

For Each Raeth In Range("J1:J7")

Zero = 0
Rate = EthName.Offset(One, 0)

    With Worksheets("PercentMet")

        .AutoFilterMode = False

            With .Range("$A$1:$O$27301")
                .AutoFilter Field:=6, Criteria1:=Rate

                For Each Grades In Range("B2:B9")

                    Grade = GradeName.Offset(Zero, 0).Value

                    With Worksheets("PercentMet")

                        With .Range("$A$1:$O$27301")
                            .AutoFilter Field:=5, Criteria1:=Grade
                            Worksheets("Sheet2").Range("C2").Offset(All, 0) = _
                                Evaluate("((SUMPRODUCT(SUBTOTAL(2,OFFSET(PercentMet!$I$2,ROW(PercentMet!$I$2:$I$27301)-ROW(PercentMet!$H$2),0)),PercentMet!$I$2:$I$27301,PercentMet!$G$2:$G$27301)/SUMPRODUCT(SUBTOTAL(9,OFFSET(PercentMet!$G$2,ROW(PercentMet!$G$2:$G$27301)-ROW(PercentMet!$G$2),0)),--(PercentMet!$I$2:$I$27301<>""NA""))))")

                        End With

                    End With

                All = All + 1
                Zero = Zero + 1
                Next Grades

            End With

    End With

One = One + 1

Next Raeth

Application.ScreenUpdating = True

End Sub

If the length of the formula is a problem then instead of this (line breaks added for clarity):

Worksheets("Sheet2").Range("C2").Offset(All, 0) = Evaluate(
"((SUMPRODUCT(SUBTOTAL(2,OFFSET(PercentMet!$I$2,ROW(PercentMet!$I$2:$I$27301)-
ROW(PercentMet!$H$2),0)),PercentMet!$I$2:$I$27301,PercentMet!$G$2:$G$27301)/
SUMPRODUCT(SUBTOTAL(9,OFFSET(PercentMet!$G$2,ROW(PercentMet!$G$2:$G$27301)-
ROW(PercentMet!$G$2),0)),--(PercentMet!$I$2:$I$27301<>""NA""))))")

you can use this form:

Worksheets("Sheet2").Range("C2").Offset(All, 0) = Worksheets("PercentMet").Evaluate(
"((SUMPRODUCT(SUBTOTAL(2,OFFSET($I$2,ROW($I$2:$I$27301)-
ROW($H$2),0)),$I$2:$I$27301,$G$2:$G$27301)/
SUMPRODUCT(SUBTOTAL(9,OFFSET($G$2,ROW($G$2:$G$27301)-
ROW($G$2),0)),--($I$2:$I$27301<>""NA""))))")

Since all the inputs come from the same sheet you can use that sheet's Evaluate method and the formula will be evaluated in the context of that sheet.

The default Application.Evaluate version uses whichever sheet is Active at the time of execution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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