For Each loop with Dim as Long - Run-time error '6' Overflow

kjaer

New Member
Joined
Jul 24, 2013
Messages
8
Hi all

I'm struggling a bit with a Run-time error '6' Overflow.

My code is only able to handle 32.767 cells so I assume the reason is a default Dim As Integer. If so I guees I only need to have Dim changed to Long but for some reason I can figure out how to do this in my For Each loop. And as the macro works fine besides this limitation it would be nice if there is an easy way to do add a Dim As Long.

This is relevant part of my code:

Dim myMonthRng As Range

Cells.Find(What:="Antal Måneder", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Offset(1, 0).Select

Set myMonthRng = Range(ActiveCell, Selection.End(xlDown))

For Each cell In myMonthRng
If cell.Value >= 2 Then
cell.EntireRow.Interior.ColorIndex = 4
Else
cell.EntireRow.Interior.ColorIndex = xlNone
End If
Next

For Each cell In myMonthRng
If cell.Value >= "Ny Sag" Then
cell.EntireRow.Interior.ColorIndex = xlNone
End If
Next

Hopefully one of you will know a simple way :)?

Thanks,
Martin
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Got it to work with this code instead:

Dim myMonthRng As Range

Cells.Find(What:="Antal Måneder", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Offset(1, 0).Select

Set myMonthRng = Range(ActiveCell, Selection.End(xlDown))

myCells = myMonthRng.Count

Dim i As Long

Cells.Find(What:="Antal Måneder", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Offset(1, 0).Select

For i = 1 To myCells
If ActiveCell.Value >= 2 Then
ActiveCell.EntireRow.Interior.ColorIndex = 4
If ActiveCell.Value = "Ny Sag" Then
ActiveCell.EntireRow.Interior.ColorIndex = xlNone
End If
ActiveCell.Offset(1, 0).Select

Else
ActiveCell.EntireRow.Interior.ColorIndex = xlNone
ActiveCell.Offset(1, 0).Select
End If
Next i
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,213
Members
448,554
Latest member
Gleisner2

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