How to learn complex formulas

crm911

New Member
Joined
Apr 28, 2015
Messages
15
This was from an old thread about extracting domains from a URL, but this is a question about *HOW* you (any expert) visualise this formula before writing it? Do you write the core and then add refinements?

What is a good method of learning to write such formulas?

I didn't even notice the "dots" were gone. Okay, let's just SUBSTITUTE the spaces away...

=SUBSTITUTE(TRIM(RIGHT(SUBSTITUTE(TRIM(TRIM(LEFT(SUBSTITUTE(TRIM(IFERROR(MID(A1,FIND("://",A1)+3,99),A1))&"/","/",REPT(" ",99)),99))),".",REPT(" ",99)),99*(2+(LEN(TRIM(RIGHT(SUBSTITUTE(TRIM(TRIM(LEFT(SUBSTITUTE(TRIM(IFERROR(MID(A1,FIND("://",A1)+3,99),A1))&"/","/",REPT(" ",99)),99)))&".",".",REPT(" ",99)),198)))=2))))," ",".")
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Don't be impressed by long, convoluted, arcane formulas. They usually exist for one of two reasons:
1) They should have been done in VBA, but couldn't be (for some reason)
2) The person building it got it to work, but didn't put enough effort into making it efficient.

For instance, I may be wrong, but...from what i can see, that formula could be replaced by this one:
Code:
=TRIM(RIGHT(SUBSTITUTE(LEFT(A1,FIND("/",SUBSTITUTE(A1,"://","|||"))-1),".",REPT(" ",LEN(A1)),1),LEN(A1)))

