Sheet VBA to Auto-change Certain Values

hajiali

Well-known Member
Joined
Sep 8, 2018
Messages
623
Office Version
  1. 2016
Platform
  1. Windows
Hello Everyone.

This might be simple but can not figure out. I need a VBA to Automatically change certain Values.

Ex. in Range ("A1:A1000, D1,D1000, F1,F1000)

to automatically change the following values after its type.

1 to 01
1A to 01A
1B to 01B
1C to 01C

same for all Single Digit Numbers 2-9 add a 0 in front as shown above.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
This code needs to be placed in the SHEET module level of VBA; for the sheet you want to watch. I created a named range on my sheet called MyWatchRange; you can name it anything. This adds a leading zero if the first character of the cell value is a number other than zero.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim i As Range
  Dim Cel As Range
  Dim Astr As String
  
  Set i = Intersect(Target, Range("MyWatchRange"))
  If Not i Is Nothing Then
    Application.EnableEvents = False
    For Each Cel In i
      Astr = Cel.Value
      If InStr("123456789", Left(Astr, 1)) > 0 Then
        
        Cel.Value = "0" & Astr
      End If
    Next Cel
    Application.EnableEvents = True
  End If
  
End Sub

1638569043403.png
 
Upvote 0
Thanks Jeffrey auto change value part works great. However when I attempt to delete a value in the range it puts double zeros "00" in the cells that the value is being deleted.
 
Upvote 0
I would not have expected that. The red text below was changed
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim i As Range
  Dim Cel As Range
  Dim Astr As String
 
  Set i = Intersect(Target, Range("MyWatchRange"))
  If Not i Is Nothing Then
    Application.EnableEvents = False
    For Each Cel In i
      Astr = Cel.Value
      If Astr <> "" And InStr("123456789", Left(Astr, 1)) > 0 Then
       
        Cel.Value = "0" & Astr
      End If
    Next Cel
    Application.EnableEvents = True
  End If
 
End Sub
 
Upvote 0
Solution
Thank you so much that works like magic. I really appreciate the quick response and replies.
 
Upvote 0

Forum statistics

Threads
1,213,567
Messages
6,114,344
Members
448,570
Latest member
rik81h

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