VBA If statements

Excel_Blonde

New Member
Joined
Aug 8, 2018
Messages
44
Hi I'm a very new self teacher of VBA so this is probably a very easy problem to solve.

I'm trying to write a code that will fill column B depending on the text in column A.

For example:


[TABLE="width: 500"]
<tbody>[TR]
[TD]This[/TD]
[TD]That[/TD]
[/TR]
[TR]
[TD]When[/TD]
[TD]Why[/TD]
[/TR]
[TR]
[TD]This[/TD]
[TD]That[/TD]
[/TR]
[TR]
[TD]Where[/TD]
[TD]Who[/TD]
[/TR]
[TR]
[TD]When[/TD]
[TD]Why[/TD]
[/TR]
</tbody>[/TABLE]

So if This is Found in Column A, Column B will fill with That, if Where is found in Column A, column B will fill with Who etc.

I can get it work for 1, but not for multiples.

Any help would be greatly appreciated.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Something like this would work although it's straight forward enough to do with a formula too

Code:
Sub DoIf()
    Dim c As Range, s As String
    
    'loop through all used cells in column A from cell A2 downwards
    For Each c In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
        Select Case c.Value
            Case "This"
                s = "That"
            Case "When"
                s = "Why"
            Case "Where"
                s = "Who"
            Case Else
                s = "Unknown"
        End Select
        c.Offset(0, 1) = s
    Next c
End Sub
 
Upvote 0
Something like this would work although it's straight forward enough to do with a formula too

Code:
Sub DoIf()
    Dim c As Range, s As String
    
    'loop through all used cells in column A from cell A2 downwards
    For Each c In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
        Select Case c.Value
            Case "This"
                s = "That"
            Case "When"
                s = "Why"
            Case "Where"
                s = "Who"
            Case Else
                s = "Unknown"
        End Select
        c.Offset(0, 1) = s
    Next c
End Sub


That works perfectly, thank you very much.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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