highlight consecutive series of four numbers vba

S

smandula

I would like to highlight all four consecutive sequential
numbers in range of C2:V25

1 2 3 4 11 23 36 42
25 26 33 34 35 36 40 45

1 2 3 4 and 33 34 35 36 will be highlighted only
any color.

With Thanks
 
J

JLGWhiz

See if this will do it. This assumes that the sequence is horizontal in
rows rather than in columns.

Sub fh()
Dim c As Range
x = 1
For Each c In Range("C2:V25")
If c.Offset(, 1).Value - c.Value = 1 Then
If x = 1 Then
rng = c.Address
End If
x = x + 1
If x = 4 Then
Range(rng, c.Offset(, 1)).Interior.ColorIndex = 6
x = 1
End If
Else
x = 1
End If
Next
End Sub
 
M

Mike H

Hi,

Will any of the ranges overlap e.g. 6 (or more) consecutive numbers

123456

will any consecutive numbers wrap around 2 rows i.e. the last 2 rows on 1
line and the first 2 on the next be a consecutive series?

Mike
 
S

smandula

Thanks for replying.

There will be no wrap, just straight 4 consecutive numbers.

The solution provided works extremely well!

I tried individual if statements, but the colors and
numbers splattered all over the given range. This solution
cuts only the required 4 consecutive cells

Thanks to everyone providing and reading this post
 

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