formula to vba

cmccabe

Active Member
Joined
Feb 20, 2008
Messages
396
I am having trouble converting a manual formula into VBA:

The formula below worked great when all the values were on one sheet:

Code:
 =IF(COUNTIFS( A$2:A$6713,F2,B$2:B$6713,"<="&G2,C$2:C$6713, ">="&G2),VLOOKUP(G2,$B$2:$D$6713,3,1),"No")

Now I am trying to create a VBA with the values on two sheets, below is what I have:
VB
Code:
 Const myEpilepsy As String = "=IF(SUMPRODUCT(--(Panel!$B$2:$B$6713=Annovar!$Q$5),--(Panel!$C$2:$C$6713<=Annovar!$R$5),--(Panel!$D$2:$D$6713>Annovar!$R$5)),VLOOKUP(Annovar!$R$5,Panel!$C$2:$E$6713,3,1),""No"")"

Thanks.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
If I change the underlined line of code to match the original formula, I get application define or object defined error on the line in bold. Thanks.

Code:
 Sub Calculations()Dim l As Long
Dim Res As Variant


    Res = Application.VLookup(Range("A2").Value, Range("CA:CF"), 6, 0)


[U]    Const myEpilepsy As String = "=IF(COUNTIFS(--(Panel!$B$2:$B$6713=$Q$5),--(Panel!$C$2:$C$6713<=$R$5),--(Panel!$D$2:$D$6713>$R$5)),VLOOKUP($R$5,Panel!$C$2:$E$6713,3,1),""No"")"[/U]
    Const myMarfan As String = "=IF(SUMPRODUCT(--(Panel!$B$25610:$B$29333=$Q$5),--(Panel!$C$25610:$C$29333<=$R$5),--(Panel!$D$25610:$D$D29333>$R$5)),VLOOKUP($R$5,Panel!$C$25610:$E$29333,3,1),""No"")"


    If Not IsError(Res) Then
         l = Sheets("annovar").Range("A" & Rows.Count).End(xlUp).Row


        Select Case Res
            Case "Comprehensive Epilepsy"
[B]                Sheets("annovar").Range("AQ5:AQ" & l).Formula = myEpilepsy[/B]
                 
            Case "Marfan Disorder"
               Sheets("annovar").Range("AQ5:AQ" & l).Formula = myMarfan
        End Select


    End If
    
End Sub
 
Upvote 0
Should R1C1 reference style be used? Thanks.

So if I was writing this formula in R1C1
<!-- BEGIN TEMPLATE: bbcode_code -->
select_all_icon.jpg
page_white_copy.png


<CODE> Const myEpilepsy As String = "=IF(SUMPRODUCT(--(Panel!$B$2:$B$6713=Annovar!$Q$5),--(Panel!$C$2:$C$6713<=Annovar!$R$5),--(Panel!$D$2:$D$6713>Annovar!$R$5)),VLOOKUP(Annovar!$R$5,Panel!$C$2:$E$6713,3,1),""No"")"</CODE></PRE>
<!-- END TEMPLATE: bbcode_code -->it would be:
<!-- BEGIN TEMPLATE: bbcode_code -->
select_all_icon.jpg
page_white_copy.png


<CODE> Const myEpilepsy As String = "=IF(SUMPRODUCT(--(Panel!$R[2]$R[2]:$R[2]$R[6713]=Annovar!$R[17]$R[5]),--(Panel!$R[3]$R[2]:$R[3]$R[6713]<=Annovar!$R[18]$R[5]),--(Panel!$R[4]$R[2]:$R[4]$R[6713]>Annovar!$R[18]$R[5])),VLOOKUP(Annovar!$R[18]$R[5],Panel!$R[3]$R[2]:$R[5]$R[6713],3,1),""No"")"</CODE></PRE>
 
Upvote 0

Forum statistics

Threads
1,215,521
Messages
6,125,303
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top