VBA populate a cell based on values from several worksheets and have error handling

jconkl02

Board Regular
Joined
May 25, 2016
Messages
55
I have been learning VBA over the last two months while trying to re-write a macro that my predecessor did not have time to finish/work out the bugs before he took another job. This forum has been amazing, but I have come to something that I can't find a fix for and I am hoping someone can help me out. I have included my sample data below.


I need to populate the column 'Ticket Owner" in worksheet "Data" based off of values in "Data" and two other worksheets ("Director" and "Market"). Everything stems from what is in Worksheet "Data" Column B (Queue).



  • If the value in Worksheet "Data" Column B (Queue) ends with "RN" then use the value in Column C (Wrls_Mkt) to lookup the name of the Director in Worksheet "Market" and paste that name into "Ticket Owner" in worksheet "Data".
  • If the value in Worksheet "Data" Column B (Queue) DOES NOT end with "RN" then use the value in Column B (Queue) to look up the name in column B (Director) in Worksheet "Director" and paste that name into "Ticket Owner" in worksheet "Data".
  • If either lookup fails to find a matching value then populate Column A of worksheet "Data" with "UNKNOWN"

My predecessor was attempting to accomplish this with this IF and ISERROR function:


********ActiveCell.FormulaR1C1 = "=IF(AND(ISERROR(VLOOKUP(RC[-10],'Director to Queue'!R2C1:R250C2,2,FALSE)),RIGHT(RC[-10],2)=""RN""),VLOOKUP(RC[-7],'Market to Director'!R2C1:R500C2,2,FALSE),VLOOKUP(RC[-10],'Director to Queue'!R2C1:R250C2,2,FALSE))"

I'll be honest I don't understand all of the components of his code, but one problem that keeps popping up is that if there is a new city name or queue name in the Data worksheet that has not been added to the other two worksheets the macro crashes.


I hope this makes sense. Thanks for any help that you can give me.


Excel 2010
ABC
1Ticket OwnerQueueWrls_Mkt
2FOTORNToledo : Toledo
3V1ERRNMiami / West Palm : Miami
4V1ALRNWashington DC : Washington DC
5V1SARNMiami / West Palm : Non-MSA
6V1SARNDFW : Fort Worth / Arlington
7FOTORNDFW : Fort Worth / Arlington
8V1ALWest Washington : Seattle / Bellevue / Everett
9FOTORNPittsburgh : Sharon
10V1ERRNTampa : Tampa / St. Petersburg / Clearwater
11V1ALRNDFW : Fort Worth / Arlington
12V1SARNUpper Central Valley : Modesto
13V1SARNUpper Central Valley : Modesto
14FOTORNLower Central Valley : Bakersfield
15V1ALWest Texas : Non-MSA
16FOTORNMyrtle Beach : Wilmington, NC
17V1ERRNMyrtle Beach : Wilmington, NC
18V1ALRNMemphis : Memphis
19V1SARNBaltimore : Baltimore
20V1SARNChicago : Chicago
21FOTORNNorth Wisconsin : Non-MSA
22V1ALMyrtle Beach : Jacksonville, NC
23V1ALNew York City : New York
24FOTORNWest Texas : Non-MSA
25FOTORNFt. Wayne / South Bend : Fort Wayne

<colgroup><col style="width: 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Data

Excel 2010
AB
1MarketDirector
2Baltimore : BaltimorePeter Pan
3Baltimore : Non-MSAPeter Pan
4Boston : Barnstable / YarmouthPeter Pan
5Boston : BostonPeter Pan
6Idaho : Boise CityCaptain Hook
7Inland Northwest : Non-MSACaptain Hook
8Inland Northwest : Richland / Kennewick / PascoCaptain Hook
9Inland Northwest : SpokaneCaptain Hook
10Cincinnati : Hamilton / MiddletownTinker Bell
11Cincinnati : LimaTinker Bell
12Cincinnati : Non-MSATinker Bell
13Cleveland : AkronTinker Bell
14West Kentucky : Non-MSAWendy Darling
15West Kentucky : St. LouisWendy Darling
16Alabama : AnnistonWendy Darling
17Alabama : BirminghamWendy Darling
18Alabama : Decatur, ALWendy Darling
19Arkansas : Fayetteville / Springdale / RogersTiger Lily
20Arkansas : Fort SmithTiger Lily
21Arkansas : Little Rock / North Little RockTiger Lily
22Arkansas : Non-MSATiger Lily

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Market


