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 ?
 
It shown error when I input prefixbefore the numbers. Need to change the script .Pattern = "[^1-9]"
1619748062579.png
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
tried to change it to CONCATENATE("req"&"[^1-9]") but not workable
 
Upvote 0
What is data validation restriction. It isn't related to macro?
 
Upvote 0
Hi Maabadi,
If I just want the code applies to the dynamic range in column A not the whole column, what is the change ?
 
Upvote 0
This is applied on Dynamic range. From A2 ro last row has data at column A. Not Total Column A.
 
Upvote 0
This is applied on Dynamic range. From A2 ro last row has data at column A. Not Total Column A.
Hi maabadi,
There is error. When I input something outside the dynamic column. The debug shows TARGET. VALUE=TARGET.VALUE*1. And when I go into dynamic column and input something like req%112233, it won't convert it to REQ000000112233.
error2.png
errorinsheet.png
 
Upvote 0
Change
VBA Code:
Lr = Range("A" & Rows.Count).End(xlUp).Row
To
VBA Code:
Lr = Range("A2").End(xldown).Row
 
Upvote 0
Hi maabadi,
Having several tests, I found this code is more workable and seems compatiable with other functions in WORKBOOK_SHEETCHANGE. But how to modify the code applies to the dynamic range in column A not the whole column ? I attach my file for easy debug purpose. Please help !
problemed file

VBA Code:
If Target.Column = 1 Then
    Dim s As String
    Dim arr As Variant

    s = Target.Value
    If s = "" Then
      Target.NumberFormat = "General"
    Else
      With CreateObject("vbscript.regexp")
        .Pattern = "[^0-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
  End If
 
Upvote 0
Try this:
VBA Code:
   Dim Lr As Long
   Lr = Range("A" & Rows.Count).End(xlUp).Row
If Interect(Target, Range("A2:A" & Lr)) Is Nothing Then Exit Sub
    Dim s As String
    Dim arr As Variant

    s = Target.Value
    If s = "" Then
      Target.NumberFormat = "General"
    Else
      With CreateObject("vbscript.regexp")
        .Pattern = "[^0-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
 
Upvote 0

Forum statistics

Threads
1,214,620
Messages
6,120,559
Members
448,970
Latest member
kennimack

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