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

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 ".

Related

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

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

Excel VBA .FormulaArray Error

The error is a 1004 error but I don't understand why. The formula in the cell would be:
{=INDEX(KPI!A:AQ,MATCH(1,(KPI!A:A=Monday!$K$1)*(KPI!C:C=Monday!B4),0),37)+INDEX(KPI!A:AQ,MATCH(1,(KPI!A:A=Monday!$K$1)*(KPI!C:C=Monday!B4),0),38)-INDEX(KPI!A:AQ,MATCH(1,(KPI!A:A=Monday!$K$1)*(KPI!C:C=Monday!B4),0),39)}
To get this formula into a macro I simply used the recording tool but it did not work.
Selection.FormulaArray = _
"=INDEX(KPI!C[-3]:C[39],MATCH(1,(KPI!C[-3]=Monday!R1C11)*(KPI!C[-1]=Monday!RC[-2]),0),37)+INDEX(KPI!C[-3]:C[39],MATCH(1,(KPI!C[-3]=Monday!R1C11)*(KPI!C[-1]=Monday!RC[-2]),0),38)-INDEX(KPI!C[-3]:C[39],MATCH(1,(KPI!C[-3]=Monday!R1C11)*(KPI!C[-1]=Monday!RC[-2]),0),39)"
As long as you insist on doing things the hard way,
Option Explicit
Sub wqer()
With ThisWorkbook
.Worksheets("KPI").Name = "K"
.Worksheets("Monday").Name = "M"
'geez - decide what cell you want without Selection¹ !!??!!
Selection.FormulaArray = _
"=INDEX(K!C[-3]:C[39],MATCH(1,(K!C[-3]=M!R1C11)*(K!C[-1]=M!RC[-2]),0),37)+INDEX(K!C[-3]:C[39],MATCH(1,(K!C[-3]=M!R1C11)*(K!C[-1]=M!RC[-2]),0),38)-INDEX(K!C[-3]:C[39],MATCH(1,(K!C[-3]=M!R1C11)*(K!C[-1]=M!RC[-2]),0),39)"
.Worksheets("K").Name = "KPI"
.Worksheets("M").Name = "Monday"
End With
End Sub
Renaming your worksheets back and forth puts your formula under the built-in character limit² noted by Axel Richter for the time it takes to insert the array formula into the cell. This will put an undue calculation demand on your workbook but it may be an acceptable solution for you. Consider changing the application's calculation mode to manual for the duration of the operation.
.¹ See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals.
² See Range.FormulaArray property.
You can also try this(
please don't run it from the VBE, try to run it from sheets environment. Go to Developer-Macros-Your Macro -Run or Run it from a button or shortcut and it will work without problem):
Selection.Formula = _
"=INDEX(KPI!C[-3]:C[39],MATCH(1,(KPI!C[-3]=Monday!R1C11)*(KPI!C[-1]=Monday!RC[-2]),0),37)+INDEX(KPI!C[-3]:C[39],MATCH(1,(KPI!C[-3]=Monday!R1C11)*(KPI!C[-1]=Monday!RC[-2]),0),38)-INDEX(KPI!C[-3]:C[39],MATCH(1,(KPI!C[-3]=Monday!R1C11)*(KPI!C[-1]=Monday!RC[-2]),0),39)"
SendKeys "{F2}"
SendKeys "^+{ENTER}"

repost: copy pasting data instead of formula excel VBA

ok i have to repost this as my original thread was tagged as a duplicate with "the original" thread not having anything to do with my issue and the person who tagged it as a dupe cannot provide the answer.
there's a reason why i'm using activecell in my code as it does something with interaction with outlook and validation of certain conditions from other cells.
ActiveCell.Cells(1, 37).Copy Destination:=wsTest.Range("A" & Rows.Count).End(xlUp).Offset(1)
now i tried :
ActiveCell.Cells(1, 37).Copy Destination:=wsTest.Range("A" & Rows.Count).End(xlUp).Offset(1).pastespecial
but i'm getting error 1004.
if someone will point me here: how to paste only values in excel
please tell me which of the 4 answers will actually work, because none of them is working on my end.
Reverse the procedure and pass over the value directly.
wsTest.Range("A" & Rows.Count).End(xlUp).Offset(1) = ActiveCell.Cells(1, 37).value
The .Value is the raw underlying value in a cell or the result from a formula if the cell contains a formula. .Value2 strips the value further where a date like 18-Feb-2015 becomes 42,053. If you have used number formatting in the cell (e.g. 18-Feb-2015) and wish to retain it, use .Text in place of .Value.

ActiveCell.Formula Run Time Error 1004

First off apologies for the unsightly formulae below - I'm not sure how I can make them easier to read without potentially removing required info for a solution.
I'm trying to automate some process in Excel, and am getting
run time error 1004
each time I try to step through the vba to populate the active cell with either of the following formulae. The formulae have been tested in Excel (I then recorded the entry and put into the vba).
1)
ActiveCell.FormulaR1C1 = _
"=16*POWER(SQRT(((SUMIFS(C[-5],C[-2],RC[-2],C[-1],RC[-1])-RC[-5])/(SUMIFS(C[-6],C[-2],RC[-2],C[-1],RC[-1])-RC[-6]))*(1-((SUMIFS(C[-5],C[-2],RC[-2],C[-1],RC[-1])-RC[-5])/(SUMIFS(C[-6],C[-2],RC[-2],C[-1],RC[-1])-RC[-6]))))/(((SUMIFS(C[-5],C[-2],RC[-2],C[-1],RC[-1])-RC[-5])/(SUMIFS(C[-6],C[-2],RC[-2],C[-1],RC[-1])-RC[-6]))*((RC[-5]/RC[-6])-((SUMIFS(C[-5],C[-2],RC[-2],C[" & _
"1])-RC[-5])/(SUMIFS(C[-6],C[-2],RC[-2],C[-1],RC[-1])-RC[-6])))/((SUMIFS(C[-5],C[-2],RC[-2],C[-1],RC[-1])-RC[-5])/(SUMIFS(C[-6],C[-2],RC[-2],C[-1],RC[-1])-RC[-6]))),2)"
2)
ActiveCell.FormulaR1C1 = _
"=IF(AND(IFERROR(IF(OR(NORMDIST((((SUMIFS(C[-7],C21,RC21,C22,RC22)-RC[-7])/(SUMIFS(C[-8],C21,RC21,C22,RC22)-RC[-8]))-(RC[-7]/RC[-8]))/SQRT(POWER(SQRT((((SUMIFS(C[-7],C21,RC21,C22,RC22)-RC[-7])/(SUMIFS(C[-8],C21,RC21,C22,RC22)-RC[-8]))*(1-((SUMIFS(C[-7],C21,RC21,C22,RC22)-RC[-7])/(SUMIFS(C[-8],C21,RC21,C22,RC22)-RC[-8])))/(SUMIFS(C[-8],C21,RC21,C22,RC22)-RC[-8]))),2)+" & _
"RT(((RC[-7]/RC[-8])*(1-(RC[-7]/RC[-8]))/RC[-8])),2)),0,1,TRUE)<(1-cpanel!R4C3),NORMDIST((((SUMIFS(C[-7],C21,RC21,C22,RC22)-RC[-7])/(SUMIFS(C[-8],C21,RC21,C22,RC22)-RC[-8]))-(RC[-7]/RC[-8]))/SQRT(POWER(SQRT((((SUMIFS(C[-7],C21,RC21,C22,RC22)-RC[-7])/(SUMIFS(C[-8],C21,RC21,C22,RC[-3])-RC[-8]))*(1-((SUMIFS(C[-7],C21,RC21,C22,RC[-3])-RC[-7])/(SUMIFS(C[-8],C21,RC21,C22,R" & _
"8])))/(SUMIFS(C[-8],C21,RC21,C22,RC22)-RC[-8]))),2)+POWER(SQRT(((RC[-7]/RC[-8])*(1-(RC[-7]/RC[-8]))/RC[-8])),2)),0,1,TRUE)>cpanel!R4C3), ""YES"", ""NO""),""NO"")=""YES"",RC[-8]>RC[-2]),IF(RC[-1]=TRUE,""WINNER"",""LOSER""),""NOTSIG"")"
Is there any basic syntax that I'm misunderstanding here?
Thanks in advance for your time.

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.