Excel 2010
ABC
1QueueDirectorGroup
2PROIMr. SmeeSACO
3RSANGeorge ScourieOEM
4ECPTBlack GilmourSACO
5MOT2Canary RobbSACO
6T2LEBill JukesSACO
7VBO2Black PirateSACO
8VBSCAlf MasonSACO
9WBHERobert MullinsSACO
10WBHSCBGeorge ScourieSACO

<colgroup><col style="width: 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Director
 

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.
Hi J
Your problem is kind of hard to solve because the sample data you have posted does not match with your formula where RC[-10] and RC[-7] gives us a clue on where is the active cell when the macro is run, much to the right
Also data rows are cut to your own caprice not showing the complete domain that is necessary to examine where is/are errors.
Nevertheless for your statement when "there is a new city name or queue name in the Data worksheet" I can tell you that all formulas will crush if you don't add the values to
'Director to Queue' and/or 'Market to Director'
Your are using a VLOOKUP and this formula needs to find the matching values to make their propose.
Also you will get an error when
'Director to Queue' has more the 250 rows and when 'Market to Director' has more then 500 rows
My advice is for you read some information about VLOOKUP to understand how the formula works
Cheers
Sergio

 
Upvote 0
Hi J
Your problem is kind of hard to solve because the sample data you have posted does not match with your formula where RC[-10] and RC[-7] gives us a clue on where is the active cell when the macro is run, much to the right
Also data rows are cut to your own caprice not showing the complete domain that is necessary to examine where is/are errors.
Nevertheless for your statement when "there is a new city name or queue name in the Data worksheet" I can tell you that all formulas will crush if you don't add the values to
'Director to Queue' and/or 'Market to Director'
Your are using a VLOOKUP and this formula needs to find the matching values to make their propose.
Also you will get an error when
'Director to Queue' has more the 250 rows and when 'Market to Director' has more then 500 rows
My advice is for you read some information about VLOOKUP to understand how the formula works
Cheers
Sergio


Sergio,
Thanks for responding. The VLOOKUP in the formula is what the person that maintained this report used. I only added it to the post to give you an idea of how it was being done before.

I am just looking for the most efficient way of looking up the data.

The active cells would be column A of "Data". For example, the script would start on A2 and it would look at the value in B2. If B2 ends with "RN" then the value of C2 would be used in column A of the worksheet "Market" to find the name of the owner of that market and populate A2 of "Data" with that value. If B2 did not end with "RN" then the value of B2 would be used in column A of the worksheet "Director" to find the name of the owner of that queue and populate A2 of "Data" with that value.

In either case, if a match can't be made then A2 would be populated with "UNKNOWN"
 
Upvote 0
Hi J
That is quite different, in this case, the case you describe in your second mail, you do not need a macro/VBA you just need a formula in A2
Code:
=IFERROR(IF(RIGHT(B2,2)="RN",VLOOKUP(C2,market!A:B,2,0),VLOOKUP(B2,director!A:B,2,0)),"UNKNOWN")
Cheers
Sergio
 
Upvote 0
Solution
Hi J
That is quite different, in this case, the case you describe in your second mail, you do not need a macro/VBA you just need a formula in A2
Code:
=IFERROR(IF(RIGHT(B2,2)="RN",VLOOKUP(C2,market!A:B,2,0),VLOOKUP(B2,director!A:B,2,0)),"UNKNOWN")
Cheers
Sergio

Sergio,

My apologies, I rushed that last email because I was trying to leave the office. If I only needed to populate A2 or if the number of lines in the worksheet "Data" was static, then your formula would be perfect. However, in my haste I left out that I would need to populate every cell in column A if there were values in column B and the number of rows will vary daily.

BR,
Jason
 
Upvote 0
Hi Jason
So you do need a macro/vba to write the formula in column A from A2 to as far down as columns B has values, using FormulaR1C1 and end xldown and such
Cheers
Sergio
 
Upvote 0

Forum statistics

Threads
1,215,432
Messages
6,124,860
Members
449,194
Latest member
HellScout

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