Excel VBA Evaluate not working, getting a #VALUE error - vba

I have a VBA formula that works when I use the ActiveCell.Formula2R1C1 function, which I used to test that I could get the result I want. This works as it should, giving me a comma delimited list.
ActiveCell.Formula2R1C1 = "=SUBSTITUTE(ARRAYTOTEXT(FILTER(VendSelect1,VendSelect3=LEFT(R[-7]C[-3],6))),LEFT(R[-7]C[-3],6) & "","","""")"
VendSelect1 and VendSelect3 are named ranges from a table in another tab
I am wanting to use the result of this formula as a string to use in a Data Validation list. The reason to go this route is the list will be dynamic based on a value that is entered in another cell, so the list will vary. I have the code for creating the Data Validation working, I just need to get the result of the above equation to be stored as a variable for that code.
I have tried using the Evaluate function to store the result but when I try to put that value in a cell to test that it is working, I get a #VALUE error in the cell. Is this just more complicated a formula than Evaluate can handle? Or am I doing something wrong? I am open to other suggestions to accomplish the same thing but I'm not a VBA expert and this has been the best thing I have found, so far, to accomplish this. Here is the code I have that is giving me the #VALUE error in cell D14.
Sub Test()
Dim FormTest As Variant
FormTest = Evaluate("=FILTER(VendSelect1,VendSelect3=LEFT(R[-7]C[-3],6))")
Range("D14") = FormTest
End Sub
I've been trying to get this to work for about 2 days and this seems to be the last obstacle to getting what I am wanting to do.
Thank you, in advance.

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

Today function equivalent in VBA in combination with countifs

I am having some problem with using a countifs formula in Excel / VBA. I have got the formula working perfect in Excel but ideally I want to use this in VBA with my form. Here is the formula in Excel which works a treat:
=COUNTIFS(Sheet1!A:A,"Place",Sheet1!K:K,"<"&TODAY())
will count the names places that are now in the past
=COUNTIFS(Sheet1!A:A,"place",Sheet1!K:K,">"&TODAY())
will count the names places that are current
I have five different Places in column A and hundreds of different dates in column K. The above formulas work well in Excel and return the correct values. I have spent hours trying to get this to work in VBA with my userform but keep getting various errors. The first part is not the problem but as soon as I get to the &today function it falls apart. From what I can see the &today function is not available in VBA and the &Date seems to be the recommendation. I have tried this but still get no where. I'm missing a trick (or several) here and I would really like to get this working in VBA rather than using the current formulas in Excel. The returned results are then displayed in textboxes on my form.
All ideas and feedback much welcome!
Second edit
================================
Thanks for the quick replies! Here is the actual code I am playing about with in VBA
'Count events by area'
Dim ListLondon As Long
ListLondon = .CountIf(Range("a1:a1998"), "London"), ("Sheet1!K1:K1998"), "<" & Date)
End With
Me.TextBox1 = ListLondon
I know the second part of the count if is all wrong regards the date - that's how I've left it for now. I am really hoping to use the current layout and a working Date / Today code at the end. Please show me what I've done wrong here!
====
oops - can see a mistake already - but the initial problem remains around the date issue. I should of used countifs as using multiple criteria.
You have to read the values of the cells to your VBA code. I recommend you to use Excel.Range object to do that. It can interpret the range like the edit line of the Excel, something like
Dim foo as Excel.Range
set foo = yourworksheet.Range("A1:B3")
Read the Date type data into VBA Date type variable by iterating through the cells.
Examine relation between the read data and the current date. Current date can be read by using the DateTime.Now function.
Increment a variable based on a decision

vba write numeric data as text into excel

I am writing data into an excel spreadsheet. The data comes from an Access Database. Code like the line below works:
xl.cells(5,5)="joe"
but code like the line below does not:
xl.cells(5,5)="1/2CD438"
I get an error like "Application defined or object defined error"
I think Excel is trying to do a calculation, and the calculation fails. All I really want is the text string.
I've tried formating the column like
columns(1).NumberFormat="#"
, but it doesn't work either.
Any ideas?
Thanks for all the input. I had tried several of your ideas without success. As it turns out I was using variables to determine the exact row, column as:
xl.cells(NextRow, Col) = ars!PartNo
By simply replacing NextRow with an integer, the code worked. Replaced the integer in the test with NextRow, and the code ran. I suspect a misspelling or something, but couldn't see it looking at the code.
Thanks for the suggestions.

Problem using SUM(If( Rather than SumIf(

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