VBA Syntax Error

BenGee

Board Regular
Joined
Mar 5, 2016
Messages
196
Hi - I have this code but it keeps chucking up a syntax error at this part;

Code:
Range("E20").Formula = "IF(AND(D20=1,COUNTIF($C$4:$C$6,">="&1)=3),"Yes",IF(AND(OR(D20=2,D20="3A",D20="3B",D20=4,D20=5),COUNTIF($C$4:$C$6,">="&1)>=1),"Yes","No"))"

Full Code;
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("D3")) Is Nothing Then
    Exit Sub
    Else
        Range("$D$4").FormulaArray = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B4,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
        Range("$D$5").FormulaArray = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B5,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
        Range("$D$6").FormulaArray = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B6,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
        Range("$D$7").FormulaArray = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B7,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
        Range("$D$8").FormulaArray = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B8,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
        Range("$D$9").FormulaArray = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B9,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
        Range("$D$10").FormulaArray = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B10,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
        Range("$D$11").FormulaArray = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B11,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
        Range("$D$12").FormulaArray = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B12,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
        Range("$D$13").FormulaArray = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B13,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
        Range("$D$14").FormulaArray = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B14,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
    End If
    If Intersect(Target, Range("D20")) Is Nothing Then
    Exit Sub
        Range("E20").Formula = "IF(AND(D20=1,COUNTIF($C$4:$C$6,">="&1)=3),"Yes",IF(AND(OR(D20=2,D20="3A",D20="3B",D20=4,D20=5),COUNTIF($C$4:$C$6,">="&1)>=1),"Yes","No"))"
    End If
End Sub

Any idea why this might be?

Thank you
 
Last edited:

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I believe that you will need to identify each of the cells in your IF formula as a range, ie. Range("D1") instead of D1.
 
Upvote 0
You need to double up any quotes that are inside the formula string itself - like this:

Rich (BB code):
Range("E20").Formula = "=IF(AND(D20=1,COUNTIF($C$4:$C$6,"">=""&1)=3),""Yes"",IF(AND(OR(D20=2,D20=""3A"",D20=""3B"",D20=4,D20=5),COUNTIF($C$4:$C$6,"">=""&1)>=1),""Yes"",""No""))"
 
Last edited:
Upvote 0
Perfect, thanks Rory!

It's got rid of the error however nothing happens when I enter a value into D20 whereas having the formula in the cell, it would count as appropriate?
 
Upvote 0
I think you're missing an =
Code:
Range("E20").Formula = "[COLOR=#ff0000]=[/COLOR]IF(AND(D20=1,COUNTIF($C$4:$C$6,"">=""&1)=3),""Yes"",IF(AND(OR(D20=2,D20=""3A"",D20=""3B"",D20=4,D20=5),COUNTIF($C$4:$C$6,"">=""&1)>=1),""Yes"",""No""))"
 
Upvote 0
What are you entering in D20 and what is showing in E20?
 
Upvote 0
D20 will be either 1, 2, 3A, 3B, 4 or 5
E20 is supposed to return "Yes" or "No" dependent on which IF condition is met. Currently though, when I enter a value into E20, it's just blank!
 
Upvote 0
There are two problems. One is the missing = that Fluff mentioned and the other is that your code exits if you didn't change D3. Change the code to this:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("D3")) Is Nothing Then
        Range("$D$4").Value2 = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B4,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
        Range("$D$5").Value2 = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B5,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
        Range("$D$6").Value2 = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B6,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
        Range("$D$7").Value2 = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B7,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
        Range("$D$8").Value2 = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B8,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
        Range("$D$9").Value2 = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B9,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
        Range("$D$10").Value2 = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B10,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
        Range("$D$11").Value2 = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B11,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
        Range("$D$12").Value2 = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B12,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
        Range("$D$13").Value2 = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B13,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
        Range("$D$14").Value2 = Evaluate("INDEX(Roles!$B$2:$AH$12,MATCH('Maps'!B14,Roles!$A$2:$A$12,0),MATCH('Maps'!D3,Roles!$B$1:$AH$1,0))")
    End If
    If Not Intersect(Target, Range("D20")) Is Nothing Then
        Range("E20").Formula = "=IF(AND(D20=1,COUNTIF($C$4:$C$6,"">=""&1)=3),""Yes"",IF(AND(OR(D20=2,D20=""3A"",D20=""3B"",D20=4,D20=5),COUNTIF($C$4:$C$6,"">=""&1)>=1),""Yes"",""No""))"
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,210
Members
453,023
Latest member
alabaz

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