Check if a cell contains same digits

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I am looking for a way to verify if a cell say A1 contains same digits like 1111, 2222, etc.

The Len will always be 4.

Thanks in advance
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Probably there is lots for solution, I've got something like this:
[TABLE="class: head"]
<tbody>[TR]
[TH][/TH]
[TH]
A​
[/TH]
[TH]
B​
[/TH]
[/TR]
[TR]
[TD]
1
[/TD]
[TD]
1111​
[/TD]
[TD]
TRUE​
[/TD]
[/TR]
[TR]
[TD]
2
[/TD]
[TD]
1233​
[/TD]
[TD]
FALSE​
[/TD]
[/TR]
[TR]
[TD]
3
[/TD]
[TD]
2222​
[/TD]
[TD]
TRUE​
[/TD]
[/TR]
[TR]
[TD]
4
[/TD]
[TD]
2452​
[/TD]
[TD]
FALSE​
[/TD]
[/TR]
[TR]
[TD]
5
[/TD]
[TD]
8888​
[/TD]
[TD]
TRUE​
[/TD]
[/TR]
</tbody>[/TABLE]


[TABLE="class: head"]
<tbody>[TR]
[TH][/TH]
[TH]
A​
[/TH]
[TH]
B​
[/TH]
[/TR]
[TR]
[TD]
1
[/TD]
[TD]
1111​
[/TD]
[TD]
=SUM(CODE(MID(A1,ROW($1:$4),1)))/4=CODE(LEFT(A1,1))​
[/TD]
[/TR]
[TR]
[TD]
2
[/TD]
[TD]
1233​
[/TD]
[TD]
=SUM(CODE(MID(A2,ROW($1:$4),1)))/4=CODE(LEFT(A2,1))​
[/TD]
[/TR]
</tbody>[/TABLE]


There is array formula in B1 so have to be accept with Ctrl+Shift+Enter ({} should appear) and drag it down.
 
Last edited:
Upvote 0
You did not mention about VBA in original post or thread title?

Into sheet code:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    MsgBox Evaluate("=SUM(CODE(MID(" & Target.Value & ",ROW($1:$4),1)))/4=CODE(LEFT(" & Target.Value & ",1))")
End Sub
 
Last edited:
Upvote 0
Hi, something like this?

Code:
Option Explicit


Option Explicit


Sub Worksheet_Change(ByVal Target As Range)
Dim numberInQuestion As String, firstDigitInQuestion As Long, i As Long, nextDigitInQuestion As Long, isItANumber As Boolean
If Target.Address = "$A$1" Then
numberInQuestion = Range("A1").Value
isItANumber = IsNumeric(numberInQuestion)
firstDigitInQuestion = Left(numberInQuestion, 1)
If isItANumber = True Then
For i = 1 To Len(numberInQuestion)
nextDigitInQuestion = Mid(numberInQuestion, i, 1)
If firstDigitInQuestion <> nextDigitInQuestion Then
MsgBox "numbers are different"
End If
Next i
End If
End If
End Sub


Sub init()
Dim rng As Range
Set rng = Range("A1")
Call Worksheet_Change(rng)
End Sub
 
Last edited:
Upvote 0
Okay thanks but I need a VBA code for this.
Something like
Code:
MsgBox Replace(Range("A1").Value, Left(Range("A1").Value, 1), "") = vbNullString

or
Code:
MsgBox Range("A1").Text = String(4, Left(Range("A1").Text, 1))
 
Last edited:
Upvote 0
Cool cool!!! Thank you all

I have tested all the above. They are working fine.
@Peter_SSs
your last code is very cute. Thanks
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

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