1

If I have the following table:

------------------
| X | helloworld |
------------------
| X | random1234 |
------------------
| X | random5678 |
------------------
| X | helloworld |
------------------
| X | random9123 |
------------------

How can I set the value of the first cell to the left of each occurence of helloworld to Y?

After processing, I would expect the above example to be:

------------------
| Y | helloworld |
------------------
| X | random1234 |
------------------
| X | random5678 |
------------------
| Y | helloworld |
------------------
| X | random9123 |
------------------

To clarify, my spreadsheet contains thousands of these occurrences, I'm looking for a bulk operation, in pseudocode:

for every row:
    if column_B = 'helloworld':
        column_A = 'X'
    if column_B = 'random':
        column_A = ...
user2018084
  • 2,254

1 Answers1

1

This VBa does it.

There is no option to undo, so take a back up first!

Sub WalkThePlank()

Dim updateColumn As String
updateColumn = "A"           'update this ye filthy seadog if needed! This be the first column

Dim contentColumn As String
contentColumn = "B"         'aye, scrub the deck and update if the "helloworld" isn't in column B

Dim startRow As Integer
startRow = 1                  'enter the start row or be fed to the sharks  

Do While (Range(contentColumn & startRow).Value <> "")

    Dim val As String
    val = Range(contentColumn & startRow).Value

    Select Case val

    Case "helloworld"
        Range(updateColumn & startRow).Value = "Y"

    Case Else
        Range(updateColumn & startRow).Value = "X" ' This is the "default value

    End Select        

    startRow = startRow + 1

Loop    

End Sub

To add another case, such as if the search word was Goodbyeworld, update the code to

    Select Case val

    Case "helloworld"
        Range(updateColumn & startRow).Value = "Y"

    Case "Goodbyeworld" ' CASE SENSITIVE!!!!
        Range(updateColumn & startRow).Value = "A"

    Case Else
        Range(updateColumn & startRow).Value = "X" ' This is the "default value

    End Select        

How do I add VBA in MS Office?

Before
enter image description here

After
enter image description here

Dave
  • 25,513