Simultaneous nummber

haseft

Active Member
Joined
Jun 10, 2014
Messages
321
Hi!

Value of range:
A1=1
A2=2
A3=4
A4=5
A5=7
A6=6
A7=3

I want to know if value of range A1:A7 is simultaneous? I mean if the value is 1,2,3,4,5,6,7.
In case above, cell A3 should be 3, and the macro must give a msg box "The cells are not in simultaneous".

Tanks for help.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
There's probably some pitfalls here , but on the basis that you are testing for consecutive numbers, so each number has the same count between each other number then with you numbers starting in "A1" try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG08Apr04
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Num [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] nstr [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    Num = Dn.Value - Dn.Offset(-1)
        [COLOR="Navy"]If[/COLOR] InStr(nstr, Num) = 0 [COLOR="Navy"]Then[/COLOR]
            nstr = nstr & IIf(nstr = "", Num, "," & Num)
        [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]If[/COLOR] UBound(Split(nstr, ",")) = 0 [COLOR="Navy"]Then[/COLOR]
    MsgBox "Numbers are consecutive"
[COLOR="Navy"]Else[/COLOR]
    MsgBox "Numbers are not consecutive"
 [COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
I get error with code
Num = Dn.Value - Dn.Offset(-1)
error "Incompatible"
Mick's code works fine for me. Does your vertical range of values start in cell A1?

I have never seen the error "Incompatible" in Excel before. Are you actually using Excel or some other Excel-type competitor product?

I was going to offer you this alternative macro, but if Mick's code does not work for you, I am not so sure this code will work either...
Code:
Sub ConsecutiveNumbers()
  Dim Diff As Long
  With Range("A2", Cells(Rows.Count, "A").End(xlUp))
    Diff = Evaluate("MEDIAN(" & .Address & "-" & .Offset(-1).Address & ")")
    If Evaluate("SUM(--((" & .Address & "-" & .Offset(-1).Address & ")<>" & Diff & "))") Then
      MsgBox "Numbers are not consecutive"
    Else
      MsgBox "Numbers are consecutive"
    End If
  End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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