Writing excel functions in vba - vba

I'm trying to write the match and index functions as vba code but its not working due to several errors.
In excel, I have a table R5:AD33
I used to match functions to get my row and column number to input into my index function.
=MATCH(R35,Q5:Q33, 0)
=MATCH(R36, R3:AD3,0)
Then I use the index function to give me the cell value corresponding to the row and column number.
=INDEX(R5:AD33,S35,S36)
I tried to input this as code however it gives me 2 errors I cant solve to proceed debugging.
This line of code below gives me the "assignment to constant not permitted". I'm not sure where the constant is as x is a variable.
WorksheetFunction.Match("NPS", Sheets("sheet2").ListObjects("Table1"), 0) = x
There was also a user type not defined error but it didn't highlight a particular line of code.
Thanks.

Related

Excel VBA Evaluate not working, getting a #VALUE error

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.

Retrieve Column 1 value from VBA ComboBox after using "combobox.value =" to modify selected index value

Hello I am working with VBA and I have run into a strange situation. I am hoping that someone out there has run into the same thing and can give me a little help. In my program, there is a 2 column combo box that has it's value set to a string. The value is set like this:
ComboBox1.Value = "this_string"
Initially this ComboBox has 2 legit columns. I am able to confirm this by using the Immediate Window Later in code the first column of that combo box is checked. For example:
value = ComboBox1.Column(0)
But the code breaks here. I get this error:
Could not get the Column property. Invalid property array index.
After the value assignment. I can no longer get any Column values. Why is this?
Thank you for your help,
Billy
This question should not have a vb6 tag. - Unlike VBA, Vb6 only has single column comboboxes.

IF statement error despite the check being FALSE and the [value_if_false] being 0 or 1

For my own amusement I am trying to automate a project from work to learn more about excel vba. For the primary tab of data, I have an import function that drags in the relevant columns from another sheet for columns A through J. After that, there are a bunch of columns already set up to manipulate the the data being imported.
One of the calculation columns is supposed to return a basic 0/1 flag if the related company has been a client for >20 years. However, it ended up being a very slow UDF, so I wrapped it in an If statement to check if any other flags are false first so I can skip the slow calculation if the claim is already being eliminated. It worked fine before I went back to tweaking the import macro. Here is the function:
=IF( Q2*O2*N2 = 0, 0, IF(CompanyYears(J2, End_of_Claim_Period, Analysis_Width) >= Valid_Co_Years, 1, 0))
So now after the data import the only two results the column gives are 0 and #VALUE!. However, the 'evaluate function' step through confused me more. Turns out that for every value error, the 3 flags from columns Q/O/N are 1. So first the evaluate looks up and multiplies those 3 flags, gets 1=0 -> FALSE. The next thing it does is resolve what the named range End_of_Claim_Period points to. While it successfully changes it to a cell address, the outer IF statement's value_if_true changes from 0 to #N/A at the same time. The inner if statement continues just fine to a result of 0 or 1, and finally the statement
=IF(FALSE, #N/A, 1)
or
=IF(FALSE, #N/A, 0)
gets evaluated to #VALUE!.
Also, if I try to filter that column to see only the rows with a #VALUE! response, it correctly reevaluates them to the appropriate 0/1 values. I tried having the vba code filter then unfilter the row automatically but that leaves the errors in place still.
Update: While working on something else I came across a somewhat similar question here:
Excel is not updating cells, options > formula > workbook calculation set to automatic
I added Application.CalculateFull in at the end of the macro, which technically solved my problem, albeit for a notable performance hit.

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