Excel VBA IF statement

realtoast

New Member
Joined
Nov 24, 2015
Messages
47
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Experts,

I have a data table, to which I need to insert a column B, then test whether data in cell D2 (Relationship) is "Employee". If so, in B2, add "P". If D2 is not Employee, leave blank. If I were doing this in formula, =IF([@[ Relationship]]="Employee","P","") works. But I need to do this within a series of macros.

The below script is where I'm starting, but I'm not getting the if statement. I first insert the column, B, then title the header, Participant_ID. Here, I currently just put "P" all the way down. I need to instead test if Relationship is "Employee", and if so, place "P". If not, leave blank.

Sub AA2_PID()
'
' This script inserts a column B, then tests whether data in cell E2 is "Employee" or not, if so
' then place "P" in cell B2, then drag down to all cells in column B. If not Employee, leave blank

Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("StartTable[[#Headers],[Column1]]").Select
ActiveCell.FormulaR1C1 = "Participant_ID"
Range("B2").Select
ActiveCell.FormulaR1C1 = "P"
Selection.AutoFill Destination:=Range("StartTable[Participant_ID]")

End Sub
VBA Code:



Employee CodeParticipant_IDNameRelationship
A5A0PSmith, BRIAN JEFFERYEmployee
A5A0Smith, KariSpouse
A5A0Smith, TimothyChild
A5A0Smith, SaraChild
A5A0Smith, JerryChild
A5A0Smith, KimberlyChild
A1HDPJones, StepanieEmployee
A58KJefferson, JASMIN MARISELASpouse
A55XJefferson, MICHELLE ORTEGAChild
A59XJefferson, RAUL ALEJANDROChild
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try this_

VBA Code:
Sub Macro1()
  Range("StartTable[[#Headers],[Name]]").ListObject.ListColumns.Add Position:=2
  Range("B1").Value = "Participant_ID"
  Range("B2").FormulaR1C1 = "=IF([@Relationship]=""Employee"",""P"","""")"
End Sub
 
Upvote 0
Deleted as doesn't cover the OP's request to insert column (+ DanteAmor's code is cleaner)
 
Last edited:
Upvote 0
Try this_

VBA Code:
Sub Macro1()
  Range("StartTable[[#Headers],[Name]]").ListObject.ListColumns.Add Position:=2
  Range("B1").Value = "Participant_ID"
  Range("B2").FormulaR1C1 = "=IF([@Relationship]=""Employee"",""P"","""")"
End Sub
Thank you. I was not able to get this script to work, but it lead me to recording a macro that almost resulted in the same script.
 
Upvote 0
I was not able to get this script to work

Try:
VBA Code:
Sub Macro1()
  Range("B1").EntireColumn.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
  Range("B1").Value = "Participant_ID"
  Range("B2").FormulaR1C1 = "=IF([@Relationship]=""Employee"",""P"","""")"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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