using VB to enter a sumproduct formula in a cell

R

ram

I would like help with the following syntax error:

Private Sub worksheet_Activate()
If Not ActiveSheet.Previous Is Nothing Then
Me.Range("B2").Formula =
"=SumProduct(--(Sheets("Sheet4").Range(A1:A30000) = Sheets(Processd
Amount).Range(B1)),--(Sheets(Sheet4).Range(AL1:AL30000) > 0))"
End If
End Sub

also is there a way to enter the value of the above formula instead of the
formula?

Thanks for all your help
 
T

Tom Ogilvy

Private Sub worksheet_Activate()
If Not ActiveSheet.Previous Is Nothing Then
str = "=SumProduct(--(Sheets("Sheet4").Range(A1:A30000) = Sheets(Processd
Amount).Range(B1)),--(Sheets(Sheet4).Range(AL1:AL30000) > 0))"
Me.Range("B2").Value = Evaluate(sStr)
End If
End Sub
 
R

ram

Hi Tim,

I copied your code and received a compile error message.

I'm using the code below:

Private Sub worksheet_Activate()
If Not ActiveSheet.Previous Is Nothing Then
str = "=SumProduct(--(Sheets("Sheet4").Range(A1:A30000) = Sheets(Processd
Amount).Range(B1)),--(Sheets(Sheet4).Range(AL1:AL30000) > 0))"
Me.Range("B2").Value = Evaluate(sStr)
End If
End Sub

Thanks for the help
 
T

Tom Ogilvy

str should have been sStr (a typo)

Private Sub worksheet_Activate()
Dim sStr as String
If Not ActiveSheet.Previous Is Nothing Then
sStr = "=SumProduct(--(Sheets("Sheet4").Range(A1:A30000) = Sheets(Processd
Amount).Range(B1)),--(Sheets(Sheet4).Range(AL1:AL30000) > 0))"
Me.Range("B2").Value = Evaluate(sStr)
End If
End Sub
 
R

ram

Hi Tom,

When i use quotes around the sheet name i receive a compile error. If I
delete the quotes from the code i receive the value error in me.range("b2").

do you know what i'm doing wrong?

thanks again.
 
T

Tom Ogilvy

My apologies. I really didn't look at your formula - I assumed it was in
good shape and you had executed it successfully as written - were just asking
how to do it in VBA. But on closer examination, I see you have a mixture of
vba and worksheet formula notation. Evaluate just works as a virtual cell,
so the formula should be as you would place it in a cell:

Sub testit()
Dim sStr As String
Dim v As Variant
sStr = "=SumProduct(--(Sheet4!A1:A30000=" & _
"'Processd Amount'!B1),--(Sheet4!AL1:AL30000 > 0))"
v = Evaluate(sStr)
Debug.Print v
End Sub

tested fine for me. Adapt it to what you are doing.

Note that Processd does not contain an e between the ss and d - if that is
the correct spelling, then no problem. If not, fix it.
 
R

ram

HI Tom,

I apologies for asking som many questions.

i tried the code you just provided and came up with a ref error in cell b2

Private Sub worksheet_Activate()

Dim sStr As String
Dim v As Variant
sStr = "=SumProduct(--(Sheet4!A1:A30000=" & _
"'Processed Amount'!B1),--(Sheet4!AL1:AL30000 > 0))"
v = Evaluate(sStr)
Range("B2") = v
Debug.Print v
End Sub

Many thanks for all your help
 
T

Tom Ogilvy

I can get that error if I don't have sheet named Sheet4 or I don't have a
sheet named Processed Amount or don't have either.

Sounds like a configuration problem with your workbook rather than anything
wrong with the code.

You don't need the Debug.print line.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top