I am new to Excel VBA and have a simple question. Once I understand how to to this loop, I can build on it.
I would like to automate a classification. The data repeats itself in a single column i.e sampleA, sampleA, sampleA, sampleB, sampleB, sampleC, sampleC, sampleC, etc. I would like my macro to identify when the sample name changes between the sample names. So, for example, when the following cell after sampleA turns into sampleB, I would like the macro to understand that there is a change and write a phrase next to this (this will next turn into an equation with the data in the sample's respective rows but baby steps :) ).
Overall, the macro needs to sense the changes between a column of identical sample names until a new name is reached.
I have been researching solutions and the "do until" loop seems to be the closest solution to what I need. Also, to show that sampleA is not the same as sampleB I used <>.
Sub DoUntilStringMacro()
Dim sampleA As String
Dim sampleB As String
Dim sampleC As String
Dim classification As String
Do Until ActiveCell.Value = ""
If sampleA <> sampleB Then
classification = "hello"
ElseIf sampleB <> sampleC Then
classification = "goodbye"
ElseIf sampleC <> sampleA Then
classification = "see you soon"
End If
answer = classification
Cells(classification, "B").Value = answer
Loop
End Sub
Actual results: error at Cells(classification, "B").Value = answer Something is wrong here. I am trying to display results in column "B".
Expected results on Excel Worksheet in columns:
Sample: Classification
sampleA --
sampleA --
sampleA hello
sampleB --
sampleB goodbye
sampleC --
sampleC --
sampleC see you soon
ActiveCellcan cause issues. Instead of using aDo Untilloop, perhaps consider finding the last row and then looping from start row to last row.SampleAetc. are never initialized, and it isn't clear what cell you expect something likeCells("goodbye","B")to be since"goodbye"is manifestly not a row number.samplevariables come from. If you deal withActiveCell, you need to advance it in a loop - otherwise you're stuck with one and the same cell.