Below is the equivalent VBA code to what you posted above; however, you did not tell us where the "Small", "Medium" or "Large" text should go, so in my code I simply displayed it in a MessageBox... you will have to change the to output it wherever you actually want it.Please help to convert the following COUNTIF formula to VB code..
Logic -
IF
=IF(COUNTIF(G8:G17, "ü")>3, "Medium")
Else IF
=IF(COUNTIF(I8:I17, "ü")>3, "Large"
Then
"Small"
[table="width: 500"]
[tr]
[td]MsgBox Evaluate("IF(COUNTIF(G8:G17,""x"")>3,""Medium"",IF(COUNTIF(I8:I17,""x"")>3,""Large"",""Small""))")[/td]
[/tr]
[/table]
Below is the equivalent VBA code to what you posted above; however, you did not tell us where the "Small", "Medium" or "Large" text should go, so in my code I simply displayed it in a MessageBox... you will have to change the to output it wherever you actually want it.
Code:[TABLE="width: 500"] <tbody>[TR] [TD]MsgBox Evaluate("IF(COUNTIF(G8:G17,""x"")>3,""Medium"",IF(COUNTIF(I8:I17,""x"")>3,""Large"",""Small""))")[/TD] [/TR] </tbody>[/TABLE]
Thank you Rick!
Cell D19 is where I want to output the results when a criteria matches the above use case.
[table="width: 500"]
[tr]
[td]Range("D19").Value = Evaluate("IF(COUNTIF(G8:G17,""x"")>3,""Medium"",IF(COUNTIF(I8:I17,""x"")>3,""Large"",""Small""))")[/td]
[/tr]
[/table]
Code:[TABLE="width: 500"] <tbody>[TR] [TD]Range("D19").Value = Evaluate("IF(COUNTIF(G8:G17,""x"")>3,""Medium"",IF(COUNTIF(I8:I17,""x"")>3,""Large"",""Small""))")[/TD] [/TR] </tbody>[/TABLE]
The order of the IF function calls determines the priority of the responses... I used the ordering that you posted figuring that is what you wanted.Thank you Rick again for the quick reply.
The code does work but is not returning the accurate value.
E.g. When I have 5 ticks in 'column I' and I expect to output Large but it returns with Medium.
Can I alter the code to say,
Check if G8:G17 is >3, if yes, return Medium. if not then check I8:I17 >3, if yes then output Large, if not then return Small.
That is what the code I posted does now. Let me guess at how your users interact with the worksheet and what I think you might want with regard to it. See if this line of code outputs what you want in the order you want given how the user fills in the sheet...Can I alter the code to say,
Check if G8:G17 is >3, if yes, return Medium. if not then check I8:I17 >3, if yes then output Large, if not then return Small.
[table="width: 500"]
[tr]
[td]Range("D19").Value = Evaluate("IF(COUNTIF(I8:I17,""x"")>3,""Large"",IF(COUNTIF(G8:G17,""x"")>3,""Medium"",""Small""))")[/td]
[/tr]
[/table]
Give this event code a try...Now, how to incorporate values in these 3 columns E8:E17 and G8:G17 and I8:I17 that they are entered only once. That is, in row 8, user can only tick E8, G8 or I8. Same applies through to row 17.
[table="width: 500"]
[tr]
[td]Private Sub Worksheet_Change(ByVal Target As Range)
Dim R As Range
If Not Intersect(Target, Range("E:E,G:G,I:G")) Is Nothing Then
For Each R In Intersect(Target, Range("E:E,G:G,I:I")).Rows
If Application.CountA(Intersect(R.EntireRow, Range("E:E,G:G,I:I"))) > 1 Then
MsgBox "You can only put a value in one cell on Row " & R.Row & " within Columns E, G and I. The value you just placed will be removed."
Application.Undo
End If
Next
End If
End Sub[/td]
[/tr]
[/table]