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.

Thanks in advance
 

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:

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
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
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
 

Some videos you may like

This Week's Hot Topics

  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • find many based on a certain criteria
    good evening, I hope someone can help me? I have a workbook sheet 2 contains lots of data.... I would like to be able to find anything on sheet...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
  • Text Format
    I have a sheet for user to keyin the data. The format of the data can be 451 / 1903, 0012 / 9908 or 00287 / 0099. The number after the "/" is...
  • Macro to copy values across rows and transposing them and add the user id
    [FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]Hi,[/COLOR][/SIZE][/FONT] [FONT=Times New...
Top