Add an increasing Number to each instance of a String

AGALLEGOS

New Member
Joined
Aug 26, 2013
Messages
10
I have a project that imports several text files into a spreadsheet, and I am in need of a way to identify the beginning lines of each file. Doing this will allow me to separate the bunch into separate columns later on.

So, Each file begins with the word "Sample" and I would like to change Sample to Title1, while increasing the number next to Title by 1 in each instance.

I would like to use VBA to scan through Column A and change each "Sample" to "Title1", next instance of Sample changes to Title2 and so on.

[TABLE="width: 500"]
<tbody>[TR]
[TD]Sample
[/TD]
[/TR]
[TR]
[TD]data
[/TD]
[/TR]
[TR]
[TD]END
[/TD]
[/TR]
[TR]
[TD]Sample
[/TD]
[/TR]
[TR]
[TD]data
[/TD]
[/TR]
[TR]
[TD]END
[/TD]
[/TR]
[TR]
[TD]Sample
[/TD]
[/TR]
[TR]
[TD]data
[/TD]
[/TR]
[TR]
[TD]END
[/TD]
[/TR]
</tbody>[/TABLE]

the end result would look like this
[TABLE="width: 500"]
<tbody>[TR]
[TD]Title1
[/TD]
[/TR]
[TR]
[TD]data
[/TD]
[/TR]
[TR]
[TD]End
[/TD]
[/TR]
[TR]
[TD]Title2
[/TD]
[/TR]
[TR]
[TD]data
[/TD]
[/TR]
[TR]
[TD]End
[/TD]
[/TR]
[TR]
[TD]Title3
[/TD]
[/TR]
[TR]
[TD]data
[/TD]
[/TR]
[TR]
[TD]End
[/TD]
[/TR]
</tbody>[/TABLE]

I have experimented with some loops and different methods, but I cannot seem to nail this one down.

any help or direction would be greatly appreciated.

--A
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Is the data consistent? In other words, does the text "Sample" occur every 3rd cell? Or does it vary?
 
Upvote 0
Code:
Sub mrExcelTitle()
Dim iterator As Integer
Dim LastRow As Integer
LastRow = Range("A1").End(xlDown).Row
iterator = 1
For i = 1 To LastRow
    If Cells(i, 1).Value = "Sample" Then
        Cells(i, 1).Value = "Title" & iterator
        iterator = iterator + 1
    End If
Next
End Sub

This works for me
 
Upvote 0
The following macro assumes the following...

1) The sheet containing the data is the active sheet.

2) Column A contains the data.

3) The data starts at Row 2.

4) The find/replace doesn't need to be case-sensitive.


Code:
Option Explicit

Sub AddTitles()

    Dim Cnt         As Long
    Dim LastRow     As Long
    Dim i           As Long
    
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
    Cnt = 0
    For i = 2 To LastRow
        If UCase(Cells(i, "A").Value) = "SAMPLE" Then
            Cnt = Cnt + 1
            Cells(i, "A").Value = "Title" & Cnt
        End If
    Next I

    MsgBox "Completed...", vbExclamation
    
End Sub
 
Last edited:
Upvote 0
Here is another macro to consider...

Code:
Sub SampleToNumberedTitle()
  Dim LastRow As Long
  If Range("A1").Value = "Sample" Then Range("A1").Value = "Title1"
  With Range("A2", Cells(Rows.Count, "A").End(xlUp))
    .Replace "Sample", "#N/A", xlWhole
    .SpecialCells(xlConstants, xlErrors).FormulaR1C1 = "=""Title""&COUNTIF(R1C:R[-1]C,""Title*"")+1"
    .Value = .Value
  End With
End Sub
 
Upvote 0
Works like a charm. I was unable to figure it out previously because I was testing it with a different value instead of Sample. This was causing an error. But I have resolved it thanks to your help.

A
 
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