Sheet VBA to Auto-change Certain Values

hajiali

Active Member
Joined
Sep 8, 2018
Messages
488
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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Jeffrey Mahoney

Well-known Member
Joined
May 31, 2015
Messages
2,078
Office Version
  1. 365
Platform
  1. Windows
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
 

hajiali

Active Member
Joined
Sep 8, 2018
Messages
488
Office Version
  1. 2016
Platform
  1. Windows
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.
 

Jeffrey Mahoney

Well-known Member
Joined
May 31, 2015
Messages
2,078
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

hajiali

Active Member
Joined
Sep 8, 2018
Messages
488
Office Version
  1. 2016
Platform
  1. Windows
Thank you so much that works like magic. I really appreciate the quick response and replies.
 

Forum statistics

Threads
1,175,487
Messages
5,897,690
Members
434,673
Latest member
kktfc

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
Top