Increment a STRING by One?

jscranton

Well-known Member
Joined
May 30, 2011
Messages
707
Here is my sheet:

Excel Workbook
ABCDE
1DocumentIDDocument DateSenderAddresseesOther Addressees
2H10237RE0001.001.02212/4/2008John DoeJane DoeMatt Guy
3H10237RE0007.001.03212/5/2008Matt GuySteve DudeZack Boy
Sheet1

Excel Workbook
ABCDE
1DocumentIDDocument DateSenderAddresseesOther Addressees
2H10237RE0001.001.02212/4/2008John DoeJane DoeMatt Guy
3H10237RE0001.001.022_0112/4/2008John DoeJane DoeMatt Guy
4H10237RE0007.001.03212/5/2008Matt GuySteve DudeZack Boy
5H10237RE0007.001.032_0112/5/2008Matt GuySteve DudeZack Boy
Excel 2007 THe goal is to create a macro that inserts a row and then, copies the Value in column A and increments it by _01. If the row above is already _01, it would become _02 Sheet1

Excel 2007


This macro gets me really close except I don't know how to increment the _01 to _02:

Sub InsertRowWithContentsAboveAndAutoIncrementDocNumber()

Dim X As String

ActiveCell.EntireRow.Copy
ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown
X = ActiveCell.Offset(-1, 0).Value
ActiveCell.Value = X + "_01"

End Sub


Any ideas on how to increment the string? COuld I convert it to an int and then increment and then convert it back to a string?
[/B]
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hello,

Maybe this will help:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> InsertRowWithContentsAboveAndAutoIncrementDocNumber()<br><SPAN style="color:#00007F">Dim</SPAN> a <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> X <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><br>X = ActiveCell.Value<br><br><SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> NoNumber:<br>    a = <SPAN style="color:#00007F">CInt</SPAN>(Right(X, 2))<br>        <SPAN style="color:#00007F">If</SPAN> a > 0 And a < 9 <SPAN style="color:#00007F">Then</SPAN><br>            a = a + 1<br>                <SPAN style="color:#00007F">With</SPAN> ActiveCell<br>                    .EntireRow.Copy<br>                    .Offset(1, 0).EntireRow.Insert Shift:=xlDown<br>                    .Offset(1, 0).Value = Left(X, Len(X) - 2) & <SPAN style="color:#00007F">CStr</SPAN>("0" & a)<br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>            Application.CutCopyMode = <SPAN style="color:#00007F">False</SPAN><br>            <br>        <SPAN style="color:#00007F">Else</SPAN><br>            a = a + 1<br>                <SPAN style="color:#00007F">With</SPAN> ActiveCell<br>                    .EntireRow.Copy<br>                    .Offset(1, 0).EntireRow.Insert Shift:=xlDown<br>                    .Offset(1, 0).Value = Left(X, Len(X) - 2) & <SPAN style="color:#00007F">CStr</SPAN>(a)<br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>            <br>                 <br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">GoTo</SPAN> Finish:<br>NoNumber:<br>        <SPAN style="color:#00007F">With</SPAN> ActiveCell<br>            .EntireRow.Copy<br>            .Offset(1, 0).EntireRow.Insert Shift:=xlDown<br>            .Offset(1, 0).Value = X & "_01"<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    Application.CutCopyMode = <SPAN style="color:#00007F">False</SPAN><br>Finish:<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Nice work and it is almost perfect. I have come up with my own code but it isn't nearly as clean as yours. One issue. Yours works to increment. But, I realize now that if the DOCUMENT ID doesn't have a _01 after it, it simply adds one to the DOCID.
Excel Workbook
A
2H10245RE0202.051.094_01
3H10245RE0202.051.094_02
4H10245RE0202.051.094_03
5H10245RE0202.051.093
6H10245RE0202.051.094
7H10245RE0202.051.095
Sheet1
Excel 2007

Would it possible to use DocID length to set up the loop? If the length is 20 characters then it needs an _01 added. If it is greater than 20, it needs _01 incremented?
 
Upvote 0
I see. I used a string only with letters to test and this would be added.

Here is an adjustment that should do for string length of 20:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> InsertRowWithContentsAboveAndAutoIncrementDocNumber()<br><SPAN style="color:#00007F">Dim</SPAN> a <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> X <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><br>X = ActiveCell.Value<br><br><SPAN style="color:#00007F">If</SPAN> Len(X) = 20 <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> NoCountNumber:<br><SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> NoCountNumber:<br>    a = <SPAN style="color:#00007F">CInt</SPAN>(Right(X, 2))<br>        <SPAN style="color:#00007F">If</SPAN> a > 0 And a < 9 <SPAN style="color:#00007F">Then</SPAN><br>            a = a + 1<br>                <SPAN style="color:#00007F">With</SPAN> ActiveCell<br>                    .EntireRow.Copy<br>                    .Offset(1, 0).EntireRow.Insert Shift:=xlDown<br>                    .Offset(1, 0).Value = Left(X, Len(X) - 2) & <SPAN style="color:#00007F">CStr</SPAN>("0" & a)<br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>            Application.CutCopyMode = <SPAN style="color:#00007F">False</SPAN><br>            <br>        <SPAN style="color:#00007F">Else</SPAN><br>            a = a + 1<br>                <SPAN style="color:#00007F">With</SPAN> ActiveCell<br>                    .EntireRow.Copy<br>                    .Offset(1, 0).EntireRow.Insert Shift:=xlDown<br>                    .Offset(1, 0).Value = Left(X, Len(X) - 2) & <SPAN style="color:#00007F">CStr</SPAN>(a)<br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>            <br>                 <br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">GoTo</SPAN> Finish:<br>NoCountNumber:<br>        <SPAN style="color:#00007F">With</SPAN> ActiveCell<br>            .EntireRow.Copy<br>            .Offset(1, 0).EntireRow.Insert Shift:=xlDown<br>            .Offset(1, 0).Value = X & "_01"<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    Application.CutCopyMode = <SPAN style="color:#00007F">False</SPAN><br>Finish:<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
You are quick.

I just did almost the same thing (again, your code is cleaner).

Thanks so much.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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