True formula experts strive for elegant formulas that are understandable and can be maintained/edited by intermediate Excel users. I've seen some really short formulas that are so arcane that I simply have to trust that they work because I'll never figure out what they actually do. (I've seen some date calculation formulas like that). I've also seen many long, complicated formulas (like the one you posted) that just beg to be simplified. There are also formulas that look to be huge, ungainly conglomerations...but are actually repeated sections that simply reference different ranges.

The key is to be well-versed in the more powerful Excel functions (INDEX, MATCH, SUMPRODUCT, SUMIF, SUMIFS, etc) and understand how they interact with each other.

I hope that helps.
 
Upvote 0
Welcome to the Board!

What is a good method of learning to write such formulas?

Watch what guys like Ron, Aladin, Domenic, Barry, and other formula wizards do. Ron hinted at it, but a lot of it comes to trying to simplify logic as best as you can (and a lot of times it can be really hard, but that's why you come here! :))

I'd say that the best place to start is instead of trying to digest a completely convoluted formula like the one above, find a situation where you need something, but can't figure it out and ask a question here; within 15 minutes you'll probably have a formula or VBA solution (or both), but trying to figure out that monster won't do you any good (hell, I got a headache just looking at it! ;)). You're also best suited to try to find situations/data that are familiar to you; just like recording a macro for the first time, if you try doing something you've never done before, you'll never understand the code when you try to read it back.

Just keep asking questions! That's why we're all here.
 
Upvote 0
I've seen some really short formulas that are so arcane that I simply have to trust that they work because I'll never figure out what they actually do. (I've seen some date calculation formulas like that).
Formulae like:
=FLOOR(DAY(MINUTE(A1/38)/2+56)&"/5/"&A1,7)-34
or
=DOLLAR((DAY(MINUTE(A1/38)/2+55)&".4."&A1)/7,)*7-6
for example, that calculate the date of Easter for a year in A1.
 
Upvote 0
Thank you for all the above suggestions. I have asked some questions before, when I needed an answer, but now am looking to learn. I agree that short, elegant formulas are worth using. I am not an Excel newbie, but have rarely gone past VLOOKUP and Pivot Tables.
 
Upvote 0
(Mods feel free to move the post, as it could be getting into a solution mode here)
For instance, I may be wrong, but...from what i can see, that formula could be replaced by this one:
Code:
=TRIM(RIGHT(SUBSTITUTE(LEFT(A1,FIND("/",SUBSTITUTE(A1,"://","|||"))-1),".",REPT(" ",LEN(A1)),1),LEN(A1)))

Sorry, I didn't provide enough context as to the original requirement, as the "long" formula actually does the job most of the time. The problem is identifying a "domain", which in an SEO context becomes a looser definition than the strict one used by domain sellers.

The idea is to extract the unique domain part of the URL. For a top-level domain (TLD) of ".com", such as hxxp://www.example.com/blah/blah, we need to extract "example.com". For a country code TLD (ccTLD) of ".com.au", such as hxxp://blah.example.com.au/blah/blah we need to extract "example.com.au" (http changed to hxxp)

So I imagine the author kept on building the formula to cover all the odd examples that popped up. That's why I saw it as a complex problem. Your elegant formula written without any context (hence not a criticism) doesn't catch the tricky ones and I can't see some of the differences, e.g. a leading number breaks it, but it's not always the same number:

URL
LongElegant
hxxp://01example.com/auto_insurance_info.htm01example.comcom
hxxp://0772.1example.com/%E6%BE%B3%E5%A4%A7%E5%88%A9%E4%BA%9A%E7%BD%91%E5%9D%80%E5%A4%A7%E5%85%A8/01example.comcom
hxxp://0777.1example.com/%E6%BE%B3%E5%A4%A7%E5%88%A9%E4%BA%9A%E7%BD%91%E5%9D%80%E5%A4%A7%E5%85%A8/1example.com1example.com
hxxp://0796.1example.com/%E6%BE%B3%E5%A4%A7%E5%88%A9%E4%BA%9A%E7%BD%91%E5%9D%80%E5%A4%A7%E5%85%A8/1example.com1example.com
hxxp://1001example.net/travel/australia/index.html1001example.netnet
hxxp://1001example.net/travel/australia/south_pacific.html1001example.netnet
hxxp://1234example.com/car-insurance-quotes/Comprehensive-auto-insurance.htm1234example.comcom
hxxp://3points.example.co.uk/car-insurance-how-many-work-weeks-in-a-year-united-states/example.co.ukexample.co.uk
hxxp://5www.example.com/how-big-is-one-acre-in-square-meters/example.comexample.com
hxxp://61example.com/url/662/hxxp://www.example.com.au/business-insurance/61example.com
com
hxxp://99example.com.au/other-business-advertising/contests/create-infographic-cover-travel-insurance-australia-28795199example.com.au
com.au
hxxp://99example.com.au/other-business-advertising/contests/create-infographic-cover-travel-insurance-australia-287951/brief99example.com.aucom.au
hxxp://99example.com.au/other-business-advertising/contests/create-infographic-cover-travel-insurance-australia-287951/overview99example.com.aucom.au
hxxp://9example.com/alphameric/t?p=2619example.comcom
hxxp://9example.com/Business/Insurance/?p=109example.comcom
hxxp://9example.com/Business/Insurance/?p=119example.comcom
hxxp://9example.com/listing/Business/Insurance/travel-insurance-direct-70059example.comcom
hxxp://example.com.au/example.com.aucom.au
hxxp://aca.example.com.au/article/8869484/no-more-loyalty-in-insuranceexample.com.auexample.com.au
hxxp://example.com.au/?pg=1example.com.aucom.au
 
Upvote 0
You're right.

Now, just because I was in the mood for an Excel challenge....
I played around with possible solutions and stopped when I got to this one :)
Code:
=MID(MID(LEFT(A1,FIND("|",SUBSTITUTE(A1,"/","|",3))-1),8,100),
IFERROR(FIND("|",SUBSTITUTE(LEFT(MID(LEFT(A1,FIND("|",SUBSTITUTE(A1,"/","|",3))-1),8,100),
LOOKUP(100,SEARCH({".com",".net",".co."},MID(LEFT(A1,FIND("|",SUBSTITUTE(A1,"/","|",3))-1),8,100)))-1),".","|"))+1,1),100)

Yes, it's as ugly as a mud fence...but, I *think* it's less convoluted.
 
Upvote 0
Thanks, Ron. I am getting some inexplicable results for some cells, but I want to study your formula to learn and then have a crack at modifying it.
 
Upvote 0
Hi,

If you face a complex formula ... take the time to de-construct it ... in order to learn how each of the building blocks contributes to the overall result ...

XOR LX ... and his site: EXCELXOR ...is extremely interesting ...!!!
 
Upvote 0

Forum statistics

Threads
1,216,081
Messages
6,128,695
Members
449,464
Latest member
againofsoul

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