VBA include variable in vlookup

I am having trouble including the variable    ratesh1
The code is long, but it is a repeat of

I have tried with INDIRECT and not.
I get #NAME?

Can anyone help?

Sub Calc_New_Rates()
#If VBA7 And Win64 Then
    Dim lngLastRowD As Longlong
    Dim ratesh1 As String
    Dim ratesh2 As String
    Dim lngLastRowD As Long
    Dim ratesh1 As String
    Dim ratesh2 As String
#End If

Application.Calculation = xlCalculationManual
   Application.ScreenUpdating = False
With Sheets("Detail")
   ratesh1 = Sheets("Selection").Range("B11")
   ratesh2 = Sheets("Selection").Range("B12")
lngLastRowD = Sheets("Detail").Range("A800000").End(xlUp).Row

.Range("X2:X" & lngLastRowD).Formula = "=IF(OR($A2={""FO"",""PO"",""SO"",""E2AM"",""E2"",""ESP""}),VLOOKUP($P2,'Dom Ex'!$A$3:$AP$2000,40,FALSE)," & _
"IF(OR($A2={""GR"",""HD"",""PRP""}),VLOOKUP($P2,Ground!$A$3:$AP$2000,40,FALSE),IF(OR($A2={""F1"",""F2"",""F3""}), " & _
"VLOOKUP(151,""DOM_""&$A2&""_MIN_""&ratesh1,Detail!$L2,FALSE),IF(AND($A2=""IP"",$B2=""Letter"",$J2=""PR"",$K2=""EXPORT"")," & _
"VLOOKUP(1,""IP_EX_PR_L_""&ratesh1,2,FALSE),IF(AND($A2=""IP"",$B2=""Box"",$J2=""PR"",$K2=""EXPORT""), " & _
"VLOOKUP(1,""IP_EX_PR_""&ratesh1,2,FALSE),IF(AND($A2=""IE"",$J2=""PR"",$K2=""EXPORT""),VLOOKUP(1,""IE_EX_PR_""&ratesh1,2,FALSE), " & _
"IF(AND($A2=""IP"",$K2=""EXPORT"",$B2=""Letter""),VLOOKUP(1,""IP_EX_MIN_""&ratesh1,$L2,FALSE),IF(AND($A2=""IP"",$K2=""EXPORT"",$B2=""Pak""), " & _
"VLOOKUP(2,""IP_EX_MIN_""&ratesh1,$L2,FALSE),IF(AND($A2=""IP"",$K2=""EXPORT"",$B2=""Box""),VLOOKUP(3,""IP_EX_MIN_""&ratesh1,$L2,FALSE), " & _
"IF(AND($A2=""IE"",$K2=""EXPORT""),VLOOKUP(1,""IE_EX_MIN_""&ratesh1,$L2,FALSE),IF(AND($A2=""IP"",$K2=""IMPORT"",$B2=""Letter""), " & _
"VLOOKUP(1,""IP_IMP_MIN_""&ratesh1,$L2,FALSE),IF(AND($A2=""IP"",$K2=""IMPORT"",$B2=""Pak""),VLOOKUP(2,""IP_IMP_MIN_""&ratesh1,$L2,FALSE), " & _
"IF(AND($A2=""IP"",$K2=""IMPORT"",$B2=""Box""),VLOOKUP(3,""IP_IMP_MIN_""&ratesh1,$L2,FALSE),IF(AND($A2=""IE"",$K2 " & _
"=""IMPORT""),VLOOKUP(1,""IE_IMP_MIN_""&ratesh1,$L2,FALSE),IF(AND($A2=""IPF"",$J2=""PR"",$K2=""EXPORT""), " & _
"VLOOKUP(1,""IP_EX_PR_FR_MIN_""&ratesh1,2,FALSE),IF(AND($A2=""IEF"", $J2=""PR"",$K2=""EXPORT""),VLOOKUP(1,""IE_EX_PR_FR_MIN_""&ratesh1,2,FALSE), " & _
"IF(AND($A2=""IPF"", $J2=""PR"",$K2=""IMPORT""),VLOOKUP(1,""IP_IMP_PR_FR_MIN_""&ratesh1,2,FALSE), IF(AND($A2=""IEF"",$J2=""PR"",$K2=""IMPORT""), " & _
"VLOOKUP(1,""IE_IMP_PR_FR_MIN_""&ratesh1,2,FALSE),IF(AND($A2=""IPF"",$K2=""EXPORT""), " & _
"VLOOKUP(151,""IPF_EX_MIN_""&ratesh1,$L2,FALSE),IF(AND($A2=""IEF"",$K2=""EXPORT""),VLOOKUP(151,""IEF_EX_MIN_""&ratesh1,$L2,FALSE), " & _
"IF(AND($A2=""IPF"",$K2=""IMPORT""),VLOOKUP(151,IPF_IMP_MIN,$L2,FALSE),IF(AND($A2=""IEF"",$K2=""IMPORT""), " & _

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ProfessorJimJamMicrosoft Excel ExpertCommented:
you need to create a named range and then use it in formula.

instead of
 ratesh1 = Sheets("Selection").Range("B11")
   ratesh2 = Sheets("Selection").Range("B12")

use this code to create named ranges
 ActiveWorkbook.Names.Add Name:="ratesh2", RefersToR1C1:="=Selection!R12C3"
    ActiveWorkbook.Names.Add Name:="ratesh1", RefersToR1C1:="=Selection!R11C3"

