ActiveCell.Formula Run Time Error 1004 - vba

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.

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

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

Run Time error 1004: Select Method of Worksheet class failed with (varCellContent.Parent.Name)

The code edits, then copies data from a temporary sheet to the current workbook. I am using varCellContent to ask the user to choose the source sheet. I had no trouble when testing the code as I was building it by using F8 to step through (and the code still works perfectly like this).
But when I run the macro normally, I am getting
Run Time error 1004: Select Method of Worksheet class failed
on the following line:
Workbooks(varCellContent.Parent.Name).Worksheets("Sheet1").Select
The following may not be relevant to the cause of the problem but I have read a bit about it while searching for an answer.
It sounds like using .Select is not a good idea, but there are 2 reasons I thought it was useful in this situation.
I don't have to name the sheet for each of the following rows of code that delete and move the columns.
When building and debugging the code I wanted the sheet to be visible so that I could see what was happening as I stepped through each line of code.
So taking those 2 reasons into account is there still a better way to avoid using .Select?
Sub ImportNewData()
'
'
' Import data, save as new workbook
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
' Clear data
Range("A3:I" & LR).ClearContents
' User choose source sheet
Dim varCellContent As Worksheet
Set varCellContent = Application.InputBox _
(prompt:=" " & vbNewLine & "Click any cell in the Aged AP Summary that has been exported from Systematic to Excel, then click OK", Type:=8).Parent
Workbooks(varCellContent.Parent.Name).Worksheets("Sheet1").Select
'Delete unrequired columns
Columns("E:I").Select
Selection.Delete Shift:=xlToLeft
Worksheets("Sheet1").Activate
Columns("E:I").Delete Shift:=xlToLeft

Run time error 1004 placing formula in cell

I have created a formula that works in a spreadsheet but not in VBA. What I have pasted here is only a subset of the entire formula; in total it exceeds 3,000 characters
If I run this, it works
LastRow = Range("a" & Rows.Count).End(xlUp).Row
Range("cI2:cI" & LastRow).Formula = "=CONCATENATE(CONCATENATE(MID(""00"",1,2-LEN(A2)),A2))"
If I run this, I get the run time error
LastRow = Range("a" & Rows.Count).End(xlUp).Row
Range("cI2:cI" & LastRow).Formula = "=CONCATENATE(CONCATENATE(MID(""00"",1,2-LEN(A2)),A2)), " & _
"CONCATENATE(MID(""00000000"",1,8-LEN(B2)),B2),"" "", CONCATENATE(C2,MID("" "",1,12-LEN(C2))),CONCATENATE(MID(""00"",1,2-LEN(D2)),D2) "
Without seeing the rest of the formula, I would suspect that it exceeds some of the internal limits Excel has for function. Possibilities that come to mind are:
Arguments in a function: 255
Nested levels of functions: 64
Length of formula contents: 8,192 characters
You can find more information on the Office support page here.
That said, I'm curious why you would need to build a 3000 character formula to place in the cell. This is what VBA is for - converting the formula into a user defined function is likely going to be the solution.

Error when saving new workbook name

I have been trying for most of the day to get this to work, but I can't figure out what is stopping the save process in this code:
Sheets("DailyReview").Select
Sheets("DailyReview").Copy
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
File_Name = "\\Il-svr\company\department\team\Schedules\Daily_Review_Email\city\" & Range("A110").Value & "\" & Range("D110").Value & "\DailyReview_" & Range("C110").Value & ".xlsx"
ActiveWorkbook.SaveAs Filename:=File_Name
The last line of code gives the error: "Run-time error '1004': Method 'SaveAs' of object '_Workbook' failed. But a file actually does get renamed, just not "saved" to the path location. The path location is completely 100% correct and works fine when I save the macro workbook using the same path location. I am also using Excel 2010 and trying to save it in 2010 format. I have also tried setting the FileFormat to xlWorkbookDefault (51) with no success. I do have permission to save to this path and works fine if I save the new book manually. The ranges are just date values. For instance, the file would save as DailyReview_122914.xlsx if I used this code for today's date. I hope this is enough information. I appreciate any help.
Thank you #RubberDuck. The values were blank for cell A110 which I think was throwing it off. I rewrote the code and discarded the value of cell A110 and it works fine now. It really is the simplest things some time. Thank you!