Why does Excel stop a macro when using Application.Evaluate? - vba

I'm trying to use the Application.Evaluate function to test if a conditional formatting condition is true. However, what is happening is that the macro just stops - no error message, and the cell in which the UDF is referenced returns #VALUE.
The value of the conditional formula Formula1 property in this instance is "=A1<>VLOOKUP($A1,actWOrders1!$A:$EF,COLUMN(A1),FALSE)"
I've tried replacing Application.Evaluate with ActiveWorksheet.Evaluate, in case it is the Application form is struggling with the context, but the same happens.
Any ideas what might be causing the issue?
Thanks! Screwtape.

If your macro doesn't contain any on error handing statements it would certainly provide you with an error message if at any point it was unable to execute. It could be that your macro is executing completely however is not achieving the result you expect.
Try running it step by step using F8 and observing what is happening on each line to find the culprit, as you step through you can hover your cursor over a number of items such as variables to see what their value is. You might also find reading this webpage will help you to use the tools in VBA to debug your macros.

You created a circular reference.
If you try and type the formula into a cell Excel will tell you that has a circular reference.
You can also step through a formula in the formula bar.
Highlight an expression in the formula that you want the value of
Press [F9] to calculate the expression
The expression will be replaced with its value

Related

Is there a way to use VBA for Excel to output a formula to a specific cell in an Excel worksheet?

I am fairly new at programming. I feel like this should be a simple fix but I cannot find anything that works. I am trying to use a Select Case structure to write a different formula to a cell depending on the selected case. If I type:
Sheet1.Range("g10").Value = "=IF(SUM(F10)>0,SUM((F10-15)),"")"
I get an error:
Run-time error '1004'
Application-defined or object-defined error.
I can get it to work if I include spaces like so:
Sheet1.Range("g10").Value = " =IF(SUM(F10)>0,SUM((F10-15)),"") "
but then Excel puts it into the cell like text, not as a formula (it doesn't do anything).
I have tried a few different methods but have run into the same issue. Is it possible to do this?
I apologize if this has been asked and answered, but I wasn't able to find anyone referencing this specific problem.
You don't need to sum a single cell ie SUM(F10) Also SUM((F10-15)) is the same as F10-15.
Try this: Sheet1.Range("g10").Formula = "=IF(F10>0,F10-15,"""")" Also note, you needed to double the double quotes towards the end.
Note, you can do this on a range too like this:
Sheet1.Range("g10:g20").Formula = "=IF(F10>0,F10-15,"""")" and it is smart enough to increment the references for you for the 10 cells.
Wrong property. You need .formula not .value
worksheets("sheet1").range("g10").formula = "=IF(SUM(F10)>0,SUM((F10-15)),"")"
You should be able to type out the formula and then turn on the Macro Recorder and double-click on the cell with the forums, then turn off the Macro recorder and examine your code. That should give you what you want.

Error 1004 when using =AND(ISERROR) formula in macro

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)))

VBA Goal seek is setting cells to 0 without changing the argument

I'm tearing my hair out here.
When I run the goal seek in the worksheet it's fine. When I try to run the exact same goal seek from vba (recorded or coded by hand) the cell I'm supposed to be goal seeking displays as 0 and the variable does not change. F9 does nothing. If I edit the goalseek cell, to force it to recalculate, it displays the original output again - because nothing has changed.
Has anyone else encountered this?
Range("G14").GoalSeek Goal:=0, ChangingCell:=Range("F14")
G14 is a user defined formula with 2 range arguments and a couple of single cell arguments.
F14 is a cell inside one of the range arguments, and is definitely a driver in the calculation.
Try running it as
DoEvents
ActiveSheet.Range("G14").GoalSeek Goal:=0, ChangingCell:=ActiveSheet.Range("F14")
Not sure if it's materially different from your existing code, but might go through correctly.
Also try running your goalseek on the spreadsheet manually while recording a macro and see the auto-generated macro code if something odd is happening.

#Value! error with =(A1+A2)

I'm trying to add multiple cells with the formula =(A1+A2+A3...etc)
Which works, but if all the cells are empty then I get a #Value!
PLEASE NOTE:
Yes I am aware the proper way to add cell values is with =SUM(A1:A3)
However the cells I'm adding together each have their own functions to get their numbers, and the =SUM function won't add them together.
So! Is there a way I can make =(A1+A2+A3...etc) not give me a #Value! error in the cell that's supposed to total them if ALL the cells (A1,A2,etc) are empty? (as in, the cell with the total will just be blank)
Yes I know this is overly complicated. I'm working with that I've got.
EDIT
I might have figured out my problem. My 'false' statement in the function of the cells that were being added is "" in order to make the cell not have a 0 in it when empty. When it tries to add those cells together, if they all read "" and none are a number that's when I get the #Value! error. Not sure yet what I'm going to do about that...
EDIT 2
Yup. Problem was caused by having a non-numerical value as my false statement. Didn't want a bunch of zeros everywhere, but oh well I guess.
I tried both Excel 2007 and Calc 3.4.1, and neither one of these generated the #Value! that you mention. I am thinking that perhaps your source cells' equations are producing a value that is causing this to error out.
For example, if one of the cells has a String value, then this will be the result. This can be detected with the TYPE() function. for example:
=( IF(TYPE(A1)=1;A1;0) + IF(TYPE(A1)=1;B1;0) + ...)
this will make sure that you are actually adding numbers before the addition takes place.
edit
See: http://www.techonthenet.com/excel/formulas/type.php
for details on TYPE()

Calculate a percentile using EPPlus

I'm trying to use either PERCENTILE.EXC, PERCENTILE.INC or PERCENTILE.
Looking at FormulaParserManager.GetImplementedFunctionNames() these are not implemented functions.
I wondered if I could set the formula and leave it to Excel to calculate. So far I've not got this to work and I get a #NAME? and "The formula contains unrecognized text". Merely clicking in the formula bar causes the formula to be calculated correctly.
Inspecting the internals of the Excel file I am creating (via EPPlus):
_xludf.PERCENTILE.EXC(B14:B113,0.95)
whereas in Excel I get:
_xlfn.PERCENTILE.EXC(A14:A113,0.95)
I think this is user defined function vs function. I've tried prefixing "_xlfn." to my formula string.
This is as far as I've got I think I either need to roll my own percentile calculation in code or manipulate the xml in the Excel file maybe.
Any help appreciated.
Doh! I spend all afternoon stuck, post a question here and then immediately suss the answer...
Anyway in case anyone is interested:
var row95 = percentile95RowLookup[profileKey];
var percentileRange = sheet.Cells[row, i, row+ profileCollection.Profiles.Count-1, i];
sheet.Cells[row95, i].Formula = $"_xlfn.PERCENTILE.INC({percentileRange.Address},0.95)";
With the important proviso that workbook.CalcMode is not Manual.