Formatting cell with unique format while having different inputs

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
How and what is the best way to unify the inputs of a cell (ie "A2") : There will probably be three type of inputs
123456
req%123456
req000000123456
but I want to this cell ("A2") to turn these inputS to one single format REQ000000123456 (add REQ000000 to the last six digits of input)
I only know to formate 123456 in FORMAT CELLS / CUSTOM but no knowledge to tackle the other two !
Should I create a marco for this ?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
if you have textjoin function try this with CTRL+SHIFT+ENTER
Excel Formula:
=RIGHT(TEXTJOIN("",TRUE,IFERROR((MID(B2,ROW(INDIRECT("1:"&LEN(B2))),1)*1),"")),6)*1
if you have only one numbers at each text first add this UDF to your VBA
VBA Code:
Public Function ExtractNumbers(Data As Range)
Dim s As String, arr As Variant
s = Data.Value
With CreateObject("vbscript.regexp")
    .Pattern = "[^1-9]"
    .Global = True
    .IgnoreCase = True
    arr = Split(Application.Trim(.Replace(s, " ")), " ")
End With
ExtractNumbers = arr
End Function
Now you can use it as function with input:
Excel Formula:
=ExtractNumbers(B2)
B2 is Cells Address has your Data. You can change it.
Then GO to Number format and Change it to Custom and at type Section Write:
"Req0000000"@
 
Last edited:
Upvote 0
if you have textjoin function try this with CTRL+SHIFT+ENTER
Excel Formula:
=RIGHT(TEXTJOIN("",TRUE,IFERROR((MID(B2,ROW(INDIRECT("1:"&LEN(B2))),1)*1),"")),6)*1
if you have only one numbers at each text first add this UDF to your VBA
VBA Code:
Public Function ExtractNumbers(Data As Range)
Dim s As String, arr As Variant
s = Data.Value
With CreateObject("vbscript.regexp")
    .Pattern = "[^1-9]"
    .Global = True
    .IgnoreCase = True
    arr = Split(Application.Trim(.Replace(s, " ")), " ")
End With
ExtractNumbers = arr
End Function
Now you can use it as function with input:
Excel Formula:
=ExtractNumbers(B2)
B2 is Cells Address has your Data. You can change it.
Then GO to Number format and Change it to Custom and at type Section Write:
"Req0000000"@
Hi Maabadi,
Thanks. This works. Is there any way that the marco be applied to the input cell A2 directly without involving any other cells (create a shortcut key to run the marco in A2) !
 
Upvote 0
I think you want Macro to Replaced Data at Column A (your Data Started from Cell A2). Is it Correct?
If yes. Add This Macro To VBA and Add ShortCut key for it. This one Convert all Data at Column A to your Format.
For add Shotcut Key Go to View (Or Developer) Tab , Select Macros Then ExtractNumbers2 Now Select Option & Add ShortCut Key you want.
VBA Code:
Sub ExtractNumbers2()
' Keyboard Shortcut: Ctrl+Shift+L
Dim s As String, arr As Variant, Sr As Range, Lr As Long, Cell As Range
Lr = Range("A" & Rows.Count).End(xlUp).Row
Set Sr = Range("A2:A" & Lr)
For Each Cell In Sr
s = Cell.Value
With CreateObject("vbscript.regexp")
    .Pattern = "[^1-9]"
    .Global = True
    .IgnoreCase = True
    arr = Split(Application.Trim(.Replace(s, " ")), " ")
