If statement in VBA

gdiamonds22

New Member
Joined
Dec 6, 2005
Messages
4
I'm new to VBA and I need assitance in writing a macro that automatically puts a value in a cell in respect to what a cell in a different column is. For example.

Column A Column B
parent
child
aunt

*My macro needs to say if column A is "parent" then column B should be "1", if column A is "child" column B should be "2"...etc

Thanks in advance for any help.
 

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

gdiamonds22

New Member
Joined
Dec 6, 2005
Messages
4
What would the formula look like? I thought it might be easier in VBA (or just a good way for me to learn it).
 

Greg Truby

MrExcel MVP
Joined
Jun 19, 2002
Messages
10,014

ADVERTISEMENT

Are you matching a short, fixed list? Or a long, dynamic list? If short & fixed then perhaps something as simple as <ul>[*]=MATCH(A1,{"parent","child","aunt"},0)[/list] would suit. If longer or more complex, we could help much more quickly and easily if you use COLO's utility (see the link at the bottom of all threads) to post some sample data.
 

gdiamonds22

New Member
Joined
Dec 6, 2005
Messages
4

ADVERTISEMENT

The match didn't work. When the spreadsheet comes in it is different every day. I need to updated the sheet with the corresponding numbers to what ever the original data is. I.E. if parent in column a, add a 1 to column b and so on.
 

dantb

Active Member
Joined
Mar 20, 2002
Messages
358
This seems to work but if the data is existing you have to use your arrow keys and scroll down column a, don’t know to do that with script. Dan


Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 1 And Target.Count = 1 Then
Application.EnableEvents = False
Target.Offset(0, 1).Formula = "=IF(RC[-1]=""parent"",(1),IF(RC[-1]=""child"",(2),IF(RC[-1]=""aunt"",(3),"""")))"
Application.EnableEvents = True
End If
End Sub
 

Greg Truby

MrExcel MVP
Joined
Jun 19, 2002
Messages
10,014
I'd suggest using Colo's HTML maker utility (see link at bottom of threads) to post your current data and your desired results. Otherwise we're gonna waste a lot of your and our efforts just trying to clarify your requirements.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,437
Messages
5,596,120
Members
414,043
Latest member
thomas Stein

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
Top