I am new to VBA and inherited a requirements tracking spreadsheet from someone else, which I am currently updating. The requirements are already grouped and summed correctly with the current VBA code. The particular block I am trying to update (for example, there are multiple levels):
For groups = 1 To i ' Level 3 grouping
Range(Cells(Start(groups), 1), Cells(Finish(groups), 1)).Rows.Group
Range("G" & Start(groups) - 1).Formula = "=COUNTIF(E" & Start(groups) & ":E" & Finish(groups) & ",""Requirement"")"
Range("H" & Start(groups) - 1).Formula = "=COUNTIF(H" & Start(groups) & ":H" & Finish(groups) & ",""Ok"")"
Next
I need to add an extra condition to the COUNTIF, so I am using COUNTIFS. I want to only count requirements that are not level 4 (they can be level 1, 2, 3, or 4). I have tried variations on the following logic:
For groups = 1 To i ' Level 3 grouping
Range(Cells(Start(groups), 1), Cells(Finish(groups), 1)).Rows.Group
Range("G" & Start(groups) - 1).Formula = "=COUNTIFS(E" & Start(groups) & ":E" & Finish(groups) & "," & "Requirement" & ",S" & Start(groups) & ":S" & Finish(groups) & ",""<>4"")"
Range("H" & Start(groups) - 1).Formula = "=COUNTIF(H" & Start(groups) & ":H" & Finish(groups) & ",""Ok"")"
Next
I tried entering the formula(s) manually in the correct cells manually before executing the macro and the formula updated the sums correctly. However, I always get the following when I run the macro:
RunTime Error ‘1004’ Application_defined or object_defined error
I realize I have a lot of quotes and some could probably be consolidated; however, I’m more concerned with why the logic is failing. Could you please help? I’ve spent way too much time on this exploring different avenues.
Thank you in advance!
After this, I will have to learn how to hide each “level 4” requirement (which means that row and the following 2 rows), but that is another topic. I thought I’d try the easier logic first..
Your use of double quotes inside double quotes is your problem. Use
Chr(34)instead.The Chr() method returns the ASCII character at the index number you specify. In this case, Chr(34) returns the double quote character. For more information about ASCII characters, check out the following table:
http://www.asciitable.com/
EDIT:
Alternatively, you could also use double double-quotes, like so:
instead of