Substitution for MAXIFS without array formula - excel

After a query is completed, I am inserting the following formula into the sheet with the query data using the vba code below. All works great on Excel Office365 but if the version of Excel is 2016 standalone the formula fails with a #NAME error as this function is not available in that version. I have some users that are stuck with it.
I know that a formula array could replace this, but I am not sure how to do this and insert it with code, as well as what the most efficient formula is that could replace this one.
=IF(OR(ISERROR(MAXIFS(Consumed!D:D,Consumed!B:B,A2)),
MAXIFS(Consumed!D:D,Consumed!B:B,A2)=0),"",
MAXIFS(Consumed!D:D,Consumed!B:B,A2))
Any help appreciated.
strInsertFormula = "=IF(OR(ISERROR(MAXIFS(Consumed!D:D,Consumed!B:B,A2)),MAXIFS(Consumed!D:D,Consumed!B:B,A2)=0),"""",MAXIFS(Consumed!D:D,Consumed!B:B,A2))"
With Sheet3
.Range("Individual_Bottles").Columns(.Range("EndRng").Offset(0, 1).Column).Insert Shift:=xlToRight
.Range("EndRng").Offset(-1, 1).Cells(1, 1).Value = "Last Drank"
.Range("EndRng").Offset(0, 1).Formula = strInsertFormula
.Range("EndRng").Offset(0, 1).NumberFormat = "yy/mm/dd"
End With

You can use the Excel Array formula =MAX(IF(B:B=A2,D:D,"")) to find the conditional maximum (you can add the extra error checks etc. around this). This will work in all versions from 2016 and earlier.
If you type this into a cell, you do need to press the usual Crtl+Shift+Enter (known as CSE).
If you want to enter this array formula via code, you need to set the FormulaArray property of the range to the above, NOT the Formula property. So your code should read:
.Range("EndRng").Offset(0, 1).FormulaArray = strInsertFormula
where strInsertFormula is the array formula I mentioned above.

#JohnF Well I got it working thanks to John F. I had expected that I would have to use a loop but was hoping not to. In the end I did, as I believe the FillDown method requires the range to be selected and I did not want to do that. Here is the final code
ii = .Range("EndRng").rows.Count
ConsRows = Consumed.Range("Consumed").rows.Count
For i = 1 To ii
strInsertFormula = "=IF(IFERROR(MAX(IF(Consumed!B1:B" & ConsRows & "=" & _
.Range("A1").Offset(i, 0).Address & _
",Consumed!D1:D" & ConsRows & ","""")),0)>0,MAX(IF(Consumed!B1:B" & ConsRows & "=" & _
.Range("A1").Offset(i, 0).Address & _
",Consumed!D1:D" & ConsRows & ","""")),"""")"
.Range("EndRng").Offset(0, 1).Cells(i, 1).FormulaArray = strInsertFormula
Next
End If

Related

Having fits with a formula which includes a variable

I have been trying to put a SUMIF formula into a cell but it returns a weird result.
BottomRow winds up being the last row in a table. When I run the macro and look at the value of txtformula in the immediate window I get =Sumif(B6:B241,F6:F19,D6:D241)
when it goes in the cell it enters =SUMIF(B6:B241,#F6:F19,D6:D241)
Where is the darn # sign coming from?
Range("B6").End(xlDown).Select
Classbottom = ActiveCell.Address
BottomRow = Range(Classbottom).Row
'Create totals data
Range("G6").Activate
txtFormula = "=Sumif(B6:B" & BottomRow & ",F6:F19,D6:D" & BottomRow & ")"
ActiveCell.Formula = txtFormula
I guessed at trying a different formula. I did
activecell.formula2 = txtFormula
It did the trick

Extract current cell address

I need to extract the current cell in VBA without passing it in parameter of the function.
I mean, for example, if the formula is in cell B35, I want to extract the address (line = 35, column = 2), in the VBA code of the formula.
I thought the active cell would do it, but no, it extracts the address of the cell where the cursor is.
Do you know how I could do it?
Thanks in advance for help.
I think you mean Application.Caller which is the object that calls VBA. It can be used inside a function to return the cell calling the UDF, not the ActiveCell
Application.Caller property (Excel)
An easy example would be this:
Public Function ThisCell() As String
ThisCell = "ROW:" & Application.Caller.Row & " - COLUMN:" & Application.Caller.Column
End Function
If you type now in any cell =ThisCell() it will return its row and column:
Notice the output is different in all of them, even if they use the same UDF with no arguments.
A similar one to Foxfire And Burns And Burns' answer, but no vba needed.
Apply this formula, then copy it and paste as values.
="line = "&ROW()& " column= "&COLUMN()
Try below codes
Sub ExAddress()
MsgBox "Row: " & ActiveCell.Row & vbNewLine & _
"Column: " & ActiveCell.Column
End Sub

excel vba: insert formula with dynamic cell reference using vba?

I am inserting the following hyperlink as a formula using vba:
ActiveSheet.Range("AD" & ActiveCell.Row).Formula = "=HYPERLINK(""S:\Tasks\" & Range("C" & ActiveCell.Row).Value & "\log.txt"",""View Log"")"
This works fine, however if my value in cell C was to change then my hyperlink becomes invalid and won't work because it has retained the value of the cell at the time in which the formula was entered.
I need a way of making a dynamic reference to my cell C in the event that the value should change then my link will also change.
Can someone please show me how to do this? Thanks in advance
Your code is taking the value from column C and building a string using that value that looks like this:
"S:\Tasks\FolderName\log.txt"
Instead, what you want to do is build the following string:
"S:\Tasks\" & C2 & "\log.txt"
To do that, use this VBA code:
ActiveSheet.Range("AD" & ActiveCell.Row).Formula = "=HYPERLINK(""S:\Tasks\"" & C" & ActiveCell.Row() & " & ""\log.txt"",""View Log"")"

