UDF using application.worksheetfunction and structured reference in a table

Lucky_Irishman

New Member
Joined
Feb 19, 2022
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Error in UDF using application.worksheetfunction and structured reference in a table.

Compile Error:
Expected: Expression

The object is to replace the complex formula in the "Carrier" column with a UDF.
I'm having trouble with the syntax of this part. I don't need help replicating the entire formula in VBA.

Thanks in advance for your help.


VBA Code:
Function Carrier()
   ' UDF using application.worksheetfunction and structured reference in a table
   ' Compile Error:
   ' Expected: Expression
   
   Dim strInput
   Dim strOutput
   
   strInput = "usps"
   strOutput = "USPS"

   Carrier = Application.WorksheetFunction.IFERROR(IF(FIND(strInput, [@[Tracking  Link]])<>0,strOutput),"")
  
End Function

Orders_test.xlsm
ABC
1ItemTracking LinkCarrier
2Item 1https://www.fedex.com/apps/fedextrack/Fedex
3Item 2https://www.amazon.com/progress-tracker/Amazon
4Item 3https://wwwapps.ups.com/etracking/UPS
5Item 4https://www.pochta.ru/Russian Post
6Item 5http://www.dhl-usa.com/DHL
7Item 6http://link.order.homedepot.com/Home Depot Express Delivery
8Item 7https://www.sf-international.com/SF-International
9Item 8https://www.yuntrack.com/YunExpress
10Item 9https://tools.usps.com/USPS
Order Tracking
Cell Formulas
RangeFormula
C2:C10C2=IF([@[Tracking Link]]="", "",IFERROR(IF(FIND("homedepot", [@[Tracking Link]])<>0,"Home Depot Express Delivery" ),IFERROR(IF(FIND("fedex", [@[Tracking Link]])<>0,"Fedex"),IFERROR(IF(FIND("usps", [@[Tracking Link]])<>0,"USPS" ), IFERROR(IF(FIND("amazon", [@[Tracking Link]])<>0,"Amazon" ),IFERROR( IF( FIND("ups", [@[Tracking Link]])<>0, "UPS"),IFERROR( IF(FIND("canadapost", [@[Tracking Link]])<>0,"Canada Post"),IFERROR(IF(FIND("dhl", [@[Tracking Link]])<>0,"DHL"),IFERROR(IF(FIND("sf-international", [@[Tracking Link]])<>0,"SF-International"),IFERROR(IF(FIND("Pickup", [@[Tracking Link]])<>0,"Pickup" ),IFERROR( IF(FIND("yuntrack", [@[Tracking Link]])<>0,"YunExpress"),IFERROR(IF(FIND("pochta", [@[Tracking Link]])<>0, "Russian Post"),"" ))))))))))))
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I suspect your issue is that you're passing variables as literals. Usually variables require concatenation when creating formulas or expressions.
Perhaps

Dim strFormula As String
strFormula = "Application.WorksheetFunction.IFERROR(IF(FIND( " & strInput & ",[@[Tracking Link]])<>0," & strOutput & ")0,' ' )"
Carrier = strFormula

I've shown 2 single quotes with spaces (red) to show it isn't a double quote. You'd remove the space in between. However, I don't know if the formula will accept them, or if you'll have to use double quotes and escape them in vba. That might need to be 0,"""")""" or something similar. Also, if the input and output are strings, they'll need delimiters also.
 
