VBA text change

jaden210

New Member
Joined
Jun 6, 2012
Messages
34
Hey, I'm looking for some VBA help. preferably an add-in. I have a string of numbers in a cell ex. 2900321543253 that I need changed. the first three characters need to be changed to "978" and then the last character needs to be x+1 so in this case 4. I'm having a hard time getting the function to do all this, and any help will be appreciated.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try this (with the cell selected):

Code:
Sub ConvertValues()

ActiveCell.Value = "978" & Mid(ActiveCell, 4, 9) & Right(ActiveCell, 1) + 1

End Sub
 
Upvote 0
Code:
Function MyFunc(rng As Range) As String

    MyFunc = "978" & Mid(rng.Value + 1, 4)
    
End Function

If the value ends in say 59, this will make it end in 60. I wasn't sure if you wanted it to end in 60 or 50 when you add 1 to 9
 
Last edited:
Upvote 0
Jaden,

Alpha's post begs an important question: When the value of the last digit changes from a "9" to a "0", should then the value of the previous digit increase by 1, as well? If so, then the code I suggested will not provide an accurate result.

Edit: Just saw Alpha's follow-up which basically asks the same question ... I'd say great minds think alike, but I'm afraid I'm out of my league here. ;)
 
Upvote 0
sorry for not being more clear with that.. if the number ends in 49 it should go back to 40 and not up to 50.
 
Upvote 0
I have a feeling this can be shortened some, but I have to leave and have no time to investigate it now. However, using AlphaFrog's setup, this will do what you asked for...

Code:
Function MyFunc(rng As Range) As String
  MyFunc = "978" & Mid(Left(rng, Len(rng) - 1) & ((Right(rng, 1) + 1) Mod 10), 4)
End Function
 
Upvote 0
Try

Code:
Public Function Converter(x As Variant)
x = 978 & Mid(x, 4, Len(x))
x = Left(x, Len(x) - 1) + Right(Right(x, 1) + 1, 1)
Converter = x
End Function

then use in a cell like
=CONVERTER(A1)
 
Upvote 0
sorry for not being more clear with that.. if the number ends in 49 it should go back to 40 and not up to 50.

Code:
[color=darkblue]Function[/color] MyFunc(rng [color=darkblue]As[/color] Range) As [color=darkblue]String[/color]

    MyFunc = "978" & Mid(rng.Value + IIf(Right(rng.Value, 1) = "9", -9, 1), 4)

[color=darkblue]End[/color] [color=darkblue]Function[/color]
 
Upvote 0
This is perfect! Is there any way I could possibly use an if then or something so that if the data already starts with 978 it will leave it unaffected? (wont add 1 to the last digit)
 
Upvote 0

Forum statistics

Threads
1,203,668
Messages
6,056,653
Members
444,880
Latest member
Kinger1968

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