# Check if a cell contains same digits

#### kelly mort

##### Well-known Member
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.

### Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the \$ sign).

#### KOKOSEK

##### Board Regular
Probably there is lots for solution, I've got something like this:
A​
B​
1
1111​
TRUE​
2
1233​
FALSE​
3
2222​
TRUE​
4
2452​
FALSE​
5
8888​
TRUE​

<tbody>
</tbody>

A​
B​
1
1111​
=SUM(CODE(MID(A1,ROW(\$1:\$4),1)))/4=CODE(LEFT(A1,1))​
2
1233​
=SUM(CODE(MID(A2,ROW(\$1:\$4),1)))/4=CODE(LEFT(A2,1))​

<tbody>
</tbody>

There is array formula in B1 so have to be accept with Ctrl+Shift+Enter ({} should appear) and drag it down.

Last edited:

#### kelly mort

##### Well-known Member
Okay thanks but I need a VBA code for this.

#### gaz_chops

##### Well-known Member
A non array formula

=SUBSTITUTE(A1,LEFT(A1,1),"")=""

#### Peter_SSs

##### MrExcel MVP, Moderator
Probably there is lots for solution,
True, here are a couple more.

Excel Workbook
ABC
11111TRUETRUE
21233FALSEFALSE
32222TRUETRUE
42452FALSEFALSE
58888TRUETRUE
Same digits

#### KOKOSEK

##### Board Regular
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:

#### Lavina

##### Board Regular
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
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:

#### Peter_SSs

##### MrExcel MVP, Moderator
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:

#### kelly mort

##### Well-known Member
Cool cool!!! Thank you all

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

#### Peter_SSs

##### MrExcel MVP, Moderator
You're welcome. Plenty of choice.

1,102,682
Messages
5,488,241
Members
407,632
Latest member
varunwalla

### This Week's Hot Topics

• Timer in VBA - Stop, Start, Pause and Reset
[CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
• how to updates multiple rows in muliselect listbox
Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
• Delete Row from Table
I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
• Assigning to a variable
I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
• Way to verify information
Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
• Active Cell Address – Inactive Sheet
How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...