Reputation: 95
It sounds simple but I couldn't find a way to do this. I'm fairly limited on VBA.
Column A:
Some Text
Some Text
Some Text
Repeated Text
Some Text
Some Text
Repeated Text
SomeText
First colum has some text like this. I also have a list that includes 30 different strings like this:
Dog
Cat
Lion
Tiger
Bear
...
Elephant
I want to copy paste those strings to Column B with one condition. Whenever that repeated text is encountered at the same row in column A the copied value will change to the next string in the animal list. When it reaches the last animal it should revert back to the first animal "Dog". In the end simply I want something like this;
Column A: Column B:
R1 Some Text Dog
R2 Some Text Dog
R3 Some Text Dog
R4 Repeated Text Cat
R5 Some Text Cat
R6 Some Text Cat
R7 Repeated Text Lion
R8 SomeText Lion
.............
R124 Some Text Elephant
R125 Some Text Elephant
R126 Repeated Text Dog (List is over and it reverted back to the first animal in the list)
R127 Some text Dog
....
I couldn't do it with any excel formula...
Upvotes: 0
Views: 66
Reputation: 422
I like Scott's answer better, but here's my solution: List of Critters in Column A, some text in C, and the results in D.
Sub Get_Animal()
Dim rngAnimals As Range, rngWords As Range, rngThing As Range, strChangeValue As String, AnimalCounter As Long
Set rngWords = ActiveSheet.Range("C1:C26")
Set rngAnimals = ActiveSheet.Range("A1:A3")
strChangeValue = "end"
AnimalCounter = 1
ActiveSheet.Range("D1:D99").Clear
For Each rngThing In rngWords
If rngThing.Text = strChangeValue Then
AnimalCounter = AnimalCounter + 1
If AnimalCounter > rngAnimals.Rows.Count Then
AnimalCounter = 1
End If
End If
rngThing.Offset(0, 1) = rngAnimals.Cells(AnimalCounter, 1)
Next rngThing
End Sub
Upvotes: 1
Reputation: 152605
A simple INDEX with A COUNTIF will do the trick. In B1 put:
=INDEX(D:D,MOD(COUNTIF($A$1:$A1,"Repeated Text"),COUNTA(D:D))+1)
And copy down.
My list of animals is in Column D.
This will adjust as the list in D gets larger or shorter and will repeat when it reaches the end of the list.
Upvotes: 2