Add +1 to a string var

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

Add +1 to a string var

Post by sal21 »

I have this myvar="tot01" (as string) i need to add +1 to the last two character and return a new myvar="tot02?
Tks.

NOTE:
the adding value are the ordinal number of the months.
Naturally if i have: myvar="tot12" and add +1 the new result is myvar="tot01"

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Add +1 to a string var

Post by Rudi »

Could you not use the current date and append?

Code: Select all

Dim myVar As String
myVar = "tot" & Month(Date)
MsgBox myVar
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

Re: Add +1 to a string var

Post by sal21 »

Rudi wrote:Could you not use the current date and append?

Code: Select all

Dim myVar As String
myVar = "tot" & Month(Date)
MsgBox myVar
No....
the append month is dinamic, and not currentd date

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Add +1 to a string var

Post by Rudi »

Maybe something like this?

Code: Select all

Dim myVar As String
Dim oldVal As String
    myVar = "tot01"
    oldVal = CInt(Right(myVar, 2)) + 1
    If oldVal > 12 Then oldVal = 1
    myVar = "tot" & Format(oldVal, "00")
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

PJ_in_FL
5StarLounger
Posts: 1098
Joined: 21 Jan 2011, 16:51
Location: Florida

Re: Add +1 to a string var

Post by PJ_in_FL »

Code: Select all

myVar = Left(myVar, 3) & WorksheetFunction.Choose(CInt(Right(myVar, 2)), "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "01")
PJ in (usually sunny) FL