Find cells that begin with a number, and add text to them

mlarson

Well-known Member
Joined
Aug 25, 2011
Messages
509
Office Version
  1. 2010
Platform
  1. Windows
Hi there, hoping you guys can help me with this. Thanks in advance for your help!

I would like for any cell in my spreadsheet that begins with a number to have XXX added to the beginning of that cell. For example...
123abc becomes XXX123abc
abc stays abc
4idiayd becomes XXX4idiayd
98a72ksh becomes XXX98a72ksh
cheeseburger stays cheeseburger

Hopefully I explained this clearly. Thanks!
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Let me know if that works for you.
VBA Code:
Sub ConcatXXXtoValueIfFirstCharacterNumeric()
    Dim sheet as Worksheet: Set sheet = ActiveSheet
    Dim arr as Variant: arr = sheet.UsedRange.value

    Dim rowNum as Long
    Dim colNum as Integer

    For rowNum = LBound(arr, 1) to UBound(arr, 1)
        For colNum = LBound(arr, 2) to UBound(arr, 2)
            If IsNumeric(Left(arr(rowNum, colNum), 1)) Then
                arr(rowNum, colNum) = “XXX” & arr(rowNum, colNum)
            End If
        Next colNum
    Next rowNum
    sheet.UsedRange.value = arr
End Sub
 
Upvote 0
Thanks so much, very helpful! As an aside, I wonder if there's a way to do it without VBA?
 
Upvote 0
Many columns and rows throughout the sheet.
 
Upvote 0
A workaround that would be useful is if there is a formula in C5 that would essentially say "if the first character of A5 is a number, return XXX in C5, if not, return A5 in C5" - if there is a way to do that formula I could make it work for what I'm trying to accomplish. Thanks!
 
Upvote 0
A workaround that would be useful is if there is a formula in C5 that would essentially say "if the first character of A5 is a number, return XXX in C5, if not, return A5 in C5" - if there is a way to do that formula I could make it work for what I'm trying to accomplish. Thanks!
Oh, that's simpler than what I was trying to do.
=IF(ISNUMBER(LEFT(A5,1)),"XXX" & A5,A5)
 
Upvote 0

Forum statistics

Threads
1,207,013
Messages
6,076,149
Members
446,187
Latest member
LMill

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