Custom Number Formats

Joined
Oct 29, 2015
Messages
30
Office Version
  1. 365
Platform
  1. Windows
HI,

I have some cells in excel I would like to format as per the following typical entries I will be making

1 / A
1 / B
etc...
1 / 1 / A
1 / 1 / B
1 / 1 / C
etc...
1 / 1 / 1 A
1 / 1 / 1 B
1 / 1 / 1 C

Etc.

The numerical data is based on sections or subsections and there may be only one or more than one subsection at any time. The letter reference refers to the unique item on that page.

I can get as far as “# "/" # "/"” in the custom number format but I cannot get any further as it does not give me any flexibility with the other scenarios. Does anybody have any suggestions?
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I'm assuming you want to enter them as 1A, 111A, etc. I think you will need VBA for this.
I have also added the functionality of making the letters uppercase so that you don't have to enter them that way.
This will actually change the value to be in that format.

This works for Column A, change as necessary.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range, d As Range, x As Long
Set d = Intersect(Target, Range("A:A"))
If d Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each c In d
    c = Subsection(c.Text)
Next
Application.EnableEvents = True
End Sub

Function Subsection(r As String) As String
Dim x As Long, s As String
For x = 1 To Len(r)
    s = s & Mid(r, x, 1) & " / "
Next
If Len(s) > 3 Then Subsection = UCase(Left(s, Len(s) - 3))
End Function
 
Last edited:
Upvote 0
I'm assuming you want to enter them as 1A, 111A, etc. I think you will need VBA for this.
I have also added the functionality of making the letters uppercase so that you don't have to enter them that way.
This will actually change the value to be in that format.

This works for Column A, change as necessary.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range, d As Range, x As Long
Set d = Intersect(Target, Range("A:A"))
If d Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each c In d
    c = Subsection(c.Text)
Next
Application.EnableEvents = True
End Sub

Function Subsection(r As String) As String
Dim x As Long, s As String
For x = 1 To Len(r)
    s = s & Mid(r, x, 1) & " / "
Next
If Len(s) > 3 Then Subsection = UCase(Left(s, Len(s) - 3))
End Function
If the OP is interested, there is a one-liner function available for Scott's Subsection function...
Code:
[table="width: 500"]
[tr]
	[td]Function Subsection(S As String) As String
  Subsection = UCase(Replace(Trim(Replace(StrConv(S, vbUnicode), Chr(0), " ")), " ", " / "))
End Function[/td]
[/tr]
[/table]
And since it is a one-liner, the Subsection function could be eliminated entirely and this single line of code could be used directly in the For Each loop of Scott's Change event procedure instead. For example...
Code:
[table="width: 500"]
[tr]
	[td]Private Sub Worksheet_Change(ByVal Target As Range)
  Dim c As Range, d As Range, x As Long
  Set d = Intersect(Target, Range("A:A"))
  If d Is Nothing Then Exit Sub
  Application.EnableEvents = False
  For Each c In d
    c = UCase(Replace(Trim(Replace(StrConv(c.Text, vbUnicode), Chr(0), " ")), " ", " / "))
  Next
  Application.EnableEvents = True
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
That is very interesting. That could come in handy for all sorts of things. :)
 
Upvote 0

Forum statistics

Threads
1,215,013
Messages
6,122,694
Members
449,092
Latest member
snoom82

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