formulaR1C1 insert concatenate string in excel vba

I'm trying to insert a concatenated string into a cell using VBA. Here's the formula I want to insert:
="ObjectID(" & E152 & ")"
here's what I'm trying, but I can't get it to work:
ActiveCell.FormulaR1C1 = "=""ObjectID("" & RC[-1] & ")"
I've tried "")" and ")"" and a bunch of other combinations, but I cant get it to work.
How do I do this?
here's how I did it:
ActiveCell.FormulaR1C1 = "=""ObjectID("" & RC[-1] & "")"""
Try this:
Sub InputConcatenatedString()
FormulaStr = """ObjectID(""&E2&"")"""
Range("A2").Formula = "=" & FormulaStr '--Modify A2 as needed.
Range("A2:A162").FillDown '--Modify affected range as needed.
End Sub
You just have to change the address of the initial cell as well as the end cell in the desired range. I just assumed that your data starts at A2 and ends at A162. :)
Let us know if this helps.
try this:
Range("A2:A162").Formula = "=""ObjectID("" & E2 & "")"""
Assuming you want to put your values in A2:A162.
Change to whatever range you got.

Vba Excel Formula for following condition(Circular Referencing)

Hi, i tried to put a formula like ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""India"",RC,"""")" If the country is not India then Check1, Check2 and Check3 should be empty otherwise they should display their own value. when i tried to put that formula the excel has given me circular referencing warning. I just want that formula. Any help would be appreciated greatly.
When a formula refers back to its own cell, either directly or indirectly, it creates a circular reference.
ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""India"",RC,"""")"
You are writing the formula in activecell and the formula says if RC[-1]="India"
and if its true RC which is same as activecell. Hence you are getting the circular reference error.
But if you put this formula in Column E as below it would work.
Range("E2:E" & lastRow).FormulaR1C1 = "=IF(RC[-4]=""India"",RC[-3],"""")"
Alternatively below is simple VBA code.
Sub sample()
Dim lastRow As Long
lastRow = Range("A65000").End(xlUp).Row
For i = 2 To lastRow
If (InStr(1, Cells(i, 1), "India") <= 0) Then
Range("B" & i & ":D" & i).Clear
End If
Next
End Sub
You can't use RC as a result only as this is referencing the current formula.
Option 1: You need to have another cell to be the validated cell, e.g.the following will create a cell to the left one of the current cell:
ActiveCell.Offset(0,1).FormulaR1C1 = "=IF(RC[-2]=""India"",RC[-1],"""")"
Option 2 after comment: If your line of code is only going to clear the current cells value if the left cell is not India then use this:
If ActiveCell.Offset(0,-1).Value <> "India" Then ActiveCell.Value = ""
Option 3: If your default value in RC has to stay but a formula is needed to override it if the value of RC[-1] becomes a not equal to India you must hard code your value in the formula like so:
ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""India"",""" & ActiveCell.Value & ""","""")"

Resources