Upvote 0
ups.xlsx
ABCDEF
1ItemTracking LinkCarrierSearchCarrier
2Item 1https://www.fedex.com/apps/fedextrack/FedexhomedepotHome Depot Express Delivery
3Item 2https://www.amazon.com/progress-tracker/AmazonfedexFedex
4Item 3https://wwwapps.ups.com/etracking/UPSuspsUSPS
5Item 4https://www.pochta.ru/Russian PostamazonAmazon
6Item 5http://www.dhl-usa.com/DHLupsUPS
7Item 6http://link.order.homedepot.com/Home Depot Express DeliverycanadapostCanada Post
8Item 7https://www.sf-international.com/SF-InternationaldhlDHL
9Item 8https://www.yuntrack.com/YunExpresssf-internationalSF-International
10Item 9https://tools.usps.com/USPSPickupPickup
11https://tools.ussps.com/-yuntrackYunExpress
12pochtaRussian Post
Blad1
Cell Formulas
RangeFormula
C2:C11C2=IFERROR(INDEX(Tabel1[Carrier],MAX(IF(ISNUMBER(SEARCH(Tabel1[Search],B2)),ROW(Tabel1[Carrier])-ROW(Tabel1[[#Headers],[Carrier]]),-1))),"-")
 
Upvote 0
The object is to replace the complex formula in the "Carrier" column with a UDF.
Did I misinterpret that to mean that a formula was not desired?
If not, my code has an extra 0 in it somehow, so that would need to be removed (as per below). Also, if code is what is required and the single delimiters are an issue, I have figured out the syntax for double quotes:

strFormula = "Application.WorksheetFunction.IFERROR(IF(FIND(""" & strInput & """"
strFormula = strFormula & ",[@[Tracking Link]])<>0,""" & strOutput & """),"""")"

Output is
Application.WorksheetFunction.IFERROR(IF(FIND("usps",[@[Tracking Link]])<>0,"USPS"),"")
I also note that there are 2 spaces between tracking and link and I removed one - perhaps not correct to do that.
 
Upvote 0
I suspect your issue is that you're passing variables as literals. Usually variables require concatenation when creating formulas or expressions.
Perhaps

Dim strFormula As String
strFormula = "Application.WorksheetFunction.IFERROR(IF(FIND( " & strInput & ",[@[Tracking Link]])<>0," & strOutput & ")0,' ' )"
Carrier = strFormula

I've shown 2 single quotes with spaces (red) to show it isn't a double quote. You'd remove the space in between. However, I don't know if the formula will accept them, or if you'll have to use double quotes and escape them in vba. That might need to be 0,"""")""" or something similar. Also, if the input and output are strings, they'll need delimiters also.

Did I misinterpret that to mean that a formula was not desired?
If not, my code has an extra 0 in it somehow, so that would need to be removed (as per below). Also, if code is what is required and the single delimiters are an issue, I have figured out the syntax for double quotes:

strFormula = "Application.WorksheetFunction.IFERROR(IF(FIND(""" & strInput & """"
strFormula = strFormula & ",[@[Tracking Link]])<>0,""" & strOutput & """),"""")"

Output is
Application.WorksheetFunction.IFERROR(IF(FIND("usps",[@[Tracking Link]])<>0,"USPS"),"")
I also note that there are 2 spaces between tracking and link and I removed one - perhaps not correct to do that.

You are correct, there was an extra space. Glad you noticed. The output is now just text. I must be missing something.




ItemTracking LinkCarrierCarrier_Micron
Item 1https://www.fedex.com/apps/fedextrack/FedexApplication.WorksheetFunction.IFERROR(IF(FIND("usps",[@[Tracking Link]])<>0,"USPS"),"")
Item 2https://www.amazon.com/progress-tracker/AmazonApplication.WorksheetFunction.IFERROR(IF(FIND("usps",[@[Tracking Link]])<>0,"USPS"),"")
Item 3https://wwwapps.ups.com/etracking/UPSApplication.WorksheetFunction.IFERROR(IF(FIND("usps",[@[Tracking Link]])<>0,"USPS"),"")
Item 4https://www.pochta.ru/Russian PostApplication.WorksheetFunction.IFERROR(IF(FIND("usps",[@[Tracking Link]])<>0,"USPS"),"")
Item 5http://www.dhl-usa.com/DHLApplication.WorksheetFunction.IFERROR(IF(FIND("usps",[@[Tracking Link]])<>0,"USPS"),"")
Item 6http://link.order.homedepot.com/Home Depot Express DeliveryApplication.WorksheetFunction.IFERROR(IF(FIND("usps",[@[Tracking Link]])<>0,"USPS"),"")
Item 7https://www.sf-international.com/SF-InternationalApplication.WorksheetFunction.IFERROR(IF(FIND("usps",[@[Tracking Link]])<>0,"USPS"),"")
Item 8https://www.yuntrack.com/YunExpressApplication.WorksheetFunction.IFERROR(IF(FIND("usps",[@[Tracking Link]])<>0,"USPS"),"")
Item 9https://tools.usps.com/USPSApplication.WorksheetFunction.IFERROR(IF(FIND("usps",[@[Tracking Link]])<>0,"USPS"),"")
 
Upvote 0
ups.xlsx
ABCDEF
1ItemTracking LinkCarrierSearchCarrier
2Item 1https://www.fedex.com/apps/fedextrack/FedexhomedepotHome Depot Express Delivery
3Item 2https://www.amazon.com/progress-tracker/AmazonfedexFedex
4Item 3https://wwwapps.ups.com/etracking/UPSuspsUSPS
5Item 4https://www.pochta.ru/Russian PostamazonAmazon
6Item 5http://www.dhl-usa.com/DHLupsUPS
7Item 6http://link.order.homedepot.com/Home Depot Express DeliverycanadapostCanada Post
8Item 7https://www.sf-international.com/SF-InternationaldhlDHL
9Item 8https://www.yuntrack.com/YunExpresssf-internationalSF-International
10Item 9https://tools.usps.com/USPSPickupPickup
11https://tools.ussps.com/-yuntrackYunExpress
12pochtaRussian Post
Blad1
Cell Formulas
RangeFormula
C2:C11C2=IFERROR(INDEX(Tabel1[Carrier],MAX(IF(ISNUMBER(SEARCH(Tabel1[Search],B2)),ROW(Tabel1[Carrier])-ROW(Tabel1[[#Headers],[Carrier]]),-1))),"-")

Even though this is not what I was looking for, it works perfectly, will be easy to modify, and will likely perform better than a UDF on a large table.
 
Upvote 0
Your function was written to only return a string. If you wanted an actual formula, you'd need to begin with equals sign?
I would agree with using the formula if it works. Note also that Find is case sensitive so you'd have that constraint as well.
 
Upvote 0
Your function was written to only return a string. If you wanted an actual formula, you'd need to begin with equals sign?
I would agree with using the formula if it works. Note also that Find is case sensitive so you'd have that constraint as well.
The formula BSALV posted is better than the solution I was asking for.

I can see why the confusion now that I look back at my post. I meant I didn't need the whole formula that would find each carrier. Just the correct syntax for the part I posted. I thought that would be simpler. Maybe not so much. Thank you for your help.
 
Upvote 0
Welcome to the MrExcel board!

It looks like you may be dealing with two tables? If so, or in any case, would this shorter structure work just as well for you?
For me, the left table is Table1 and the right table is Table2

22 02 20.xlsm
ABCDEF
1ItemTracking LinkCarrierSearchCarrier
2Item 1https://www.fedex.com/apps/fedextrack/FedexhomedepotHome Depot Express Delivery
3Item 2https://www.amazon.com/progress-tracker/AmazonfedexFedex
4Item 3https://wwwapps.ups.com/etracking/UPSuspsUSPS
5Item 4https://www.pochta.ru/Russian PostamazonAmazon
6Item 5http://www.dhl-usa.com/DHLupsUPS
7Item 6http://link.order.homedepot.com/Home Depot Express DeliverycanadapostCanada Post
8Item 7https://www.sf-international.com/SF-InternationaldhlDHL
9Item 8https://www.yuntrack.com/YunExpresssf-internationalSF-International
10Item 9https://tools.usps.com/USPSPickupPickup
11Item 10https://tools.ussps.com/-yuntrackYunExpress
12pochtaRussian Post
13
14
Lookup
Cell Formulas
RangeFormula
C2:C11C2=IFNA(LOOKUP(9^9,SEARCH(Table2[Search],[@[Tracking Link]]),Table2[Carrier]),"-")


If it is a layout as in post #3 then replace [@[Tracking Link]] with B2
Excel Formula:
=IFNA(LOOKUP(9^9,SEARCH(Table2[Search],B2),Table2[Carrier]),"-")
 
Upvote 0
=IFNA(LOOKUP(9^9,SEARCH(Table2[Search],[@[Tracking Link]]),Table2[Carrier]),"-")
Thanks, this one looked good, but when I applied it to the real sheet, there were incorrect lookups. The problem is that real links can have 100+ characters, which may contain more than one valid result from the search table. For example a long Amazon link may include "UPS" or "DHL" somewhere in it. For some reason, the other solution did give any false results. I'm not sure why.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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