1004 error when trying to run a macro that applies to a table - vba

I have an existing table. I want to add a new column next to it, with a formula in it so that the formula adds a new column and fills in all the values.
This is a two-page worksheet. Table1 is on another page than the table we're working with, but has the master data it draws from.
I recorded a macro of the formula I used to produce this, but when I try to run it, I get
Run time error '1004': Application-defined or object-defined error.
The code I'm using is below:
Range("B2").Select
ActiveCell.FormulaR1C1 = "=IF(INDEX(Table1,MATCH([#Listing],Table1[Property],0)," & _
"MATCH(""Status"",Table1[#Headers],0))=""for sale"",""seller""," & _
"IF(INDEX(Table1,MATCH([#Listing],Table1[Property],0)," & _
"MATCH(""Status"",Table1[#Headers],0))=""for lease"",""landlord""," & _
"IF(INDEX(Table1,MATCH([#Listing],Table1[Property],0)," & _
"MATCH(""Status"",Table1[#Headers],0))=""for sale or lease"",""seller / landlord""" & _
"X(Table1,MATCH([#Listing],Table1[Property],0)," & _
"MATCH(""Base Rent/Mo"",Table1[#Headers],0))>0,""landlord"",""seller""))))"
Range("B1").Select
Things I've checked so far:
All the names match (i.e. Table1 is a valid name, and all the headers are correctly named)
The formula works exacly as I want to if I just type it in and hit 'enter'
I've looked for similar issues here, and the most similar seem to be some problems with Pivot tables, but the solutions don't seem to be applicable to my problem
(The reason I'm using VBA for this is that this is one part of a multi-step process that I'm trying to automate to make it simpler to run a complicated report from a large set of data.)
EDIT: Additional fixes I tried:
Moving the master data table to the same sheet as my 'target' table to see if it would work if the two were on the same worksheet. No go.

