Change adjacent cell where a cell matches a certain value

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 = ... 

Replay

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
Change adjacent cell where a cell matches a certain value

After
Change adjacent cell where a cell matches a certain value

Category: microsoft excel 2013 Time: 2016-07-29 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.138 (s). 12 q(s)