aayaanmayank

Board Regular
Joined
Jul 20, 2018
Messages
157
Hi Can anyone advise why do we get overflow error and how to get rid off it.

sub mu()

Set shgroup = ThisWorkbook.Worksheets("Grouping Data_DQ")
lastrow1 = shgroup.Range("B" & Rows.Count).End(xlUp).Row



Range("I2").Select
ActiveCell.Interior.Color = vbYellow
For U = 2 To lastrow1
Set MYNAME2 = Cells(U, "I")
Set MYNAME3 = Cells((U + 1), ("I"))
If MYNAME2.Value = MYNAME3 Then
shgroup.Cells(U, "I").Interior.Color = vbGreen
shgroup.Cells(U + 1, "I").Interior.Color = vbGreen
Else
shgroup.Cells(U + 1, "I").Interior.Color = vbYellow
End If
Next U

end sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Your code runs prefectly well on my computer, but it may be somethign to do with how many rows you have, I have rewritten is useing varainta rays which will make it as bit faster and might solve your problem
Note another possible cause of the problem is that you set lastrow1 from the Grouping Data_DQ sheet, but then you operate all the formatting on the active sheet. do you know for sure that the active sheet and the grouping data have the same number of rows?

Code:
Sub mu()


Set shgroup = ThisWorkbook.Worksheets("Grouping Data_DQ")
lastrow1 = shgroup.Range("B" & Rows.Count).End(xlUp).Row


Varray = Range(Cells(1, 9), cell(lastrow1, 9))


Range("I2").Select
ActiveCell.Interior.Color = vbYellow
For U = 2 To lastrow1
MYNAME2 = Varray(U, 1)
MYNAME3 = Varray((U + 1), 1)
If MYNAME2 = MYNAME3 Then
shgroup.Cells(U, "I").Interior.Color = vbGreen
shgroup.Cells(U + 1, "I").Interior.Color = vbGreen
Else
shgroup.Cells(U + 1, "I").Interior.Color = vbYellow
End If
Next U


End Sub
 
Upvote 0
Hi All this is happening in active sheet. now m getting another error MYNAME3 = Varray((U + 1), 1) subscript out of range
 
Last edited:
Upvote 0
the avoid overflow change this line
Code:
Varray = Range(Cells(1, 9), cell(lastrow1, 9))
to

Code:
Varray = Range(Cells(1, 9), cell(lastrow1+1, 9))
The code does exactly the same check as your code and it shouldn't be finding partial matches, if it is really doing that you need to use debug to find out what is going on.
 
Last edited:
Upvote 0
Also by default it is doing Green color in the lastrow/ lastcell ...pls advice
What do you want to happen on the last row? Your check is checking against the next row which in the case of the last row will be blank?
 
Upvote 0
Does this do what you want:
Code:
Sub mu()

Set shgroup = ThisWorkbook.Worksheets("Grouping Data_DQ")
lastrow1 = shgroup.Range("B" & Rows.Count).End(xlUp).Row


Varray = Range(Cells(1, 9), Cells(lastrow1 + 1, 9))
shgroup.Range(Cells(2, 9), Cells(lastrow1, 9)).Interior.Color = vbYellow


For U = 2 To lastrow1
MYNAME2 = Varray(U, 1)
MYNAME3 = Varray((U + 1), 1)
If MYNAME2 = MYNAME3 Then
shgroup.Cells(U, "I").Interior.Color = vbGreen
shgroup.Cells(U + 1, "I").Interior.Color = vbGreen
End If
Next U


End Sub
Note: by coding it this way it should be much faster because the checks for equality are done in memory, I format the whole column to yellow which is very fast and then I only have reformat those cells that are equal. This is much faster that you orginal way of loading each cell in turn (twice) and formatting each cell individually)

Has it fixed the overflowe problem??
 
Last edited:
Upvote 0
Yes now its fine...Need a bit more favor that instead of 100% match, if 80% string matches it sud throws green color...how to do tht
 
Upvote 0
Can you please advise, what changes i need to make so it matched only 80% text and then green
Sub mu()

Set shgroup = ThisWorkbook.Worksheets("Grouping Data_DQ")
lastrow1 = shgroup.Range("B" & Rows.Count).End(xlUp).Row


Varray = Range(Cells(1, 9), Cells(lastrow1 + 1, 9))
shgroup.Range(Cells(2, 9), Cells(lastrow1, 9)).Interior.Color = vbYellow


For U = 2 To lastrow1
MYNAME2 = Varray(U, 1)
MYNAME3 = Varray((U + 1), 1)
If MYNAME2 = MYNAME3 Then
shgroup.Cells(U, "I").Interior.Color = vbGreen
shgroup.Cells(U + 1, "I").Interior.Color = vbGreen
End If
Next U


End Sub
 
Last edited:
Upvote 0
What do you mean by "80% match" . How are you going to check for that? Computers are not very easy to program to do partial matches, 100% match is very easy it is either a total match or not. I suggest if you want some inpuit on how to do a partial mathc you start another thread because it is a completely different problem
 
Upvote 0

Forum statistics

Threads
1,214,645
Messages
6,120,711
Members
448,984
Latest member
foxpro

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