When you need to use quotes inside quotes like in "MATCH(""Status"", which I suppose you are trying to output Match("Status" try using "MATCH("&Chr(34)&Status&Chr(34)
Chr(34) outputs the " symbol. Otherwise it would return MATCH(Status (without quotes)

There were two separate issues.
First, I was recording the macro and apparently, Excel will not record formulas longer than a certain length properly. This limit is greater than 407 (the longest formula I got to auto capture), but shorter than 467 characters. A similar problem was discussed on this Mr. Excel post. This also was why part of the "INDEX" was missing, as BruceWayne pointed out.
In addition, it was necessary to switch from .FormulaR1C1 to .Formula to get it to work correctly, as R3uK had suggested.
The final code ended up as follows:
Range("B2").Select
ActiveCell.Formula = _
"=IF(INDEX(Table1,MATCH($A2,Table1[Property],0),MATCH(""Status"",Table1[#Headers],0))=""for sale"",""seller"",IF(INDEX(Table1,MATCH($A2,Table1[Property],0),MATCH(""Status"",Table1[#Headers],0))=""for lease"",""landlord"",IF(INDEX(Table1,MATCH($A2,Table1[Property],0),MATCH(""Status"",Table1[#Headers],0))=""for sale or lease"",""seller / landlord"",IF(INDEX(Table1,MATCH($A2,Table1[Property],0),MATCH(""Base Rent/Mo"",Table1[#Headers],0))>0,""landlord"",""seller""))))"
Range("B3").Select

Related

Too many IF statements Excel / VBA

I have added some sample data in this file to explain my problem a bit better. I have blended out the columns which have no relevance to the formula: https://docs.google.com/spreadsheets/d/1_lq0mYPF2ICiFgehQz_YkOa28JUyamG4G8ObV957onY/edit?usp=sharing
My initial file has one row per contract. I want to end up with one row per customer ID. I need to only keep the most recent contract, so the contract with the end date closest to Now(). If one customer ID has two contracts ending at the same time, I would like to keep the row with the higher monthly invoice amount.
I have been doing this with IF statements but there must be a more efficient way. At the moment the highest number of contracts a customer has with 3, but this will increase in the coming months and my if statement method will no longer be viable.
Here is my current formula.
Range("P2").Select
ActiveCell.FormulaR1C1 = _
"=IF(OR(Status=""in renewing process"",Status=""first contract still running""),""Not Eligable"",IF(Count_of_ID=1,""keep"",IF(Count_of_ID=2,(IF(AND(RC[-15]=R[1]C[-15],MIN(RC[-1],R[1]C[-1])=RC[-1]),""keep"",""delete"")),IF(Count_of_ID=3,IF(AND(RC[-15]=R[1]C[-15],RC[-15]=R[2]C[-15],MIN(RC[-1],R[1]C[-1],R[2]C[-1])=RC[-1]),""keep"",""delete"")))))"
Range("P3").Select
ActiveCell.FormulaR1C1 = _
"=IF(OR(Status=""in renewing process"",Status=""first contract still running""),""Not Eligable"",IF(Count_of_ID=1,""keep"",IF(Count_of_ID=2,(IF(AND(RC[-15]=R[1]C[-15],MIN(RC[-15],R[1]C[-15])=RC[-15]),""keep"",IF(Count_of_ID=2,(IF(AND(RC[-15]=R[-1]C[-15],MIN(RC[-15],R[-1]C[-15])=RC[-15]),""keep"",""delete"")))),IF(Count_of_ID=3,IF(AND(RC[-15]=R[1]C[-15],RC[-15]=R[2]C[" & _
"-15],MIN(RC[-15],R[1]C[-15],R[2]C[-15])=RC[-15]),""keep"",IF(Count_of_ID=3,IF(AND(RC[-15]=R[1]C[-15],RC[-15]=R[-1]C[-15],MIN(RC[-15],R[1]C[-15],R[-1]C[-15])=RC[-15]),""keep"",""delete""))))))))" & _
""
Range("P4").Select
ActiveCell.FormulaR1C1 = _
"=IF(OR(Status=""in renewing process"",Status=""first contract still running""),""Not Eligable"",IF(Count_of_ID=1,""keep"",(IF(AND(Count_of_ID=2,RC[-15]=R[1]C[-15],MIN(RC[-1],R[1]C[-1])=RC[-1]),""keep"",IF(AND(Count_of_ID=2,RC[-15]=R[-1]C[-15],MIN(RC[-1],R[-1]C[-1])=RC[-1]),""keep"",IF(AND(Count_of_ID=3,RC[-15]=R[1]C[-15],RC[-15]=R[2]C[-15],MIN(RC[-1],R[1]C[-1],R[2]C" & _
"[-1])=RC[-1]),""keep"",IF(AND(Count_of_ID=3,RC[-15]=R[1]C[-15],RC[-15]=R[-1]C[-15],MIN(RC[-1],R[1]C[-1],R[-1]C[-1])=RC[-1]),""keep"",IF(AND(Count_of_ID=3,RC[-15]=R[-1]C[-15],RC[-15]=R[-2]C[-15],MIN(RC[-1],R[-1]C[-1],R[-2]C[-1])=RC[-1]),""keep"",""delete""))))))))" & _
""
Range("P4").Select
Selection.AutoFill Destination:=Range("P4:P" & Lastrow)
Any help much appreciated!
I would use Named Formulae. They are not widely used in Excel but very powerful. As an example I reduced
=IF(OR(Status=""in renewing process"",Status=""first contract still running"")
to a single word returning true or false using a named formula. I have used relative addressing, excel handles this for you. The attached image should give you all the information you need to use it. I clicked on B2 and built the formula as normal, tested it then copied it, right clicked >DefineName, gave it a name and pasted the formula into 'refers to'.
To use it then just add the Name into the cell and Excel handles all the relative addressing stuff.

Why is my auto fill not working?

Hoping you can help with the below, I've no idea why this isn't working and I can't seem to figure it out. After some Googling, I can't even find another example of this issue.
Essentially the code should take data on one page, place HLOOKUPS in another page to sort everything into the right columns (all working fine). Then once that's done, it should auto fill down using the row count.
The problem I have, is that it is auto filling on the wrong sheet (it might be worth calling out that the sheet it fills is the same one the code is in and where the rowcnt is.
I tried to explicitly call out the sheet I want to use as such: Range("A2:V2").AutoFill Destination:=Sheets(5).Range("A3:V" & rowcnt), Type:=xlFillDefault but this then throws an Application-defined or object-defined error on the fill line of code.
Public Sub FormatData()
rowcnt = Application.WorksheetFunction.CountA(Sheet4.Range("B:B")) + 1
With Sheets("Final Datasets")
.Cells(2, "A").FormulaR1C1 = _
"=HLOOKUP(""oOrder_date"",'Teradata Downloads'!R1:R1048576,ROW('Final Datasets'!RC),0)"
[snip] load more of the same as above [/snip]
'FILL
Range("A2:V2").AutoFill Destination:=Range("A3:V" & rowcnt), Type:=xlFillDefault
End With
End Sub
I'm properly puzzled here, so any help you can give as to how to fix this (and more importantly, why it's happening) would be greatly appreciated.
if you have the AutoFill in the With Statement, try to use the "." before the Range.
.Range("A2:V2").AutoFill Destination:=.Range("A3:V" & rowcnt)

Cannot run VBA Because of Syntax Error (when recording) & Object-Defined Eerror (when running)

I am getting the same error as Dennis did here, but the answer that he provided simply said to remove some modules or rename them. However, I only have one module, and renaming it did nothing for me. I even tried making a whole new workbook and copying the macro over to that for a fresh start, but it also did nothing. When I go to run the macro after it was recorded, it also gives me an "application-defined or object-defined" error. The formula works by itself completely fine outside of the macro, but I do need it as a part of the macro.
Here is what excel recorded, and this is the line in my macro that is giving me the error:
ActiveCell.FormulaR1C1 = _
"=IF(COUNTA(RC[-15]:RC[-2])=2,RC[-14],IF(COUNTA(RC[-15]:RC[-2])=4,CONCATENATE(RC[-14],"":"",RC[-12]),IF(COUNTA(RC[-15]:RC[-2])=6,CONCATENATE(RC[-14],"":"",RC[-12],"" - "",RC[-10]),IF(COUNTA(RC[-15]:RC[-2])=8,CONCATENATE(RC[-14],"":"",RC[-12],"":"",RC[-10],"" - "",RC[-8]),IF(COUNTA(RC[-15]:RC[-2])=10,CONCATENATE(RC[-14],"":"",RC[-12],"":"",RC[-10],"":"",RC[-8],"" - """ & _
","""")))))"
It has a Range("Q3").Select prior to that, which activates that cell. Furthermore, if you look at what it recorded vs. the original formula, you can see it leaves off the K3) or RC[-6]),. I tried to add it, but it did not help.
Original Formula:
=IF(COUNTA(B3:O3)=2,C3,IF(COUNTA(B3:O3)=4,CONCATENATE(C3,":",E3),IF(COUNTA(B3:O3)=6,CONCATENATE(C3,":",E3," - ",G3),IF(COUNTA(B3:O3)=8,CONCATENATE(C3,":",E3,":",G3," - ",I3),IF(COUNTA(B3:O3)=10,CONCATENATE(C3,":",E3,":",G3,":",I3," - ",K3),"")))))
I also tried the formula without the concatenate, but it did not help either. It is below:
ActiveCell.FormulaR1C1 = _
"=IF(COUNTA(RC[-15]:RC[-2])=2,RC[-14],IF(COUNTA(RC[-15]:RC[-2])=4,RC[-14]&"":""&RC[-12],IF(COUNTA(RC[-15]:RC[-2])=6,RC[-14]&"":""&RC[-12]&"" - ""&RC[-10],IF(COUNTA(RC[-15]:RC[-2])=8,RC[-14]&"":""&RC[-12]&"":""&RC[-10]&"" - ""&RC[-8]),IF(COUNTA(RC[-15]:RC[-2])=10,RC[-14]&"":""&RC[-12]&"":""&RC[-10]&"":""&RC[-8]&"" - ""&RC[-6],"""")))))"
Edit: Here is the code directly above, and the P3 formula works fine:
Range("P3").Select
ActiveCell.FormulaR1C1 = _
"=INDEX(RC[-14]:RC[-1],1,IF(COUNT(RC[-14]:RC[-1])=1,COUNT(RC[-14]:RC[-1]),COUNT(RC[-14]:RC[-1])*2-1))"
Range("P3").Select
Range("P3").AutoFill Destination:=Range("P3:P" & LastRow)
Range("Q3").Select
ActiveCell.FormulaR1C1 = _
"=IF(COUNTA(RC[-15]:RC[-2])=2,RC[-14],IF(COUNTA(RC[-15]:RC[-2])=4,RC[-14]&"":""&RC[-12],IF(COUNTA(RC[-15]:RC[-2])=6,RC[-14]&"":""&RC[-12]&"" - ""&RC[-10],IF(COUNTA(RC[-15]:RC[-2])=8,RC[-14]&"":""&RC[-12]&"":""&RC[-10]&"" - ""&RC[-8]),IF(COUNTA(RC[-15]:RC[-2])=10,RC[-14]&"":""&RC[-12]&"":""&RC[-10]&"":""&RC[-8]&"" - ""&RC[-6],"""")))))"
Range("Q3").Select
Range("Q3").AutoFill Destination:=Range("Q3:Q" & LastRow)
I am using Microsoft Office 2013, and there is an ASAP Utilites add-on, which has never caused me a problem before, if that helps at all.
the formula itself contains an error:
ActiveCell.FormulaR1C1 = "=IF(COUNTA(RC[-15]:RC[-2])=2,RC[-14],IF(COUNTA(RC[-15]:RC[-2])=4,RC[-14]&"":""&RC[-12],IF(COUNTA(RC[-15]:RC[-2])=6,RC[-14]&"":""&RC[-12]&"" - ""&RC[-10],IF(COUNTA(RC[-15]:RC[-2])=8,RC[-14]&"":""&RC[-12]&"":""&RC[-10]&"" - ""&RC[-8]),IF(COUNTA(RC[-15]:RC[-2])=10,RC[-14]&"":""&RC[-12]&"":""&RC[-10]&"":""&RC[-8]&"" - ""&RC[-6],"""")))))"
should be
ActiveCell.FormulaR1C1 = "=IF(COUNTA(RC[-15]:RC[-2])=2,RC[-14],IF(COUNTA(RC[-15]:RC[-2])=4,RC[-14]&"":""&RC[-12],IF(COUNTA(RC[-15]:RC[-2])=6,RC[-14]&"":""&RC[-12]&"" - ""&RC[-10],IF(COUNTA(RC[-15]:RC[-2])=8,RC[-14]&"":""&RC[-12]&"":""&RC[-10]&"" - ""&RC[-8],IF(COUNTA(RC[-15]:RC[-2])=10,RC[-14]&"":""&RC[-12]&"":""&RC[-10]&"":""&RC[-8]&"" - ""&RC[-6],"""")))))"
at the ...RC[-8]),IF... there shouldn't be a )!
If you delete it, the macro runs without fail.
Still: I suggest changing your formula
=IF(COUNTA(B3:O3)=2,C3,IF(COUNTA(B3:O3)=4,CONCATENATE(C3,":",E3),IF(COUNTA(B3:O3)=6,CONCATENATE(C3,":",E3," - ",G3),IF(COUNTA(B3:O3)=8,CONCATENATE(C3,":",E3,":",G3," - ",I3),IF(COUNTA(B3:O3)=10,CONCATENATE(C3,":",E3,":",G3,":",I3," - ",K3),"")))))
to
=IFERROR(CHOOSE(COUNTA(B3:O3)/2,C3,C3&":"&E3,C3&":"&E3&" - "&G3,C3&":"&E3&":"&G3&" - "&I3,C3&":"&E3&":"&G3&":"&I3&" - "&K3),"")
which is shorter ;)
For the part of "false" recording: If excel "translates" your formula to R1C1, and the string is cut into multiple rows, then sometimes parts are missing. For that case (if you notice the formula is cut to multiple rows) simply select the cell and run in immediate window: ?ActiveCell.FormulaR1C1 to get the formula in R1C1. But do not forget to double up all ".

Excel VBA - AdvancedFilter

I am trying to filter a range dynamically in VBA and the VBA I am using is not working but I cannot see a logical reason as to why. To explain, I have a range of data in a sheet entitled "Full Stock Report" the size of which will change but I've set it statically in this example... And I'm trying to filter it by a list of criteria held in a range on a sheet initiated "Spitfire Aval Locations", again this is also dynamic but I've set as static again in this example. This sounds simple to me but the below line of code applies a filter but with no results (I have checked I know there are lots that should appear from this filter).
My second question is related, how does this VBA statement dictate which column in the range is being filtered ? (I fear this may be my issue ....)
Sheets("Full Stock Report").Range("A1:F20623").AdvancedFilter Action:=xlFilterInPlace,
CriteriaRange:=Sheets("Spitfire Aval Locations").range("A2:A228"), Unique:=False
Think I've solved this ... essentially AdvancedFilter requires the criteria to be the same format and same column titles as your data set. Not hugeley helpful to me, but I can bodge it to work.
I also have a hunch that AutoFilter with specified criteria might be a better option...
The column to filter on is the first .Range that you call .AdvancedFilter on. The code you posted filters columns A through F. If you wanted to only filter based on values in column A, it would look more like this:
Sheets("Full Stock Report").Range("A1:A20623").AdvancedFilter _
Action:=xlFilterInPlace, _
CriteriaRange:=Sheets("Spitfire Aval Locations").Range("A2:A228"), _
Unique:=False

How do I convert this Excel formula into VBA code?

'I am using this formula in an Excel worksheet, in cell A6. It is working fine.
=IF(O6="Hand","Manual Entry",IF(O6="JET",R6,IF(O6="COKE","Red Bull",IF(O6="Freight","Logistics",IF(O6="TAX","Tax",IF(O6="TRANSFER COST","Transfer Cost Transactions",IFERROR(IF(FIND("INV#",R6,1)>=1,MID(R6,FIND("INV#",R6,1),10),""),"")))))))
Now, my question is: how do I convert this to VBA? I have tried recording it, and the code is as follows:
ActiveCell.FormulaR1C1 = _
"=IF(RC[14]=""Hand"",""Manual Entry JE"",IF(RC[14]=""JET"",RC[17],IF(RC[14]=""COKE"",""Red Bull"",IF(RC[14]=""FREIGHT"",""Logistics"",IF(RC[14]=""TAX"",""Tax"",IF(RC[14]=""TRANSFER COST"",""Transfer Cost Transactions"",IFERROR(IF(FIND(""INV#"",RC[17],1)>=1,MID(RC[17],FIND(""INV#"",RC[17]" & _
"""""),"""")))))))"
When I run this, I am receiving Run Time Error 1004: Application-defined or object-defined error.So I Changed this to something like this, this doing same as above formula except the find option, everything is running fine.
![VBA For Above formula][Any Help on the find?]
End sub.How do i get the fine option in the above VBA code.`
This works for me:
Range("L6").Formula = "=IF(O6=""Hand"",""Manual Entry"",IF(O6=""JET"",R6,IF(O6=""COKE"",""Red Bull"",IF(O6=""Freight"",""Logistics"",IF(O6=""TAX"",""Tax"",IF(O6=""TRANSFER COST"",""Transfer Cost Transactions"",IFERROR(IF(FIND(""INV#"",R6,1)>=1,MID(R6,FIND(""INV#"",R6,1),10),""""),"""")))))))"
This is just your original Excel formula, but with the " characters escaped as "".
You need to escape your quotes.
ActiveCell.Formula = "=IF(O6=""Hand"",""Manual Entry"",IF(O6=""JET"",R6,IF(O6=""COKE"",""Red Bull"",IF(O6=""Freight"",""Logistics"",IF(O6=""TAX"",""Tax"",IF(O6=""TRANSFER COST"",""Transfer Cost Transactions"",IFERROR(IF(FIND(""INV#"",R6,1)>=1,MID(R6,FIND(""INV#"",R6,1),10),""""),"""")))))))"
And use .Formula since you're using specific cell names that are not relative to the currently selected cell.