Vba Function that counts how many times a text is contained inside a string? - vba

I have a very specific question
There is a native vba function that counts how many time a text (such as a word or even a single character) repeats inside other text (string)?
It would get as parameters two string values, for example, and return a integer/long value.
I looked among worksheet functions and I couldn't find any. Should I make my own one?
Thanks very much!

As it doesn't exist in vba, you should make your own. You can write a loop using InStr, counting the number of iterations. It should be pretty performant as it essentially won't have to do any memory allocations, and VBA's string functions are fast.

Related

VBA formula for cell in excel, inserting special character ambiguously [duplicate]

I am adding a formula to a worksheet via VBA which should be:
=UNIQUE(IF(TableA[ColumnA]=A1,TableA[ColumnB],""))
This utilises the new SPILL feature in Excel to give me a list of column B values where the related value in column A matches what is in cell A. I'm also applying the UNIQUE function to remove any multiple blank ("") results.
This works perfectly if I manually type the formula into Excel, however in using VBA to add the formula, Excel is adding # symbols within the formula, and causing it to show #VALUE!.
The VBA line being used to add the formula is:
=Cells(x,y).Formula = "=UNIQUE(IF(TableA[ColumnA]=A1,TableA[ColumnB],""""))"
The resulting output in Excel is:
=#UNIQUE(IF(TableA[#[ColumnA]]=A1,TableA[ColumnB],""))
What is going on, and what have I missed?
Thanks in advance!
Good question, and I looked it up...
In short:
Use =Cells(x,y).Formula2 instead of =Cells(x,y).Formula
Explaination:
The # that shows is called the implicit intersection operator. From MS docs:
Implicit intersection logic reduces many values to a single value. Excel did this to force a formula to return a single value, since a
cell could only contain a single value. If your formula was returning
a single value, then implicit intersection did nothing (even though it
was technically being done in the background).
But why does it appear in your newer Excel O365? Well, Range.Formula uses IIE (implicit intersection) thus adding the # to basically undo your dynamic array functionality. UNIQUE is a new dynamic array function. So, to write this out in code, you should use the Range.Formula2 property (or Range.Formula2R1C1 if you use R1C1 notation). These properties use AE (array evaluation) and is now the default.
Here is an informative doc from MS on the subject which explains the difference between Formula and Formula2 in more detail.
If you want to know more about the implicit intersection operator then have a look at this
I answered another question earlier on that involved implicit intersection with an example on how that actually works here if one finds it interesting.

Custom functions in SQL

how would I implement custom functions in SQL?
Let's say I have a list of values and I want to transform those in a way where SQL doesn't supply a default function. E.g. look at every field's string, count the number of chars and add the sum of it to the end of each string.
In a programming language this is easy, e.g. save the field's string in a variable A, loop through the string char by char and increasing a counter by one each time, adding the counter to variable A.
But how would I do this in SQL? Can I implement EVERY function solely with the means of SQL, or do I need to use a common programming language like Java, or something like PL/SQL for that?
Thanks so much.

Excel DAO Query Using a Range of Cells as Parameters

I am using VBA to query a database. I am using a FOR EACH loop to iterate through the range that contains the values for the parameters. This works okay for most of what i am trying to accomplish, but it seems inefficient because of the number of database calls that must be made. Is there a way to make the query accept the range of cells as the query parameters in the query WHERE clause or perhaps a more efficient way to do this?
I'm probably going to get flamed for this, but so be it.....
I wrote an excel function that concatenates a range of cells into a delimited string for use in SQL IN statements. You can find the whole code on my blog. Use my sqlConcat() function to genereate a string to concatenate into your sql statement as part of an IN clause.
Concatenation is not best practice, and it really is open to injection. So, I don't know how much I support running my function through code this way, but it will help you do what you're asking.

SQL Array within a cell

I have a cell that contains an array of characters seperated by commas i.e. "1,2,3,4,5" My question is, is it possible to remove a particular element of the array such as if I wanted to remove "1" then the cell would then become "2,3,4,5" or remove "3" and it becomes "1,2,4,5" I want to perform this task within SQL either as a function or a stored procedure, any help is much appreciated.
Sure, it'd just be some basic string REPLACE() calls: http://msdn.microsoft.com/en-us/library/ms186862.aspx
However, since you have to manipulate individual bits of this data field separately from the rest of the field, it's a good candidate for getting normalized into its own child table.

Define initialized arrays in functions and return values based on user input Visual Basic

In this application I need to allow users to enter a month as integer (1-12) then use integer tryparse to validate that input, that seems to be the easy part. I need two create two functions, one that returns the name of the month and the other returns the number of days in that month. The arrays are supposed to be defined and initialized within the function so that the main program can take the user input and call the two functions, then return the appropriate values as output to labels. I am not sure how to declare the arrays in their appropriate functions and then how to call those functions to retrieve the right value from the function.
Since this is homework I'm not gonna write the code for you, but this should be pretty simple. Assuming the number is in the textbox and the user presses the OK button (or whatever the functionality is), the code for that OK button should include calls to the two functions you create, let's say GetMonth and GetDays.
GetMonth would take an integer, and frankly, I don't really see the need for declaring any arrays here. If the array declaration is part of your assignment then you can do that, but it just doesn't seem necessary. A simple Select...Case statement seems more straightforward: you just set up the cases for the integer being passed into the function, 1-12, and return the month's string. Similarly for GetDays, just set up cases 1-12 and return the number of days.
If you're unfamiliar with any of this, take a look at these MSDN articles; they should point you in the right direction:
Functions
Cases
Arrays
Hope this helps!
Edit: Realized I never expanded on how you could do this with arrays anyway (meant to, sorry). You would just create two string arrays of size 12 (or one string array, one integer array), and then define each of the 12 elements in each array as whatever month or number of days you need. Then in the function, just return something like arrayDays[x], where x is the input being passed in. (If you want to be fancy, you could create a 12x2 string array and store all the information in one place.) But I'm pretty sure it'll take slightly less code to do a Select...Case statement (it just seems more direct to me).