Error in formula with predefined variable - vba

I want to add a formula for this range inside my macro and include inside it the variable percentage that I have set in the beginning of the macro. When I run it, it says there is an error "Application or object defined error" in this line. The entire macros runs perfectly, but today I want to add this line and it wont, so obviously the error is in the syntax of the formula. I am providing it below.
ws3.Range("C9:C12").Formula = "=IFERROR(B9/(1- & percentage),"""")"

Everything inside the quotes will appear as a literal string so you will get the word "percentage" appearing in your formula. The spreadsheet doesn't know what this is as you have defined it in your macro (there is no doubt a proper technical term for this).
Amended as per #Peh's suggestion.
ws3.Range("C9:C12").Formula = "=IFERROR(B9/(" & 1-percentage & "),"""")"

Related

Adding a hyperlink with VBA--coding syntax

I am trying to add a hyperlink to the workbook data is pulled from to a summary workbook. I can get the filepath to populate the cell no problem but when I enter hyperlink.add I usually get a parameter error.
This provides me with the URL in the selected cell:
.offset(rowcount,112)=sXLFile
I tried this to add a hyperlink but I get a parameter error with the second comma after 'sXLFile' being highlighted:
hyperlinks.Add (anchor:=.Offset(rowcount, 112), address:= sXLFile,,, incidentreport & agentname)
What I think this could would do is make a hyperlink to the filepath saved in sXLFile and read as IncidentReportAgentName. When I reference the exact workbook/sheet in the code below I get a runtime error 13: Type Mismatch.
Workbooks("Incident Reports").Worksheets("Supplementary").Hyperlink.Add anchor:=.Offset(rowcount, 112), Address:=sXLFile, TextToDisplay:=IncidentReport & AgentName
Does anyone see what I am doing wrong?
You've got a couple things going on. You're trying to accomplish too much at once, so we'll need to take a step back.
First issue - you said:
When looking at the arguments needed for the Hyperlink.Add, I didn't want to add a subaddress or screentip and these are optional arguments. I thought I still needed to use the "," as a placemark for those arguments.
Parameters for functions can be entered one of two ways:
FunctionName Parameter1Name:=Parameter1Value, Parameter2Name:=Parameter2Value, Parameter3Name:=Parameter3Value
...is equivalent to:
FunctionName Parameter1Value, Parameter2Value, Parameter3Value
...however the purpose of using the Parameter1Name:= is so that you can skip parameters or include them out-of-order without a problem.
So this:
FunctionName Parameter1Value, , Parameter3Value
...is the same as:
FunctionName Parameter1Name:=Parameter1Value, Parameter3Name:=Parameter3Value
...which is also the same as:
FunctionName Parameter3Name:=Parameter3Value, Parameter1Name:=Parameter1Value
Therefore, when you're specifying parameters with :=, do not add additional commas for missing parameters.
Troubleshooting 101
If the correction above still does not solve you problem try this.
A couple early steps in troubleshooting should be:
Replace all variables and referenced values with the actual values. For example: instead of referring to an Offset to reference the location of the anchor, specify the actual range.
So instead of anchor:=.Offset(rowcount, 112), specify anchor:=Range("A1") or whatever cell the link is supposed to be in.
Instead of address:= sXLFile, use address:= "http://www.google.com (or wherever the destination address is.
Simplify the expression as much as possible to get the basic functionality working. For example, you don't need the "TextToDisplay" label right now to make your link work. Get rid of it, temporarily.
So after those changes, your entire line should be a nice, simplified and functioning:
Workbooks("Incident Reports").Worksheets("Supplementary").Hyperlink.Add Anchor:=Range("A1"), Address:= "http://www.google.com"
If that's indeed doing what it is supposed to, now you can start changing the values to variables and adding more parameters... but make a single change, and then test it. Then make the next change, and test it. One change at a time.
More Information:
MSDN : Hyperlinks.Add Method (Excel)
Chip Pearson : Debugging VBA

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

Error 1004 when inserting formula into cell using macro

I'm trying to use a macro to insert an equation into a cell. The equation works fine if I copy it in myself but I need to copy it to 6000 cells in each or four worksheets. This question seems pretty common, but the usual answer of replace ";" with "," doesn't apply. The first line catches error 1004.
Range("J1").FormulaLocal = "=IF(ISERROR(F1),"",IF(ISTEXT(F1),"",F1))"
Range("J1:J6000").FillDown
I also tried using .formulaLocal but that doesn't seem to help.
You need to use double quotes to leave one quote:
Range("J1").FormulaLocal = "=IF(ISERROR(F1),"""",IF(ISTEXT(F1),"""",F1))"

Excel - Inserting formula with VBA

Hello Stackoverflow friends,
I am struggling for 1 hour with a formula I would like to insert via VBA:
Formula = "=IFERROR(VLOOKUP(Q" & j & ";Table1[#All];2;FALSE);"""")"
ThisWorkbook.Worksheets("Sheet1").Cells(j, "AE").FormulaArray = Formula
I get the following error message:
Run-time error '1004' - Application-defined or object-definied error
Is there an issue with the brackets or double quotes?
Thanks!
Replace the semicolons with commas:
Formula = "=IFERROR(VLOOKUP(Q" & j & ",Table1[#All],2,FALSE),"""")"
OpenOffice uses semicolons to separate function parameters, Excel normally uses commas, and always uses commas when setting formulas in the above fashion.
When programming in any lanugage also in VBA - better not tied up user to specific regional settings or specific excel version.
So instead of this:
Formula = "=IFERROR(VLOOKUP(Q" & j & ";Table1[#All];2;FALSE);"""")"
ThisWorkbook.Worksheets("Sheet1").Cells(j, "AE").FormulaArray = Formula
Better use this approach, when you determine exact user environment:
s = Application.International(xlListSeparator)
Formula = "=IFERROR(VLOOKUP(Q" & j & s +"Table1[#All]" + s + "2" + s + "FALSE)" + s + """"")"
ThisWorkbook.Worksheets("Sheet1").Cells(j, "AE").FormulaArray = Formula
p.s. I didn't checked the formula for the brackets etc. but just indicating the correct usage of list separator, and how to insert formulas with VBA code within cells in correct way.
As well, as previous post says - excel probably change the formula automatically when you open it. However excel do not change VBA code automatically, so be aware and pay attention to proper code in VBA.
Depending on the regional settings, the list separator (which is also used to separate parameters in functions) is either the semicolon or the comma. This applies when typing a formula into a cell.
Excel dynamically adjusts the list separator (and function names) according to the regional settings of the current computer when a file is opened.
So, if a user with German regional setting, which have the list separator ; saves a file, then a user with US regional settings and a list separator , opens the same file, Excel will adjust the German list separators in the formulas automatically.
When writing VBA, though, you will always need to use the US-English conventions for the list separator, which is the comma.