Runtime Error 9

B

Bishop

I have the following code:

Dim G as Integer

G = Worksheets("Movies").WorksheetFunction.CountIf("AE:AE", "G")

I'm assuming I'm simply not using the CountIf function correctly but I keep
getting Subscript Out Of Range. What am I doing wrong?
 
M

Mike H

Hi,

Try this but note your criteria of G was invalid and subscriot out of range
could mean your sheet name Movies is spelt incorrectly

G = WorksheetFunction.CountIf(Sheets("Movies").Range("AE:AE"),
Sheets("Movies").Range("G1"))

Mike
 
B

Bishop

Still getting the same error. And I verified that the sheet name is spelled
correctly. Other ideas?
 
D

Dave Peterson

Maybe the activeworkbook isn't what you expected.

Or your worksheet name isn't spelled correctly (check for leading/trailing
spaces, too!)
 
M

Mike H

No more ideas, it works perfectly for me and subscript out of range is Excels
way of telling you it can't find something your telling it to look for

Mike
 
Y

ytayta555

In the same time , Countif is looking for
criteria ,, G ,, , which have the same name
with your G variable , rename variable G with
another name .
 
B

Bishop

That was it! When I made the Movies worksheet active (it wasn't before) it
worked. But I need this in a With block. I tried this:

With Worksheets("Movies")
G = .WorksheetFunction.CountIf(Sheets("Movies").Range("AE:AE"),
Sheets("Movies").Range("G1"))

But that's not working. Will this work in a With block?
 
M

Mike H

hi,

However you got my code to work it wasn't by making Movies the active sheet
because it doesn't need to be active if the sheet is explicitly named in the
code. the correct syntax for what your trying to do now is

With Worksheets("Movies")
G = WorksheetFunction.CountIf(.Range("AE:AE"), .Range("G1"))
End With


Mike
 

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