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.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
What would the formula look like? I thought it might be easier in VBA (or just a good way for me to learn it).
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,238
Members
448,555
Latest member
RobertJones1986

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