I have written a Formula in Excel that Sums up Items in 1 Column (Text) the Column is a Dynamic Named Range (Invoice_list_Item) the Formula sets a Value (Of my Choice) based on each Item in the Range and Sums those Values, I am now using a Userform where I would like to Display that Equation rather than in the workbook itself, so my problem is that everytime I write this code in the userform it doesn't work; either throwing an error, or not displaying a value. I have tried Application.Sum, Application.Sumif, Application.Sumproduct and the main one I've been trying to make work Application.Sum(If(Range(
Any Help in correcting this Formula in to a Macro will be much appreciated.
=SUM(IF(Invoice_list_Item="1/4 Item",0.25,0)+IF(Invoice_list_Item="1/2 Item",0.5,0)+IF(Invoice_list_Item="1 Item",1,0))
You can use Application.Evaluate (this works for array formulae) to evaluate a string as if it was an Excel formula:
Dim vResult As Variant
vResult = Application.Evaluate("=SUM(IF(Invoice_List_Item=""1/4 Item"",0.25,0)+IF(Invoice_List_Item=""1/2 Item"",0.5,0)+IF(Invoice_List_Item=""1 Item"",1,0))")
Related
I need to count unique text values in a range. I'm trying to recreate the following formula in VBA =SUMPRODUCT(--(FREQUENCY(MATCH(B2:B197,B2:B197,0),ROW(B2:B197)-ROW(B2)+1)>0)). I tried rewriting the formula above using VBA code
Dim NumLabels As Variant
NumLabels = SumProduct(--Frequency(Application.WorksheetFunction.Match(Range(Cells(2,2),Cells(Range("A1").CurrentRegion.Rows.Count,2)),Range(Cells(2,2),Cells(Range("A1").CurrentRegion.Rows.Count,2)),0),
This is as far as I was able to get. I don't know how to use the function =ROW() in VBA.
Or is there a more efficient way to do this.
I am intrigued by the VBA statement Worksheets.Calculate. It appears to me that it does not work correctly with custom function using Evaluate Statement when the function refers to cells on another sheet within the same workbook.
On Sheet1 I have Column A with just plain numbers.
Sheet2 in same workbook Column C has basic expressions on Column A Cells like A1+A2 etc.
I have this custom function in VBA Module
Function MyEval(Val1 As String)
Application.Volatile
MyEval = Evaluate(Val1)
End Function
And I put this formula =MyEval(Sheet2!C1) in say B1 in Sheet1 and drag it down.
This works fine for the first time. It takes in Expression in Sheet2:C1 passes it to MyVal, inside evaluates and returns the result.
The issue is the moment I edit the string expression in Sheet2, this entire formula returns 0. I need to Press F9 on Sheet1 to recalculate.
I have put Application.Volatile in MyVal and Worksheets("Sheet1").Calculate in Worksheet Change event in Sheet2.
Still it does not work. The only way to make it work is Press F9 on Sheet1.
Is this expected behavior? Am I missing something. How to I make a custom formula in VBA using Evaluate statement recalculate when there’s worksheet change event and the formula refers to range on another sheet?
No issue if the reference is in the same sheet. It works fine though. I have Excel 2013. Thanks.
Screenshot
The problem is that your code is calling Application.Evaluate which works in the context of the active sheet. If you want it to evaluate in the context of the sheet the function is on, you should use the worksheet.evaluate method like this:
Function MyEval(Val1 As String)
Application.Volatile
MyEval = Application.Caller.Worksheet.Evaluate(Val1)
End Function
I'm recording a macro to automate some Excel reports and have encountered the following bug whenever I try and run an iserror(search) formula:
Run-time error '1004': Application-defined or object-defined error
I have two lists. The formula iterates through the first list and compares the values with those of the second list, hiding any matching values.
The formula in Excel is like this only with a wider criteria range:
=AND(ISERROR(SEARCH($B$3212,B2)),ISERROR(SEARCH($B$3213,B2)))
It works perfectly when I insert the formula directly into the spreadsheet cell however I get an error when I record and later run the macro using the same formula.
EDIT 2
I got the formula insertion to work through the macro but now I cannot filter the data as before, even when I do it manually without the macro.
Below is a link to a picture giving an example of the type of lookup I'm trying to achieve, previously it worked perfectly and removed all the rows which contained a string from the 'to remove list' now I cannot get it to filter at all. I've tried removing the macro after saving in notepad in case the file had become corrupted but it still does not filter as before. What could be causing this?
This is how the lookup works
Cell [A13] would contain the aforementioned ISERROR formula in this example.
This formula doesn't translate well to VBA in its current form. You should use the VBA Instr function instead of the worksheet function Search.
Function FindSubstring() As Boolean
Dim rngFindText As Range
Dim rngWithinText As Range
Set rngFindText = Sheet1.Range("B3212")
Set rngWithinText = Sheet1.Range("B2")
FindSubstring = InStr(rngWithinText, rngFindText)
End Function
Sub foobar()
Debug.Print FindSubstring
End Sub
You are asking Excel a question to tell you to find the contents of $B$3212 in B2 and to find if again.
Usually the SEARCH is used to find the contents of one thing in another, by using it again the AND statement you are asking it again ... and for what?
Hence the question does not make sense.
What I think you might be asking if just once and if there is an error meaning it did not find it there in this instance for it to return 0.
=IF(ISERROR(SEARCH($B$3212,B2)),0,SEARCH($B$3212,B2))
I figured this one out, the original 1004 error was caused by vba only partially recording the formula, the solution involved simply going into the debugger to find which line hadn't been translated correctly and editing that line. I then had to edit the formula so as to be able to filter out values acording to my criteria and ended up with a formula closer to this:
=AND(ISERROR(SEARCH("Value1",B2)), ISERROR(SEARCH("Value2",B2)))
I'm trying to apply a formula through VBA for a particular range. This is the code in my VBA editor:
Sheets("WBR45").Range("AE105").Formula = "=PERCENTILE.INC(TP!$A$3:$A$30:$B$3:$B$30:$C$3:$C$30:$E$3:$E$30,50%)*24"
And the below formula gets updated in the destination cell when this is run:
=PERCENTILE.INC(TP!$A$3:$A$30:$B$3:$B$30:$C$3:$C$30:$E$3:$E$30,50%)*24
But I get an error in the destination cell as #VALUE!.
And when I click on "Show Calculation steps", only this part of the formula is underlined :
TP!$A$3:$A$30:$B$3:$B$30
I have no idea what is wrong with this simple formula. Can someone please take a look
Honestly I have no clue about what you're doing with this, but this may fix it:
"=PERCENTILE.INC(TP!$A$3:$A$30:TP!$B$3:$B$30:TP!$C$3:$C$30:TP!$E$3:$E$30,50%)*24"
You appear to have three errors in your formula:
You are using : to separate ranges instead of ,
You are not specifying which sheet the second, third and fourth ranges refer to, therefore it is defaulting to the sheet on which the formula occurs (i.e. sheet "WBR45")
Multiple ranges will need to be enclosed within brackets (...) in order to be passed as a single range.
If you are trying to have your function operate on the range A3:C30 together with the range E3:E30 (i.e. A3:E30 but ignoring column D), with those ranges being on the "TP" worksheet, I believe that you need to change your formula to
Sheets("WBR45").Range("AE105").Formula = "=PERCENTILE.INC((TP!$A$3:$A$30,TP!$B$3:$B$30,TP!$C$3:$C$30,TP!$E$3:$E$30),50%)*24"
or, slightly simplified
Sheets("WBR45").Range("AE105").Formula = "=PERCENTILE.INC((TP!$A$3:$C$30,TP!$E$3:$E$30),50%)*24"
I have this formula that looks at various criteria across multiple columns and checks to see that if all the all the criteria match, it will paste data from one column to another. I've tried a couple ways to get it into VBA, but I can't seem to get anything to work. Thanks!
=INDEX($D$2:$D$1112,MATCH(1,($A$2:$A$1112=$U$7)*($C$2:$C$1112=$W$7)*($B$2:$B$1112=F3),0))
You are not going to be able to use that array formula to directly return a value to a cell. VBA does not process an array formula the way that the worksheet can. The best method is to use the worksheet's processing or one of the Application Evaluate methods.
Your lack of a worksheet to reference troubles me. When a formula is in a worksheet cell, it knows what worksheet it is on. When using formulas within VBA, the parent worksheet is a 'best guess' without explicit worksheet referencing.
Here are three methods to put the results from that array formula into Z2:Z4 on the active worksheet. Remember that these cell references should be modified to include the worksheet name.
With ActiveSheet
'this simply puts the formula into the worksheet then reverts the cell from the formula to the returned formula value
.Range("Z2").FormulaArray = "=INDEX($D$2:$D$1112, MATCH(1, ($A$2:$A$1112=$U$7)*($C$2:$C$1112=$W$7)*($B$2:$B$1112=F3), 0))"
.Range("Z2") = .Range("Z2").Value
'this uses the 'square bracket' method of evaluating a formula on-the-fly
'the formula being evaluated can be array or non-array
'this method is does not like building a formula string from pieces of text
.Range("Z3") = [INDEX($D$2:$D$1112, MATCH(1, ($A$2:$A$1112=$U$7)*($C$2:$C$1112=$W$7)*($B$2:$B$1112=F3), 0))]
'similar to the method directly above, Application.Evaluate does just that.
'the formula being evaluated can be array or non-array
'this method is easier to build a formula string from pieces of text
.Range("Z4") = Application.Evaluate("INDEX($D$2:$D$1112, MATCH(1, ($A$2:$A$1112=$U$7)*($C$2:$C$1112=$W$7)*($B$2:$B$1112=F3), 0))")
End With
You need 2 changes:
(1) To use a function in VBA when it is available in native Excel, you need to preface each function with Application.WorksheetFunction. ie:
x = Application.WorksheetFunction.Sum(y,z)
(2) To reference a cell within a sheet, in VBA, you need to access it specifically, in one of a few ways. The simplest for our purposes is the RANGE property, as follows:
x = Application.WorksheetFunction.Sum(Range("A1:A2"))
So to put those two changes together, your formula would look like this:
=Application.WorksheetFunction.INDEX(Range("$D$2:$D$1112",Application.WorksheetFunction.MATCH(1,(RANGE("$A$2:$A$1112"=RANGE("$U$7")*(Range("$C$2:$C$1112"=Range("$W$7")*(Range("$B$2:$B$1112"=Range("F3"),0))
Although I see now having gone through this that you seem to be using an Array Formula - not sure if any special jigging is required to get that to work.