VBA Formula with Custom Variables Syntax Help

  • Thread starter Thread starter Legacy 185734
  • Start date Start date
L

Legacy 185734

Guest
Hey All,

I'm normally pretty good with most programming, but I cant seem to figure out what is wrong with my formula in VBA. I've tried a few things/different variations so apologies if the formula is now completely messed up! :p

Code:
Dim columnNumber As Integer
Dim columnNumber1 As Integer
Dim columnNumber2 As Integer

Dim columnLetter As String
Dim columnLetter1 As String
Dim columnLetter2 As String

columnNumber = 33 'AG - Fixed row number for vlookups
columnNumber1 = 33 'AG
columnNumber2 = 18 'R

columnLetter = Left(Cells(1, columnNumber).Address(1, 0), InStr(1, Cells(1, columnNumber).Address(1, 0), "$") - 1)
columnLetter1 = Left(Cells(1, columnNumber1).Address(1, 0), InStr(1, Cells(1, columnNumber1).Address(1, 0), "$") - 1)
columnLetter2 = Left(Cells(1, columnNumber2).Address(1, 0), InStr(1, Cells(1, columnNumber2).Address(1, 0), "$") - 1)

Range(columnLetter1 + "4").Select
ActiveCell.Formula = _
    "=IFERROR(IF(" & columnLetter1 + 7 & "/(" &  columnLetter1 + 5 & "*oee)"LESS THAN"VLOOKUP(" & columnLetter + 1 &  ",LineHours,4,FALSE)*'Look Up'!" & columnLetter2 + 5 & ","  & columnLetter1 + 7 & "/(" & columnLetter1 + 5 &  "*oee),VLOOKUP(" & columnLetter + 1 & ",LineHours,4,FALSE)*'Look  Up'!" & columnLetter2 + 5 & "),"")"
So yea, in this case, the formula should look like this:

Code:
=IFERROR(IF(AG7/(AG5*oee)"LESS THAN"VLOOKUP(AG1,LineHours,4,FALSE)*'Look  Up'!R5,AG7/(AG5*oee),VLOOKUP(AG1,LineHours,4,FALSE)*'Look  Up'!R5),"")
Now when I debug I can see that the formula is correct in what I want it to look like, but I'm getting an error.

Please note that when entering code, it kept ignoring everything after a "<", so I have replaced "<" with ""LESS THAN"". In the actual code there is a "<" (without quotations).

Hope someone can clear the syntax issue or whatever it is I'm doing wrong.

Cheers.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Yea, maybe I should have explained that - I just figured you'd know that I've already changed it and left it like that on purpose - apparently not.

Well I changed it to be like this:

Code:
ActiveCell.Formula = _
    "=IFERROR(IF(" & columnLetter1 + "7" & "/(" &  columnLetter1 + "5" & "*oee)"LESS THAN"VLOOKUP(" & columnLetter + "1" &  ",LineHours,4,FALSE)*'Look Up'!" & columnLetter2 + "5" & ","  & columnLetter1 + "7" & "/(" & columnLetter1 + "5" &  "*oee),VLOOKUP(" & columnLetter + "1" & ",LineHours,4,FALSE)*'Look  Up'!" & columnLetter2 + "5" & "),"") "
But it gives another error..

If you could correct the formula line with the correct "'s or &s or +s, that would be great.

Thanks. :)


[Edit]
Oh and "columnLetter1 + 7" equates to cell AG7.
 
Last edited by a moderator:
Upvote 0
& columnLetter2 + 5 & "),"""")" the end could do with 4 " in a row
why bother with columnLetter variables if its all fixed?
 
Last edited:
Upvote 0
Thanks for the help! That worked :) Didn't realise an extra "" were needed on the end.

Ok now I'm getting problems with my next formula. Whats wrong with it?

Code:
ActiveCell.Formula = _
            "=IF(" & columnLetter1 + "4" & "=IFERROR(VLOOKUP(" & columnLetter + "1" & ",LineHours,4,FALSE)*'Look Up'!" & columnLetter2 + "5" & ",""),"",IFERROR((VLOOKUP(" & columnLetter + "1" & ",LineHours,4,FALSE)*'Look Up'!" & columnLetter2 + "5" & ") - " & columnLetter1 + "4" & ",VLOOKUP(" & columnLetter + "1" & ",LineHours,4,FALSE)*'Look Up'!" & columnLetter2 + "5" & "))"
Where would the extra ""'s need to be..cus I've tried adding them but it didn't fix it.

Once this formula is fixed, my document will be complete, so thank you very much again! :)

And they aren't fixed variables. I'm just showing you a snippet of the code.
 
Upvote 0
Yea I found where to add the extra """".

Thanks for the help, all working now :).
 
Upvote 0
Please note that when entering code, it kept ignoring everything after a "<", so I have replaced "<" with ""LESS THAN"". In the actual code there is a "<" (without quotations).

If you insert a single space after a < then the forum software works properly:-
Code:
=IF(A< 100,"Yes","No")

I know, it's annoying.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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