Check if a cell contains same digits

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,728
Office Version
2016
Platform
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
 

Some videos you may like

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

KOKOSEK

Board Regular
Joined
Apr 8, 2019
Messages
206
Office Version
365, 2013
Platform
Windows
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
Joined
Apr 10, 2017
Messages
1,728
Office Version
2016
Platform
Windows
Okay thanks but I need a VBA code for this.
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
43,440
Office Version
365
Platform
Windows
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
Joined
Apr 8, 2019
Messages
206
Office Version
365, 2013
Platform
Windows
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
Joined
Dec 18, 2018
Messages
75
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:

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
43,440
Office Version
365
Platform
Windows
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
Joined
Apr 10, 2017
Messages
1,728
Office Version
2016
Platform
Windows
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
Joined
May 28, 2005
Messages
43,440
Office Version
365
Platform
Windows
You're welcome. Plenty of choice. :)
 

Forum statistics

Threads
1,089,315
Messages
5,407,527
Members
403,151
Latest member
floydschoice

This Week's Hot Topics

  • help please
    SORRY NOT ANY GOOD AT EXCEL SO HELP WOULD BE MUCH APPRECIATED this formula is in a sheet called ignore...
  • two formulas needed
    Hello, I'll try my best to explain this: First formula needed in Sheet1 cell A2: If Sheet1 cell B2 = Sheet2 cell B2 then return a 1. If not then...
  • Dynamic Counts
    Good afternoon, we are tidying up some data & the data seems to be growing quicker than we are tidying it up! What we confirm (by reviewing it...
  • Help Excel formula eliminate duplicate values and keep only 2 identical rows.
    as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9...
  • Macro Compile Error Sub or Function not defined
    Hello, I am trying to run macros from a validation list, all macros have been created and run perfectly on there own but I'm getting a compile...
  • Last row combined with Current Region VBA
    I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data...
Top