Solved

How to construct  Excel If-Then-Else VBA

Posted on 2015-01-25
13
68 Views
Last Modified: 2016-02-11
I'm looking at 2 columns - "CompanyName" and "PlanID".  While "PlanID" is a number, both are text variables.

I have over 5000 records and need to populate the "PlanID" column depending on the "CompanyName." I have a list of at least 60 "CompanyName(s)."

How can I achieve this with Excel VBA.

Thank you.
0
Comment
Question by:andreaEdwards
  • 6
  • 5
13 Comments
 
LVL 11

Expert Comment

by:Wilder1626
ID: 40569522
Hi

I'm not sure i fully understand what you are trying to do.

Can you please post a sample of your excel file, and also tell us what should also be the final result you would like to have.

But without understanding what you require, here is an example in attachment
vlookup.xlsx
0
 
LVL 40
ID: 40569621
One needs to have all the details before started to thing about coding. It's not only a question of If-Then-Else. You need to access the original data, search and loop through it, know how to organize the data and how to integrate it with already existing data if there is already something in the destination sheet.

Are you starting with an empty sheet or are the company names already on the sheet?

And in any case, when you say that you have 5000 records, where do they come from? A text file, a database, another worksheet, another workbook, some other source?

If it is a text file or a worksheet, how is the data organized in it. If it is a database, what type of database and how is the data is organized in it : simple table (what are the fields) or query/view/stored procedure?

Do you want something like the following:

<CompanyName1> <ID1>
                                   <ID2>
                                   <ID3>
<CompanyName2> <ID1>
                                   <ID2>
                                   <ID3>

or something else.
0
 

Author Comment

by:andreaEdwards
ID: 40569631
OK.

These are some of the given data definitions as far as CompanyName and PlanID's are concerned which is one excel file:
CompanyName                     	PlanID
Aetna Better Health             	03458546
Affinity Health Plan            	2010186
AgeWell New York                	03481927
Alphacare                       	03560441
Amida Care                      	S99B001
Archcare Community Life         	03466800
ArchCare Senior Life            	03114514
Catholic Health – LIFE          	03072740
CDPHP                           	1090384
CenterLight PACE                	01234037
CenterLight Select              	02710185
Centers Plan for Healthy Living 	03506989
Complete Senior Care             	03320725
Eddy Senior Care                	01674982
Elant Choice                     	01825947
Elderplan                       	03173113
Elderplan dba Homefirst         	03253707
Elderserve                       	03234044
Excellus BlueCross BlueShield   	1390598
Extended MLTC                   	03549135
FFS (Fee-for-Service)           	8888888
Fidelis Care at Home             	01788325
Fidelis Care New York           	2060193
Fidelis Medicaid Advantage Plus 	02927631
GuildNet                        	01827572
Guildnet Medicaid Advantage Plus	02942923
Health Insurance Plan           	03239801
Health Plus (Amerigroup)        	2180196

Open in new window

Here is the otherExcel  file where I have to populate the PlanID manually once I include the CompanyName.  I'd like to populate the PlanID automatically depending on the CompanyName.  I would think this would be done using the VBA IF:
status  	CompanyName         	PlanID
Pending 	Healthfirst PHSP, Inc.	2090194
Pending 	Healthfirst PHSP, Inc.	2090194
Pending 	Healthfirst PHSP, Inc.	2090194
Pending 	Healthfirst PHSP, Inc.	2090194
Inactive	HealthPlus (Amerigroup)	2180196
Inactive	Catholic Health – LIFE	03072740
Pending 	Healthfirst PHSP, Inc.	2090194
Pending 	Catholic Health – LIFE	03072740
Pending 	Healthfirst PHSP, Inc.	2090194
Inactive	Healthfirst PHSP, Inc.	2090194
Pending 	Healthfirst PHSP, Inc.	2090194
Pending 	Catholic Health – LIFE	03072740
Pending 	Healthfirst PHSP, Inc.	2090194

Open in new window

I hope this makes my question clearer...
0
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
LVL 11

Expert Comment

by:Wilder1626
ID: 40569638
Here you go with some of your data in the excel file.

a VLookup would be a good option:
Ex:
=VLOOKUP(B2,'PanID per company'!A:B,2,0)
vlookup.xlsx
0
 

Author Comment

by:andreaEdwards
ID: 40569646
Each CompanyName only has 1 PlanID.

My initial issue (I think) is that the source of the Excel data download does not contain the PlanID, so I need to make the PlanID data dependent on the CompanyName data with an IF statement.
0
 
LVL 11

Expert Comment

by:Wilder1626
ID: 40569656
so you really need 2 sheets.

One sheet where you will have each Plan ID and the Company name that goes with it, and another sheet where you have the company name only. You would then use the VLookup, just like my last attachment. This would refer to the sheet with the list of company name with his Plan ID, and add the Plan ID besides the Company name in the other sheet.

Can you add an excel template? i will be able to use your template and make the VLookup for you
0
 
LVL 11

Accepted Solution

by:
Wilder1626 earned 500 total points
ID: 40569671
another way would be to combine both, an IF statement and a VLookup
if vlookup
=IF(ISNA(VLOOKUP(B5,'PanID per company'!A:B,2,FALSE)),"", IF(VLOOKUP(B5,'PanID per company'!A:B,2,FALSE)="","", VLOOKUP(B5,'PanID per company'!A:B,2,FALSE)))

 

see attachment
vlookup.xlsx
0
 

Author Comment

by:andreaEdwards
ID: 40569831
I can't send additional data because of PHI (Personal Health Information).  regulations.

I'll try your vLookup solution and get back to you.

Thank you.
0
 
LVL 11

Expert Comment

by:Wilder1626
ID: 40569876
I understand.

Or, you can also mimic how the data and sheet would look like.

The formula would then apply as soon you would use your official file.

You don't need to put a lot of rows of dummy data's. Just a couple.
0
 

Author Comment

by:andreaEdwards
ID: 40770794
Thank you Wilder 1626!  To you goes all the glory!  My problem is solved!
0
 

Author Closing Comment

by:andreaEdwards
ID: 40770803
My apologies for not getting back to you sooner.  But thank you ever so much.  Problem solved.
0
 
LVL 11

Expert Comment

by:Wilder1626
ID: 40771690
I'm glad i was able to help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question