End With
Cell.Value = arr
Cell.Value = Cell.Value * 1
Next Cell
Sr.NumberFormat = """Req0000000""General"
End Sub
 
Upvote 0
I think you want Macro to Replaced Data at Column A (your Data Started from Cell A2). Is it Correct?
If yes. Add This Macro To VBA and Add ShortCut key for it. This one Convert all Data at Column A to your Format.
For add Shotcut Key Go to View (Or Developer) Tab , Select Macros Then ExtractNumbers2 Now Select Option & Add ShortCut Key you want.
VBA Code:
Sub ExtractNumbers2()
' Keyboard Shortcut: Ctrl+Shift+L
Dim s As String, arr As Variant, Sr As Range, Lr As Long, Cell As Range
Lr = Range("A" & Rows.Count).End(xlUp).Row
Set Sr = Range("A2:A" & Lr)
For Each Cell In Sr
s = Cell.Value
With CreateObject("vbscript.regexp")
    .Pattern = "[^1-9]"
    .Global = True
    .IgnoreCase = True
    arr = Split(Application.Trim(.Replace(s, " ")), " ")
End With
Cell.Value = arr
Cell.Value = Cell.Value * 1
Next Cell
Sr.NumberFormat = """Req0000000""General"
End Sub
Thanks Maabadi,
It works. Should I create a WORKSHEET_CALCULATE, WORKSHEET_SELECTIONCHANGE OR WORKSHEET_CHANGE to column A except cell A1 to trigger it whenever I input the cells !
 
Upvote 0
if you want worksheet Change event:
Right-Click on Sheet name at excel window and Paste this code:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long, Lr As Long, s As String, arr As Variant
Dim Lr3 As Long, Lr4 As Long, K As Long
Lr = Range("A" & Rows.Count).End(xlUp).Row
  If Intersect(Target, Range("A2:A" & Lr)) Is Nothing Then Exit Sub
   If Target.Count > 1 Then Exit Sub
   Application.EnableEvents = False
    s = Target.Value
     With CreateObject("vbscript.regexp")
      .Pattern = "[^1-9]"
      .Global = True
      .IgnoreCase = True
      arr = Split(Application.Trim(.Replace(s, " ")), " ")
     End With
    Target.Value = arr
    Target.Value = Target.Value * 1
    Target.NumberFormat = """Req0000000""General"
   Application.EnableEvents = True
End Sub
 
Upvote 0
Solution
if you want worksheet Change event:
Right-Click on Sheet name at excel window and Paste this code:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long, Lr As Long, s As String, arr As Variant
Dim Lr3 As Long, Lr4 As Long, K As Long
Lr = Range("A" & Rows.Count).End(xlUp).Row
  If Intersect(Target, Range("A2:A" & Lr)) Is Nothing Then Exit Sub
   If Target.Count > 1 Then Exit Sub
   Application.EnableEvents = False
    s = Target.Value
     With CreateObject("vbscript.regexp")
      .Pattern = "[^1-9]"
      .Global = True
      .IgnoreCase = True
      arr = Split(Application.Trim(.Replace(s, " ")), " ")
     End With
    Target.Value = arr
    Target.Value = Target.Value * 1
    Target.NumberFormat = """Req0000000""General"
   Application.EnableEvents = True
End Sub
Thank for your guidance. Your kindness and help save me a lot of work.
 
Upvote 0
You're Welcome & Thanks for Feedback.
Hi Maabadi,
I found bug that when I delete a wrong input, the cell shows "REQ000000" before I enter another input. I cannot leave it blank any more. I tried to add an ELSE statement :
ELSE
if s="" then
Target.NumberFormat = """General"

but not work.
Please help
 
Upvote 0
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long, Lr As Long, s As String, arr As Variant
Dim Lr3 As Long, Lr4 As Long, K As Long
Lr = Range("A" & Rows.Count).End(xlUp).Row
  If Intersect(Target, Range("A2:A" & Lr)) Is Nothing Then Exit Sub
   If Target.Count > 1 Then Exit Sub
   Application.EnableEvents = False
    s = Target.Value
if s="" Then 
    Target.NumberFormat = "General"
Else
     With CreateObject("vbscript.regexp")
      .Pattern = "[^1-9]"
      .Global = True
      .IgnoreCase = True
      arr = Split(Application.Trim(.Replace(s, " ")), " ")
     End With
    Target.Value = arr
    Target.Value = Target.Value * 1
    Target.NumberFormat = """Req0000000""General"
End if
   Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,360
Messages
6,124,493
Members
449,166
Latest member
hokjock

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