Excel 2016 Array formula - excel

I have the following formula in excel (created by me in the past),
=IFERROR(INDEX(Tasks!$H$2:$H$65536;SMALL(IF(A2=Tasks!$A$2:$A$65536;ROW(Tasks!$A$2:$A$65536)-ROW(Tasks!$A$2)+1);ROW($1:$1)));"")
The formula is working as I wanted, but in each and every month the a columns are changing, so for example what was Column "A" here can be Column "C" next month and so the number of rows are changing. I'm trying to modify the formula that I don't have to adjust month by month.
For this first I found a VBA script online:
Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function
So after if I type for example:
=Col_Letter(COLUMN(Table8[[#Headers];[ID]])) it will give me Letter "B" as this header can be found in "B" Column.
So using the advantage of this VBA script I was trying to create a formula what will adjust the column letter in the original formula every time.
So this formula:
=("Tasks!" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[Owned By]])) & "$" & "2" & ":" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[Owned By]])) & "$" & ROW(INDEX(Table32;1;1))+ROWS(Table32)-1) is giving me this as the result Tasks!$H$2:$H$65536.
what is part of the formula so I think it's good.
This is the whole formula I was trying to re-create in this manner:
=Index((("Tasks!" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[Owned By]])) & "$" & ROW() & ":" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[Owned By]])) & "$" & ROW(INDEX(Table32;1;1))+ROWS(Table32)-1);SMALL(IF((((Col_Letter(COLUMN(Table8[[#Headers];[ID]])) & ROW()) & "=" & (("Tasks!" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[ID]])) & "$" & "2"& ":" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[ID]])) & "$" & ROW(INDEX(Table32;1;1))+ROWS(Table32)-1))));Row((("Tasks!" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[ID]])) & "$" & "2"& ":" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[ID]])) & "$" & ROW(INDEX(Table32;1;1))+ROWS(Table32)-1)))-ROW(("Tasks!" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[ID]])) & "$" & "2"))+1);ROWS($1:$1))))

Excel Tables have the potential of resolving such questions easily. Since the Columns can be referred by their names, you do not have to worry where they are located. And likewise, since you reference the data by column name but not row number, your reference will be covering all of the rows within that Table, no matter how many rows are added or deleted.
So as a start, try converting your range to Table by selecting it and use Insert / Table. After this you should convert your formula to contain Table references rather than cell references.

Related

Looping through all the string values of cell by vba

So this is what I want to do:
For example, in the sheet named "Outputs", the A1 cell input is
= "abcd" & Input!A5 & "efgh",
where Input!A5 is the cell value of the different sheet in the excel file.
I want to make it change so that after executing all the methods after selecting the A1 cell as
= "abcd" & Input!A5 & "efgh", I want to change the A1 cell as, = "abcd" & Input!A6 & "efgh", and then = "abcd" & Input!A7 & "efgh" and so on. (So it's basically replacing the values as A1 to Ai)
I thought of using Replace function, by writing the replacing string as Ai, and replacing i with i +1 by starting with for loop.
But I don't think this is a right method.
Could anyone shed light on how to address this?
Thanks in advance.
Check this
InputWorksheetLastRow = 7 'place here your last row value
Set ws = Worksheets("Input")
For i = 5 To InputWorksheetLastRow
Worksheets("Outputs").Range("A" & i - 4).Formula = "=" & Chr(34) & "abcd" & Chr(34) & "&Input!" & ws.Cells(i, 1).Address(0, 0) & "&" & Chr(34) & "efgh" & Chr(34)
Next i

How to insert, through VBA, a formula in a cell with special characters like "-" or "$"?

I searched on internet, without any help coming out of that...
I simply would like to be able to have my VBA code to write this formula in a cell :
=IF(C4="-"; "-"; Cars!C4*C4*Data!$C$8)
As you guessed, there is a page called "Cars" and one called "Data" where I pick the informations needed.
Of course, as it is a VBA code, the C4 will be 2 variables, one for the C and one for the 4 that will evolve...
Actually, I tried this :
Worksheets("Calculation").Range(Column & PosStartCalc + 1).Formula = "=" & "IF(" & Column & PosStartCalc & " = " & "" - "" & ";" & " - " & ";" & "Cars!" & Column & PosStart & "*" & Column & PosStartCalc & "*" & "Data!" & "C" & "8" & ")"
(The variable Column contains the column letter and the variable PosStartCalc contains the row number)
This hurts my eyes and apparently VBA's ones too as it gives the error "Run-Time error '13': Type Mismatch'
Could anyone tell me how to do that?
Thanks in advance !
Try the following, assuming the column variable is a string and row a long variable. I might not have all the variables right, but you'll be able to get what I meant to do here.
Sub test()
Dim Col As String: Col = "C"
Dim Rw As Long: Rw = 4
With ThisWorkbook.Sheets("Calculation")
Debug.Print "=IF(" & Col & Rw & "=""-"",""-"",Cars!" & Col & Rw & "*" & Col & Rw & "*Data!$C$8)"
.Cells(Rw + 1, Col).Formula = "=IF(" & Col & Rw & "=""-"",""-"",Cars!" & Col & Rw & "*" & Col & Rw & "*Data!$C$8)"
End With
End Sub
So what you might forget easily is to use the , as parameter delimiter in a VBA programmed formula. When you put this on your sheet Excel will automatically replace that with the appropriate delimiter for your region.
Another thing to keep in mind; whenever you about to use a string value in such an function, don't forget to wrap it in double quotes!
Don't forget to remove the Debug.print .... line. It was merely there to show the output :)

Assigning cell value from another workbook to my code

Basically what I want to achieve is assigning the cell value from the other workbook into my code.
Customers send in a spreadsheet of goods they want to order and this spreadsheet is used for more than one purpose so it has many columns which are not needed for my purpose.
So I created the macro to look up and give me the value from certain columns and spit out a CSV file.
I then created a spreadsheet of my own with the column values all mapped out, I am trying to get the coding to lookup the cell value in my spreadsheet and knows which columns to be looking up on the customer spreadsheet.
I want it so I can just go in and change the values in the cells on
my spreadsheet instead of having to go into the coding and change the
columns value in my code.
ShipToSiteID = Application.WorksheetFunction.Trim(Range("Q" & counter))
AltShipTo1 = Application.WorksheetFunction.Trim(Range("G" & counter))
AltShipTo2 = Application.WorksheetFunction.Trim(Range("H" & counter))
AltShipToCity = Application.WorksheetFunction.Trim(Range("I" & counter))
'AltShipToState = Application.WorksheetFunction.Trim(Range("I" & counter))
AltShipToZip = Application.WorksheetFunction.Trim(Range("J" & counter))
AltShipToCountry = "UNITED KINGDOM"
RefNumber = counter - 1
UserId = LCase(Environ("username"))
ShipToSiteID = Replace(ShipToSiteID + AltShipToZip, " ", "")
Sheets(2).Select
Range("A" & counter - 1) = LineType & comma & RefNumber & comma & QName & comma & BlanketID _
& comma & BillToSiteID & comma & ShipToSiteID & comma & ContractID _
& comma & PONumber & comma & CaseID & comma & ShipVia & comma & RequiredDate _
& comma & Comments & comma & Priority & comma & TerminalID & comma _
& AltContactFirst & comma & AltContactLast & comma & AltPhone & comma _
& AltShipTo1 & comma & AltShipTo2 & comma & AltShipToCity & comma _
& AltShipToState & comma & AltShipToZip & comma & AltShipToCountry _
& comma & UserId & comma
A few suggestions...
To reference a value in workbook2 on workbook1, the easiest way to do this would be to copy the worksheet from workbook2 to workbook1. Then you can reference the value by typing "=" in the cell, and clicking on the cell that you want to reference.
Referencing in VBA code gives you some more flexibility. I'd suggest checking this good SO question and this one for more info. This may be more along the lines of what you want.
Unfortunately, if you reference a value from workbook2 on workbook1, there is no easy way to change the value of workbook2 from workbook1.

Applescript - Escaping Quotes When Setting Excel Formula

I've searched extensively for the answer to my question and I'm at an impasse.
I'm trying to pass a formula to excel with applescript. The problem is the quotes around the spaces that I want to place between fields. I've used "\" to escape the quotes but it throws an error. To make it more complicated the row number is a variable "i". Here is the formula in excel format:
=CONCATENATE(A2," ",B2," ",C2," ",D2)
Here is the formula in applescript-ese (works but does not produce spaces in data):
set rowCount to (((count of rows of used range of active sheet)) + 1)
repeat with i from rowCount to 2 by -1
set formula of row i of column 15 to "=CONCATENATE(A" & i & "," & ",B" & i & "," & ",C" & i & "," & ",D" & i & ")"
Here is the formula with escaped quotes to add the spaces (gives error):
set formula of row i of column 15 to"=CONCATENATE(A" & i & ","\" \" & ",B" & i & ","\" \" & ",C" & i & ","\" \" & ",D" & i & ")"
I get a sytax error on the first slash; Expected end of line etc. but found unknown token. I have a feeling I'm either missing a few sets of double quotes or I'm making this way more complicated than it needs to be. Any ideas? Should I be going about this differently?
Thanks in advance!
EDIT
Taking another look at it I think you have too many quotes when you use the \ character to escape the double quote. Instead of ","\" \" & ",B" try ",\" \",B" so your line would look like this:
set formula of row i of column 15 to "=CONCATENATE(A" & i & ",\" \",B" & i & ",\" \",C" & i & ",\" \",D" & i & ")"
In excel VBA you can double quote a double quote to get it to show up in a string like this:
"=CONCATENATE(A" & i & "," & """ """ & ",B" & i & "," & """ """ & ",C" & i & "," & """ """ & ",D" & i & ")"
Or you can try using ascii character for space which is Chr(32)

Creating a VBA formula using CountIF

I am creating a formula to count based upon 2 conditions. My logic is wrong - again.
When I (manually) enter enter code the summation formula (COUNTIFS) into correct cell itself, it sums correctly:
COUNTIFS(E4:E1362,"Requirement",S4:S1362, "<>4")
When I execute the following code line, I do not get any errors, but instead, all the sums are zero.
Range("G" & Start(groups) - 1).Formula = "=COUNTIFS(E" & Start(groups) & ":E" & Finish(groups) & "," & "Requirement" & ",S" & Start(groups) & ":S" & Finish(groups) & "," & Chr(34) & "<>4" & Chr(34) & ")"
I realized that the COUNTIFS was comparing the value in column S to the string "<>4". And none of the cells contain that string. This is why all my values were zero. I do not want the comparison to be against that string. I want the comparison to be column S value NOT equal to 4.
So, I changed the line to (and variations to get it to work):
Range("G" & Start(groups) - 1).Formula = "=COUNTIFS(E" & Start(groups) & ":E" & Finish(groups) & "," & "Requirement" & ",S" & Start(groups) & ":S" & Finish(groups) & "," & Chr(34) & "<>" & Chr(34) & "4)"
So, I am at a loss. (1) I manually enter the formula and it works. (2) I build it in VBA, and it does not sum correctly. (3) I look up how to do build the formula correctly, and I get errors time and time again. It seems like the only way to get this to work is to keep the quotes within quotes, but I don't want to compare against the string.
Does this make sense? I'm not liking my worksheet anymore. It is no longer any fun at all. :(
Thank you so much.
I think all the Chr(34) are getting in the way of you viewing your formula correctly. try using "" to make the quote for the formula.
.Formula = "=COUNTIFS(E4:E1362,""Requirement"",S4:S1362,""<>4"")"
with your Start and Finish functions, that would change the formula to:
.Formula = "=COUNTIFS(E" & Start(groups) & ":E" & Finish(groups) & _
",""Requirement"",S" & Start(groups) & ":S" & Finish(groups) & ",""<>4"")"
Well, One of the problem I found there was quite simple, instead using "," you must use ";".
It depends on the version you used, some using "," and some others using ";". you can just try it, I hope it solved your problem.
and second one "Requirement" there, I think you should use double quote instead.
and for your case To meet This condition:
COUNTIFS(E4:E1362,"Requirement",S4:S1362, "<>4")
Use This:
"=COUNTIF(E" & Start(groups) & ":" & "E" & Finish(groups) & "," & """Requirement""" & "," & "S" & Start(groups) & ":" & "S" & Finish(groups) & "," & """<>4"")"
I think it Should Work (if I don't miss any Quotes thought).
... Try removing the chr(34) and the extra quotations - It should look exactly like the "Requirement" criteria:
Range("G" & Start(groups) - 1).Formula = "=COUNTIFS(E" & Start(groups) & ":E" & Finish(groups) & ",""Requirement"",S" & Start(groups) & ":S" & Finish(groups) & ",""<>4"")"
After running a quick test with your initial code
Range("G1").Formula = "=COUNTIFS(E1" & ":E20" & "," & "Requirement" & ",S1" & ":S20" & "," & Chr(34) & "<>4" & Chr(34) & ")"
Modified slightly to account for your groups variable cell G1 ends up with the following formula:
=COUNTIFS(E1:E20,requirement,S1:S20,"<>4")
which when tested works absolutely fine for your second if statement S1:S20,"<>4".
I would guess that the problem lies with your first statement. What is in your requirement range and what data are you trying to match with.
I set up the requirement named range to be cell D5, I entered a 5 in there and the formula would increment each time I added an extra 5 to the range E1:E20.
I then started typing 4's into the range S1:s20 and this decreased my count.
edit
The last thing that I can suggest is to add in a helper column in column F that evaluates row to see if it matches the requirement condition, then it would be a simple matter of making your formula:
=COUNTIFS(F1:F20,TRUE,S1:S20,"<>4")

Resources