VBA to adjust number value in a range

Marklarbear

Board Regular
Joined
Nov 6, 2003
Messages
112
Office Version
  1. 365
Platform
  1. Windows
Hi Brains Trust

I'm after 2 VBA codes that will do the following in a range of numbers:

VBA 1 - for any value that is 4 characters long, remove the first 2 characters and replace with '9'. ie in cell A2 it will change the value from 9008 to 908, in cell D5 it will change the value from 2399 to 999 and so on.

VBA 2 - for any value that is 2 characters long, add '1' to the start. ie in cell A7 it will change the value from 97 to 197, in cell C4 it will change the value from 32 to 132 and so on.


The end result will be that every cell in the range will have a 3 digit number in it (that will be greater than 100 and less than 1000).



1686284487621.png



As always - any help is greatly appreciated...
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
There's a number of ways you could do this, here's just a couple. Run the code with the sheet active & adjust the range to suit.
With Evaluate
VBA Code:
Sub RoundNumsEvaluate()
    Dim rng As Range
    Set rng = Range("A1:D12")
    
    With rng
        .Value = Evaluate("IF(LEN(" & .Address & ")=4,""9""&RIGHT(" & .Address & ",2),IF(LEN(" & .Address & ")=2,""1""&RIGHT(" & .Address & ",2)," & .Address & "))")
    End With
End Sub

With arrays
VBA Code:
Option Explicit
Sub RoundNumsArray()
    Dim a, b, i As Long, j As Long
    a = Range("A1:D12")
    ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
    
    For i = 1 To UBound(a, 1)
        For j = 1 To UBound(a, 2)
            If Len(CStr(a(i, j))) >= 4 Then
                b(i, j) = "9" & Right(a(i, j), 2)
            ElseIf Len(CStr(a(i, j))) = 2 Then
                b(i, j) = "1" & Right(a(i, j), 2)
            Else
                b(i, j) = a(i, j)
            End If
        Next j
    Next i
    
    Range("A1").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub
 
Upvote 0
Solution
There's a number of ways you could do this, here's just a couple. Run the code with the sheet active & adjust the range to suit.
With Evaluate
VBA Code:
Sub RoundNumsEvaluate()
    Dim rng As Range
    Set rng = Range("A1:D12")
   
    With rng
        .Value = Evaluate("IF(LEN(" & .Address & ")=4,""9""&RIGHT(" & .Address & ",2),IF(LEN(" & .Address & ")=2,""1""&RIGHT(" & .Address & ",2)," & .Address & "))")
    End With
End Sub

With arrays
VBA Code:
Option Explicit
Sub RoundNumsArray()
    Dim a, b, i As Long, j As Long
    a = Range("A1:D12")
    ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
   
    For i = 1 To UBound(a, 1)
        For j = 1 To UBound(a, 2)
            If Len(CStr(a(i, j))) >= 4 Then
                b(i, j) = "9" & Right(a(i, j), 2)
            ElseIf Len(CStr(a(i, j))) = 2 Then
                b(i, j) = "1" & Right(a(i, j), 2)
            Else
                b(i, j) = a(i, j)
            End If
        Next j
    Next i
   
    Range("A1").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub
Cheers Kevin - i went with the Array option - your legend :)
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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