Subscript out of range error for cell ref

Reset

Board Regular
Joined
Apr 16, 2010
Messages
227
I am getting A subscript out of range error when I change a cell column reference from a 2 to a 9. It is actually anything greater than 4 will set it off. Can anyone see the issue?

Private Sub Workbook_Open()
c = Sheets.Count
For Z = 1 To c
If Sheets(Z).Name = "Sheet1" Then GoTo line1
If Sheets(Z).Name = "dat" Then GoTo line1
Sheets(Z).Select
a = Application.WorksheetFunction.CountA(Columns(1))
b = Application.WorksheetFunction.CountA(Columns(2))

If a - b < 1 Then GoTo line1
ReDim q(a - b) As Integer
inc = 0
For x = 2 To a
If Cells(x, 9) = "" Then q(inc) = Cells(x, 1): inc = inc + 1
'error at above line
Next x
Sheets("dat").Select
v = Application.WorksheetFunction.CountA(Columns(1))
For y = 1 To inc
Cells(v + y, 1) = q(y - 1)
Cells(v + y, 2) = Sheets(Z).Name
Next y

line1: 'jump out
Next Z
Sheets("dat").Select
v = Application.WorksheetFunction.CountA(Columns(1))
For x = 1 To v
Cells(x, 3) = Cells(x, 2) & ", " & Cells(x, 1)
Next x
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
It'd be nice if you could show us which line errors.

and please use code tags with proper indentation. Makes it easier to answer.

Edit: I found where it errors. Didn't realize you had a little comment in there saying error.
Hard to see without any indentations xD
 
Last edited:
Upvote 0
Code:
a = Application.WorksheetFunction.CountA(Columns(1))
b = Application.WorksheetFunction.CountA(Columns(2))

I'm guessing you're trying to get the last row in column A and column B?
If not, you would need to explain what the code does and what the sample worksheet looks like.
 
Upvote 0
My apologies all around. I have no idea how to use code tags. However, I did find the error (although I don't know why it causes it). If I change the second CountA to the column in the IF statement in the loop it works.

but while we are on the subject, how do I use code tags? I would certainly like to post more legibly. Thanks for your response.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,763
Members
452,940
Latest member
rootytrip

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