VBA Dynamically Building A Formula From An Array - excel

I am trying to dynamically construct a formula based on an array that I have generated from a cell (separated by commas), as there is a varying amount of elements in the array I need to append a new "formula block" with the updated element to use in a if statement that is generated after the for each loop. VBA is throwing a type mismatch error in the InvestigateFormula = line, here is my code:
For Each Type In ToIgnore()
InvestigateFormula = "(ISNUMBER(SEARCH(*" & ToIgnore(Type) & "*," & _
AssetTypesCol & "2)),"
FullFormula = InvestigateFormula & FullFormula
Next Asset
FinalInvestigateFormula = "=IF(OR" & FullFormula & "),""Ignore"", """")"
ActiveCell.Formula = FinalInvestigateFormula
Please let me know if there is an easier way of doing this or how I might be able to correct the above code. Btw I am not declaring a variant I am simply declaring ToIgnore() as String and using the split function from the variable which contains the comma separated values to generate the array/items to loop over.

"Type" is a reserved name? Try strType instead?

Related

How do I change a Parameter in Power Query without it converting the value to a formula?

I was following the instructions on this thread:
How to Change Excel Power Query Paramaters with VBA
which lists the following code for changing a Power Query parameter:
ThisWorkbook.Queries([ParameterName]).Formula = 'New code here
However it converts the value into a formula and adds "= " to the front of it:
I need to update the source of my query because the GUID expires and needs to be refreshed.
Source = Xml.Tables(Web.Contents("http://api.aceproject.com/?fct=getprojects&guid=" & GUID & "&Filtercompletedproject=False&projecttemplate=0&assignedonly=True")),
The only solutions I can find require using a value stored in a cell, but I want to avoid storing the GUID in a cell for security reasons.
Using VBA how can I change either just the parameter value (without it converting into a formula) or the entire source URL?
The solution was using double quotes to force a text value to the formula:
ThisWorkbook.Queries("GUID").Formula = """dogs"""
Here's the final version, which passes the variable through as text:
Sub RefreshQuery_Click()
ThisWorkbook.Queries("GUID").Formula = """" & GUID & """"
End Sub
To ensure the query retains the parameter property, add in the following meta data:
ThisWorkbook.Queries("GUID").Formula = """" & GUID & """" & " meta [IsParameterQuery=true, Type=""Text"", IsParameterQueryRequired=true]"

Reading consequent text files by using arrays in VBA

I am trying to write a VBA code to read values and write it to where I want from 4 thousand different text files.
As an example the fine name is like NACA63220_1.30_17_CD.txt and NACA63220_1.05_12_CL.txt
In this name, the value 1.30 changes, 17 changes and CD becomes CL etc.
I want to create loops so that I read and paste the value I want from these files one by one.
Mach = Array ("0.2_", "0.6_", "0.9_", "1.05_", "1.30_")
Alpha = Array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
Letter = Array("_CD", "_CL", "_CM")
strFile = D:\Database\NACA63220_ + Mach(5) + Alpha(18) + Letter(1) .txt
I want to have something like this with loops so that in this instance this strFile becomes D:\Database\NACA63220_ 1.30_17_CD.txt and then I can continue with my code.
You need to concatenate strings with & not + (which is for calculation only). Also your strings need to be enclosed in quotes "".
strFile = "D:\Database\NACA63220_" & Mach(5) & Alpha(18) & Letter(1) & ".txt"
Note that depending on how your arrays were defined the counting starts with zero 0 not with 1. So the last item is Mach(4) not Mach(5). In this case …
strFile = "D:\Database\NACA63220_" & Mach(4) & Alpha(17) & Letter(0) & ".txt"
should give the desired result D:\Database\NACA63220_ 1.30_17_CD.txt

Runtime Error 1004 when replacing formula

I am using the following code to add something to a Formula in VBA (for debugging purposes I am using the variable currentFormula, instead of doing it directly):
currentFormula = Range("A" & row).Formula
currentFormula = currentFormula & "+" & CStr(difference)
Range("A" & row).Formula = currentFormula
When going through the code step by step, the variable currentFormula has the correct value before the final step, e.g. "=A1/A2+0.5". However, then the script Fails with runtime error 1004. When I am Setting currentFormula manually to something like "=10+10", the script works.
CStr formats the number according to the current system locale.
Formula accepts formulas in English.
The function that converts numbers to strings in an invariant way is Str. Note that it prepends a space to positive numbers which you might want to remove:
currentFormula = currentFormula & "+" & LTrim$(Str$(difference))

EXCEL: Is there a function / way that works like LOOKUP, but takes an array as input and returns an array as results

Is there a way in google spreadsheet to get array multiple values in return to an array of input values?
I am attaching an image that has an example of what is desired.
Basically, want something as:
=LOOKUP(input_array,range_with_ID_values,range_with_return_values)
which returns a 'result_array' that has an array of values matching with values from 'input_array'
Please help!
Kaustubh
EDIT:
As a comment suggested, I am elaborating why I need this. I want to check if all dependent tasks are completed. if yes, then this 'dependent task' should open up to be taken up. Attaching an image to describe the scenario below
click to see example
In this image, until tasks 1,2,3 are not 'Completed', task 4 will always be 'Awaiting Dependency'. Once all tasks 1,2,3 are 'Completed', it will change to Pending
Also note that the tasks need not be serialized. There might be tasks in between, which are irrelavent to task we need to update status for. Eg. there may be tasks like 'buy grocery', 'kill cockroaches' etc which the 'water the plant' taks doesn't depend on
excelexcel-formula
Use TEXTJOIN as an array formula
=TEXTJOIN(",",TRUE,IF(ISNUMBER(SEARCH("," & A2:A9 & ",","," & D3 & ",")),B2:B9,""))
Being an array formula it must be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode. If done correct then Excel will put {} around the formula.
In Excel, consider the following User Defined Function:
Public Function MultiReturn(rng As Range, inputs As String) As String
Dim arry1, a, outstr As String, i As Long, arry2
arry1 = Split(Mid(inputs, 2, Len(inputs) - 2), ",")
outstr = "{"
arry2 = rng
For Each a In arry1
For i = LBound(arry2, 1) To UBound(arry2, 1)
If CStr(a) = CStr(arry2(i, 1)) Then outstr = outstr & arry2(i, 2) & ","
Next i
Next a
MultiReturn = Mid(outstr, 1, Len(outstr) - 1) & "}"
End Function
For example:
As you see, the first argument is the table being search, the second argument is the set of inputs in your desired format (curly brackets encapsulating a comma-separated list).

Variable that contains different value for each loop

I would like to download prices from the internet. The concept works, when I define symb as a constant value (e.g. K15). But now, I want to download data from different links, where the part symb changes according to the value of the cells G13 to G22 in my spreadsheet. (In other words, I want to go through each row from G13 to G22 - each containing a different value for symb - and download the data from the respective link).
I tried that with a simple loop, defining the variable symb in each one of the loops:
For i = 1 To 10
Symb = Worksheets("Futures").Range("G12").Offset(i, 0).Value
Set qt = querysheet.QueryTables.Add( _
Connection:="URL;" & "http://download.finance.yahoo.com/d/quotes.csv?s=" & Symb & ".cbt&f=sl1d1t1c1ohgv&e=.csv", _
Destination:=querysheet.Cells(5 + i, 1))
Next i
Obviously, it doesn't work like this. I assume that it is not possible to define a variable within the loop, is it? Can somebody give me a hint how I can make that work?
There's something worng with your Connection String. When I get rid of the .cbt in the URL string, it works. Or, you may have forgotten to include some letters, so debug it in your browser and get the Connection string correct and it should work.
You may also want to modify the destination, like so, and refresh the table:
Set qt = querySheet.QueryTables.Add( _
Connection:="URL;" & "http://download.finance.yahoo.com/d/quotes.csv?s=" & Symb & "&f=sl1d1t1c1ohgv&e=.csv", _
Destination:=querySheet.Cells(5, 1))
qt.Refresh
you could use an array like this
symb(1 to 10) as String
for i=1 to 10
symb(i)=cells(i,1)
next i

Resources