Format Cell Value to Ten Characters

Gos-C

Active Member
Joined
Apr 11, 2005
Messages
258
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi all,

I have the following code which formats the cell values in column A to 10 characters on entry:

Code:
Sub FormatToTenCharacters()
   Dim Cell As Range, LR As Long
   LR = Range("A" & Rows.Count).End(xlUp).Row
   For Each Cell In Range("A1:A" & LR)
        If Len(Cell.Value) < 10 Then Cell.Value = "'" & Application.Rept("0", 10 - Len(Cell.Value)) & UCase(Cell.Value)
    Next Cell
End Sub

For example:

Enter excel in A1 and it changes to '00000EXCEL
Enter 123456 in A2 and it changes to '0000123456
Enter abc456 in A3 and it changes to '0000ABC456

I want to format it as text so that it does not have the apostrophe ('). Any help?

Thank you,
Gos-C
 
It works after I also added the UserInterfaceOnly line.

Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    If Target.Column <> 11 Then Exit Sub 'only works in Columns K
        If Target.Count > 1 Then Exit Sub
            If Target <> "" Then
    [COLOR="Blue"]ActiveSheet.Protect Password:="test", UserInterfaceOnly:=True[/COLOR]
    Target.NumberFormat = "@"
        If Len(Target.Value) <= 10 Then Target.Value = Application.Rept("0", 10 - Len(Target.Value)) & UCase(Target.Value)
    End If
Application.EnableEvents = True
End Sub

Thank you very much for your help, mikerickson.

Gos-C
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Forum statistics

Threads
1,215,692
Messages
6,126,228
Members
449,303
Latest member
grantrob

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