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

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

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,118,727
Messages
5,573,853
Members
412,554
Latest member
faridr
Top