you cannot use defined variable name inside formula.
NorieAnalyst Assistant Commented:
Do you want the values from Selection!B11 and B12 in the formula?

Also, are IP_EX_PR_L_, IP_EX_PR_, IP_EX_MIN_ etc the prefixes for named ranges?
ProfessorJimJamMicrosoft Excel ExpertCommented:
please see attached file and run the macro. it will work.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Euro5Author Commented:
Yes, the name would be IP_EX_PR_L_2014  or IP_EX_PR_L_2015 or  IP_EX_PR_L_2016
The IP_EX_PR_L_ is the prefix.
NorieAnalyst Assistant Commented:
Can you post an example of a working formula?

This will compile but I'm not sure it will produce the correct formula.
.Range("X2:X" & lngLastRowD).Formula = "=IF(OR($A2={""FO"",""PO"",""SO"",""E2AM"",""E2"",""ESP""}),VLOOKUP($P2,'Dom Ex'!$A$3:$AP$2000,40,FALSE)," & _
"IF(OR($A2={""GR"",""HD"",""PRP""}),VLOOKUP($P2,Ground!$A$3:$AP$2000,40,FALSE),IF(OR($A2={""F1"",""F2"",""F3""}), " & _
"VLOOKUP(151,""DOM_""&$A2&""_MIN_" & ratesh1 & ",Detail!$L2,FALSE),IF(AND($A2=""IP"",$B2=""Letter"",$J2=""PR"",$K2=""EXPORT"")," & _
"VLOOKUP(1,IP_EX_PR_L_" & ratesh1 & ",2,FALSE),IF(AND($A2=""IP"",$B2=""Box"",$J2=""PR"",$K2=""EXPORT""), " & _
"VLOOKUP(1,IP_EX_PR_" & ratesh1 & ",2,FALSE),IF(AND($A2=""IE"",$J2=""PR"",$K2=""EXPORT""),VLOOKUP(1,IE_EX_PR_" & ratesh1 & ",2,FALSE), " & _
"IF(AND($A2=""IP"",$K2=""EXPORT"",$B2=""Letter""),VLOOKUP(1,IP_EX_MIN_" & ratesh1 & ",$L2,FALSE),IF(AND($A2=""IP"",$K2=""EXPORT"",$B2=""Pak""), " & _
"VLOOKUP(2,IP_EX_MIN_" & ratesh1 & ",$L2,FALSE),IF(AND($A2=""IP"",$K2=""EXPORT"",$B2=""Box""),VLOOKUP(3,IP_EX_MIN_" & ratesh1 & ",$L2,FALSE), " & _
"IF(AND($A2=""IE"",$K2=""EXPORT""),VLOOKUP(1,IE_EX_MIN_" & ratesh1 & ",$L2,FALSE),IF(AND($A2=""IP"",$K2=""IMPORT"",$B2=""Letter""), " & _
"VLOOKUP(1,IP_IMP_MIN_" & ratesh1 & ",$L2,FALSE),IF(AND($A2=""IP"",$K2=""IMPORT"",$B2=""Pak""),VLOOKUP(2,IP_IMP_MIN_" & ratesh1 & ",$L2,FALSE), " & _
"IF(AND($A2=""IP"",$K2=""IMPORT"",$B2=""Box""),VLOOKUP(3,IP_IMP_MIN_" & ratesh1 & ",$L2,FALSE),IF(AND($A2=""IE"",$K2 " & _
"=""IMPORT""),VLOOKUP(1,IE_IMP_MIN_" & ratesh1 & ",$L2,FALSE),IF(AND($A2=""IPF"",$J2=""PR"",$K2=""EXPORT""), " & _
"VLOOKUP(1,IP_EX_PR_FR_MIN_" & ratesh1 & ",2,FALSE),IF(AND($A2=""IEF"", $J2=""PR"",$K2=""EXPORT""),VLOOKUP(1,IE_EX_PR_FR_MIN_" & ratesh1 & ",2,FALSE), " & _
"IF(AND($A2=""IPF"", $J2=""PR"",$K2=""IMPORT""),VLOOKUP(1,IP_IMP_PR_FR_MIN_" & ratesh1 & ",2,FALSE), IF(AND($A2=""IEF"",$J2=""PR"",$K2=""IMPORT""), " & _
"VLOOKUP(1,IE_IMP_PR_FR_MIN_" & ratesh1 & ",2,FALSE),IF(AND($A2=""IPF"",$K2=""EXPORT""), " & _
"VLOOKUP(151,IPF_EX_MIN_" & ratesh1 & ",$L2,FALSE),IF(AND($A2=""IEF"",$K2=""EXPORT""),VLOOKUP(151,IEF_EX_MIN_" & ratesh1 & ",$L2,FALSE), " & _
"IF(AND($A2=""IPF"",$K2=""IMPORT""),VLOOKUP(151,IPF_IMP_MIN,$L2,FALSE),IF(AND($A2=""IEF"",$K2=""IMPORT""), " & _
"VLOOKUP(151,IEF_IMP_MIN_" & ratesh1 & ",$L2,FALSE),""THEN""))))))))))))))))))))))"

Open in new window

In particular I'm concerned about this part.
""DOM_""&$A2&""_MIN_" & ratesh1

Open in new window

Which named range are you trying to refer to there and where does A2 come into things?
Euro5Author Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.