VBA Application Defined or Object-Defined Error with Formula - excel

I'm trying to use the following formula, but am getting the error application defined or object defined error. I have a feeling it's due to the line break, but it looks good to me.
ActiveCell.FormulaR1C1 = _
"=IF(AND(R[1]C[-13]=RC[-13],R[1]C[-15]=RC[-15]),IF(AND(R[1]C[-8]=RC[-8],R[1]C[-3]<>RC[-3]),IF(AND(R[2]C[-8]=R[1]C[-8],R[2]C[-3]<>R[1]C[-3]),IF(AND(R[3]C[-8]=R[2]C[-8],R[3]C[-3]<>R[2]C[-3]),IF(AND(R[4]C[-13]=R[3]C[-13],R[4]C[-15]=R[3]C[-15]),"""",RC[-3]),IF(AND(R[3]C[-13]=R[2]C[-13],R[3]C[-15]=R[2]C[-15]),"""",RC[-3]))," & _
"IF(AND(R[2]C[-13]=R[1]C[-13],R[2]C[-15]=R[1]C[-15],"""",RC[-3])),""""),IF(RC[-3]="""","""",RC[-3]))"

Related

Converting IF statement to VBA with Variables

I am trying to convert the following IF statement into text that will be filled into a cell through VBA:
=IF(XLOOKUP(N7,'EDL Filter'!R2:R660,'EDL Filter'!AA2:AA660)="YES","Port Specific",XLOOKUP(N7,'EDL Filter'!R2:R660,'EDL Filter'!AB2:AB660) )
I've tried splitting it up as I have with other Xlookup statements below:
ActiveCell = "=IF(XLookUp(N" & SafetyRow2 & ",'EDL Filter'!R2:R660,'EDL Filter'!AA2:AA660)=""Yes"",""Port Specific"", Xlookup(N" & SafetyRow2 & ",'EDL Filter'!AB2:AB660))"
However, I keep getting Run-time Error '1004': Application-defined or object-defined error.
The variables have been defined and used earlier in my code without issue, but I am still new to VBA so I might be missing something obvious.

Converting xlA1 Formula to xlR1C1 gives error

All of my formulas are converting without any issues it's just these following two formulas are giving me an error when I try to convert them to R1C1. Can someone please advise why is this happening ?
"=IF(Control!$B$116=1,"",IF(A24="","",IF('Sub Lot Input'!$L$25="","",IF(OR('Calcs - Conversion'!DJ9="",'Calcs - Conversion'!DJ9="-"),"-",IF(OR(Other_Analysis_Input_Field=4,Other_Analysis_Input_Field=5),MROUND('Calcs - Conversion'!DJ9,'Calcs - Conversion'!$AC$66),'Calcs - Conversion'!DJ9)))))"
"=IF(OR('Control'!$B$235=TRUE,'Control'!$B$236=TRUE),IF('Additional Analysis Input'!$C$14="","Degree C.",'Additional Analysis Input'!$C$14),"")"
I am converting the formula to R1C1 using the following line of code:
Formula = Application.ConvertFormula( _
Formula:=wk_sht.Range(cell).Formula, _
fromReferenceStyle:=xlA1, _
toReferenceStyle:=xlR1C1)
I am getting type mismatch error.

Use the text of a cell as the argument of range() with structured references

I want to execute an advanced filter with vba. The problematic step is that the name of the source table (for the advanced filter) is written in a cell and it seems I can't find a way to pass it to range().
Here is the idea:
The F5 cell contain "DatePlace" and it is the name of the table that should be used in the advanced filter. By recording a macro I saw that we could use structured references in VBA but when I try to take the reference from the value of F5, the advanced filter doesn't execute anymore.
I receive this error message box (translated from french) :
Execution error '1004':
Error defined by the application or by the object
Here is one of my attempts :
Worksheets("Planning")
.Range(Worksheets("Macro").Range("$F$5").Text & "[[#Headers],[#Data]]")
.AdvancedFilter Action _:=xlFilterCopy,
CriteriaRange:=Worksheets("Planning").Range("E7:F8"),
CopyToRange:=Worksheets("Planning").Range( _"E11:F11"), Unique:=False
I tried with .Value instead of .Text. I tried to pass it with a string variable too. I also tried to write the full reference in the F5 cell. And when I tried to get a range variable I just moved the issue to that variable initiation.
Any help is welcomed.
Edit 1 :
This way of doing it works but I have to hard code the structured reference and that's exactly what I want to avoid.
Worksheets("Planning")
.Range("DateLieu[[#Headers],[#Data]]")
.AdvancedFilter Action _:=xlFilterCopy,
CriteriaRange:=Range("A6:B7"),
CopyToRange:=Range( _"A10:B10"), Unique:=False
Edit 2 :
It should have worked without issue. Here is how the code looks now.
' Variables
Dim rngPlanning As String
' Initiations
rngPlanning = ThisWorkbook.Worksheets("Macro").Range("$F$5").Text & "[[#Headers],[#Data]]"
' Actions
Application.CutCopyMode = False
Debug.Print (rngPlanning)
Worksheets("Planning").Range(rngPlanning).AdvancedFilter _
Action:=xlFilterCopy, _
CriteriaRange:=Worksheets("Macro").Range("$E$7:$F$8"), _
CopyToRange:=Worksheets("Macro").Range("$E$11:$F$11"), _
Unique:=False

VBA Pivot Table error "Invalid procedure call or argument"

Please help me debug this code.
I am getting the following error:
"Invalid procedure call or argument"
Code:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"MainSheet!R1C1:R" & lastRow & "C9", Version:=xPivotTableVersion15).CreatePivotTable TableDestination:="Pivot Sheet!R2C1", TableName:="Compile table", DefaultVersion:=xlPivotTableVersion15
xPivotTableVersion15 looks like a typo and
TableDestination:="'Pivot Sheet'!A1"
sheet names with spaces need to be enclosed in single quotes
I did a macro in Excel 2016 and a user on an older version of Excel got this error. In my code it had Version:=6 and DefaultVersion:=6. When I changed this to be xlPivotTableVersion14 it worked fine.

VBA: Application.WorksheetFunction.Max Runtime Error 1004

I can't seem to figure out what is wrong with my code. I am trying to set the maximum of a row to a variable lMax. This variable is declared earlier by "Dim lMax".
I try running the code and get the error: "Run=time error '1004': Method 'Cells' of object '_Global' failed. The code that fails is as follows:
lMax = Application.WorksheetFunction.Max( _
Worksheets("Data").Range(Cells(Selected_Row, 7), Cells(Selected_Row, 14).End(xlToRight))) 'Max value
The variable Selected_Row is the row the active row that the user was last on. It is a Global Long variable and was declared in the beginning of the module.
Please help! Thank you!

Resources