Increment each of the character in a string by 1 [VBA]

x0nar

New Member
Joined
May 10, 2016
Messages
34
I'd like to increment each of the character within a string by 1

So for eg. ABCD101 becomes BCDE212

The nearest I've got is using char combined by code function
ie.
=CHAR(CODE("cellreference")+1).

I would really appreciate if the function would be a User Defined Function [UDF] in VBA.

Thanks for your help
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Here's my suggestion:
Code:
Function NewText(MyText As String) As String

Dim i As Integer
Dim Characters As String
Dim Ch As String
Dim NextCh As Integer


Characters = "ABCDEFGHIJKLMNOPQRSTUVWXZÅÄÖA01234567890"
Characters = Characters & LCase(Characters)


For i = 1 To Len(MyText)
    Ch = Mid(MyText, i, 1)
    NextCh = InStr(Characters, Ch) + 1
    
    If NextCh > 1 Then 'Found the character in Characters string
        Ch = Mid(Characters, NextCh, 1)
    End If
    NewText = NewText & Ch
Next i


End Function
 
Upvote 0
Here's my suggestion:
Code:
Function NewText(MyText As String) As String

Dim i As Integer
Dim Characters As String
Dim Ch As String
Dim NextCh As Integer


Characters = "ABCDEFGHIJKLMNOPQRSTUVWXZÅÄÖA01234567890"
Characters = Characters & LCase(Characters)


For i = 1 To Len(MyText)
    Ch = Mid(MyText, i, 1)
    NextCh = InStr(Characters, Ch) + 1
    
    If NextCh > 1 Then 'Found the character in Characters string
        Ch = Mid(Characters, NextCh, 1)
    End If
    NewText = NewText & Ch
Next i


End Function



Thanks. That worked.
From what I gather you have defined an array of characters. And then you search that within the that array and ask it to shift one step further. So if I want to include symbols such as @ or _ or . would it work if I input them in defined array of yours ?
Thanks again.
 
Upvote 0
This is another option
Code:
Function NC(MyStr As String) As String

    Dim cnt As Long
    Dim Ch As String

    For cnt = 1 To Len(MyStr)
        Ch = Ch & Chr(Asc(Mid(MyStr, cnt, 1)) + 1)
    Next cnt
    NC = Ch

End Function
It will increase the ascii value of each character by 1
 
Upvote 0
Go ahead and try. It will work. I left them out because I don't know what is the desired / correct order of those special characters. And there are even some foreign alphabets missing from my list of characters for the same reason.
 
Upvote 0
This is another option
Code:
Function NC(MyStr As String) As String

    Dim cnt As Long
    Dim Ch As String

    For cnt = 1 To Len(MyStr)
        Ch = Ch & Chr(Asc(Mid(MyStr, cnt, 1)) + 1)
    Next cnt
    NC = Ch

End Function
It will increase the ascii value of each character by 1

Thanks a lot. Since the code is based on ASCII shifting, I'm guessing it will work on all types of characters including symbols . Thanks again.
 
Upvote 0
Go ahead and try. It will work. I left them out because I don't know what is the desired / correct order of those special characters. And there are even some foreign alphabets missing from my list of characters for the same reason.
Yep. Tried it and appears to have worked. Thanks again.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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