Runtime Error 11: Divison by Zero

slven

New Member
Joined
Jan 19, 2009
Messages
6
I have a simple macro to loop through worksheets in a workbook and look for the last/largest ID number (Column A) in the entire workbook. This code works periodically when running and always works when stepping through in debug mode. BUT other times when running it (even in the same workbook), i get a Runtime Error 11 Division by Zero and for life of me Can't figure out why. There is no division by zero. And it seems to happen on different worksheets too before it fails.

The code keeps failing at the line in bold below:

Sub NextNumber()
'
' NextNumber Macro
' finds next number in the workbook
'
' Keyboard Shortcut: Option+Cmd+Shift+N
'

Dim vMax As Integer
Dim holder As Integer
Dim x As Integer
Dim y As Integer 'loop through worksheets
Dim ws_count As Integer 'count number of worksheets
Dim sh As Integer

sh = ActiveSheet.Index

vMax = 1
y = 1
x = 1
ws_count = ThisWorkbook.Worksheets.Count

vMax = Application.WorksheetFunction _
.Max(Range("A:A"))

Do While y <= ws_count
ActiveWorkbook.Sheets(y).Activate
holder = Application.WorksheetFunction.Max(Range("A:A"))

If IsNumeric(holder) = True And (holder < 10000) Then
If holder > vMax Then
vMax = holder
End If
End If

y = y + 1
Loop

MsgBox vMax + 1, , "Next Number"


End Sub
 
Last edited:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,215,242
Messages
6,123,830
Members
449,127
Latest member
Cyko

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