concatenate using vba array - excel

I have a situation where I would like to merge two columns in to one.
Example : column 2 and 3 data should get merged in column 1 (column 1 is empty). column 2 is string while column 3 has date.
I can use concatenate function. However, I would like to use array in order to boost the speed.

Try:(adjust to your range(s)
[A1:A5] = [B1:B5&" "&TEXT(C1:C5,"DD-MM-YYYY")]

Related

How to do an Excel FILTER including a match on multiple (arbitrary) fields?

I'm trying to do a FILTER considering to include a match on several columns (whose number is not predefined at start). Explaining...
Consider the following table:
KEY
COL1
COL2
COL3
A
XXXX
YYYY
ZZZZ
B
YYYY
XXXX
C
ZZZZ
I want to FILTER when COL1 OR COL2 OR COL3 contain, for instance, "XXXX". The result here would be:
KEY
A
B
I know I can do the following, and it works:
=FILTER(A1:A4;(B1:B4="XXXX")+(C1:C4="XXXX")+(D1:D4="XXXX");"")
The question is that the number of columns COL can grow/shrink and one would need to add many OR conditions on the include which is non practical.
I was expecting to be able to do something like the following:
=FILTER(A1:A4;B1:D4="XXXX";"")
But this does not work as the FILTER include parameter needs to have size 1 column per 4 rows in order to operate.
I understand that, to do this, I might "just" need to create an array with TRUEs/FALSEs that can then be used in the include of the FILTER function. But my Excel skills do not go so deep :)
Question: how to I do this in one Excel formula ?
I think you were headed in the right direction. B1:D4="XXXX" gets you to a 3 column by 4 row array of TRUE and FALSE values. What you're wanting to do is then perform a "row-wise OR of all the values in each row, one row at a time." So... Let's actually do just that, a row-wise OR of each row:
=BYROW(B1:D4="XXXX",LAMBDA(r,OR(r)))
This will result in a column array like {TRUE;TRUE;FALSE;FALSE} where TRUE means one or more matches and FALSE means no matches. In this case, rows 1 and 2 had matches, row 3 and 4 did not.
BYROW steps through each row of a two-dimensional array, and passes the row to a lambda function. (Note that in the BYROW/BYCOL functions, the LAMBDA is passed the entire row/column content as an array. This is in contrast to a function like MAKEARRAY where the "r" value passed to the LAMBDA is simply the current row number and not the content.)
Then:
=FILTER(A1:A4,BYROW(B1:D4="XXXX",LAMBDA(r,OR(r))))
Now you can just edit A4 and D4 to be what you need and you're off and running.
If this filter is going to become part of a more complicated formula, consider:
=LET(
matchingRows, BYROW(B1:D4="XXXX",LAMBDA(r,OR(r))),
matchingValues, FILTER( A1:A4, matchingRows ),
// then use "matchingValues" in another formula //
)
I think you're trying to filter out the values if any of the columns contain value "XXXX" in the same row.
In that case you could use:
=FILTER(A2:A4,MMULT(--(B2:D4="XXXX"),SEQUENCE(ROWS(B2:D4),,1,0)))
Or using BYROW:
=FILTER(A2:A4,BYROW(B2:D4,LAMBDA(x,COUNTIF(x,"XXXX"))))
Or more dynamically;
=LET(range,A2:D4,
key,INDEX(range,,1),
cols,INDEX(range,SEQUENCE(ROWS(range)),SEQUENCE(1,3,2)),
FILTER(key,MMULT(--(cols="XXXX"),SEQUENCE(ROWS(cols),,1,0))))

Create random list of strings in random size in excel

I have the following data in my excel.
A
Text 1
Text 2
Text 3
Text 4
Text 5
Text 6
I want to fill column B with random joined data from column A. It should respect the criteria 1> B > 6. i.e. Column B should have a min 1 value from column A or can have a max of up to 6 unique values joined by ,. I can have column B dragged up to 100 rows. But still, they should respect the criteria.
I'm able to get a random value from column A using the formula INDEX($A$1:$A$6, RANDBETWEEN(1, ROWS($A$1:$A$6)), 1), and to join 2 random texts I'm using the formula
=TEXTJOIN(",",true, INDEX($A$1:$A$6, RANDBETWEEN(1, ROWS($A$1:$A$6)), 1), INDEX($A$1:$A$6, RANDBETWEEN(1, ROWS($A$1:$A$6)), 1))
Currently, I'm able to get 2 fixed strings using this formula. Instead of doing the above 6 times, I want to know If there is a way to get this joined string with a random number of unique strings(of the max size of column A length concatenated with a ,).
I'm able to get only 1 value using the random function. Please let me know how can I do this.
You could try:
Formula in B1:
=TEXTJOIN(",",,TAKE(SORTBY(A1:A6,RANDARRAY(COUNTA(A1:A6))),RANDBETWEEN(1,6)))
Note that TAKE() is a new function which is still in BETA. If you don't have access just yet, then try:
=TEXTJOIN(",",,INDEX(SORTBY(A1:A6,RANDARRAY(COUNTA(A1:A6))),SEQUENCE(RANDBETWEEN(1,6))))
In each option:
SORTBY(A1:A6,RANDARRAY(COUNTA(A1:A6))) - Will create a randomized array of the values in column A;
RANDBETWEEN(1,6) - The part which defines the lower- & upper-limit of strings to concatenate;
TAKE/INDEX - A way to retrieve an X amount of rows from the above randomized array. In your case X itself is randomized (see 2nd bullit);
TEXTJOIN() - Concatenate all selected values into a single string.
This formula exploits the functions RANDARRAY and RANDBETWEEN to get a random number of text items to join.
First, I created a dynamic named range called AllTextItems. This automatically expands to capture any number of rows in your dataset:
Then, use the formula:
=TEXTJOIN(",",TRUE,INDEX(AllTextItems,RANDARRAY(RANDBETWEEN(1,ROWS(AllTextItems)),1,1,ROWS(AllTextItems),TRUE)))
in the cells you'd like your joined list.
=TEXTJOIN(",", TRUE, INDEX(A:A, RANDARRAY(1, RANDBETWEEN(1, COUNTA(A:A)), 1, COUNTA(A:A), TRUE)))
EDIT didn't quite beat JvdV to it, but basically a similar basis to his but not using TAKE and not sorting the output

Why am I obtaining this strange result adding all the values in 2 Excel columns?

I am not into Excel and I have this problem trying to sum the values of 2 different column and put this result value into a cell.
So basically I have the D column containing 2 values (at the moment only 2 but will grows without a specific limit, I have to sum all the values in this column). These value are decimal values (in my example are: 0,3136322400 and 0,1000000000).
Then I have an I column containing the same type of value (at the moment only one but also the values in this column can grow without a specific limit...in my example at this time I have this value −0,335305)
Then I have the K3 cell where I have to put the sum of all the valus into the D column and all the values into the I column (following my example it will contain the result of this sum: 0,3136322400 + 0,1000000000 −0,335305.
Following a tutorial I tried to set this simple forumla in the K3 cell:
=SUM(A:I)
The problem is that in this cell now I am not obtaining the expected result (that is 0.07832724) but I am obtaining this value: 129236,1636322400.
It is very strange...I think that maybe it can depend by the fact that the D and the I column doesn't contain only number but both have a textual "heder" (that is the string "QUANTITY" for both the cells). So I think that maybe it is adding also the number conversion of this string (but I am absolutly not sure about this assertion).
So how can I handle this type of situation?
Can I do one of these 2 things:
1) Adding the column values starting from a specific starting cell in the column (for example: sum all the values under a cell without specify a down limit).
2) Exclude in some way the "header" cells from my sum so the textual values are not considered in my sum.
What could be a smart solution for my problem? How can I fix this issue?
The sum function can take several arguments.
=sum(d2:d10000, i2:I10,000, more columns )
This should remove the header from the calculation.
If you turn your data into an Excel Table (Insert > Table), you can use structured referencing to address a table column, excluding the header.
=SUM(Table1[This Header],Table1[That Header])
Then you don't need to reference whole columns. If you add new data to the table, the formula will take that into account.

Create Comma Separated List In Excel

I have two workbooks that I need to pull data from workbook1, to workbook2. The identifier to achieve such is empID Now for eachempID I need to show what location(s) they worked. So sample data looks like this
Workbook1
empID.....Name....Address...City...State....Zip
1
2
3
4
5
Workbook2
empID.......locationworked
1 12
2 33
1 11
4 22
3 9
1 55
5 19
2 76
1 99
I have used this formula to return the data to a different cell for each empID
=IFERROR(INDEX($B$2:$B$8, SMALL(IF($A$11=$A$2:$A$8, ROW($A$2:$A$8)-ROW($A$2)+1), ROW(1:1))),"" )
But I want to create a Comma Separated list and put everything in one cell, like so
1 11,12,55,99
2 33,76
etc
Is there a way to modify the syntax so that a comma separated list is created like in my desired output?
In workbook 2, I added this formula to column C
=IFERROR(VLOOKUP(A1,A2:$C$50,3,0)&","&B1,B1).
This assumes that your data goes as far down as row 50. Replace $C$50 with whatever row is last in your spreadsheet.
If this is a variable list, use
=INDIRECT("A2:C"&MATCH(TRUE,D:D="",0),1)
in place of the
A2:$C$50
however don't forget to use Ctrl + Shift + Enter to set the formula to an array.
Next, copy this formula down all rows. The VLOOKUP will work up the sheet. Then you can reference this list from your report sheet (I believe in this case its Sheet 1) with a VLOOKUP. it will automatically pick the first instance of each employee ID which contains the csv list.
I'd like to point out that whilst bad_neighbor's solution is quite accurate and reusable for future data changes, it is often preferable to avoid lookups where possible, and to store calculated results as values, since these aren't perfectly efficient and tend to slow down the sheet something awful given a larger quantity of data, for example when filtering / unfiltering. It's worse in older versions.
So, if this list formatting were part of a manual operation, and assuming the requirement is for each list item to be in ascending order (per the question's output), I'd do the following instead:
If workbook2's order is important, add an index of the rows (D1 := 1; D2 := D1 + 1; paste values).
Sort workbook2 by [A ascending, B descending], including index if present.
Apply this formula to column C - a fillup version of the lookup.
C1 := IF(A1=A2,C2&", "&B1,B1)
Copy-paste special values column C.
Lookup from workbook1 + copy-paste special values.
Optionally sort back according to original index (D) in workbook2.

Multiple conditions in excel 2002

I am using excel 2002 to create a spreadsheet. What I am trying to do is use the countif function but I have more than one condition. So I have 2 columns with a list of numbers and what I want to say is count the number of occurences where the number x is in one column and the number y is in the other column (in the same row).
E.g.
1 1
1 1
1 2
2 2
2 3
3 3
So if in the above I wanted to count the rows where the first column had the number 1 and the second column had the number 2, the answer should be 1.
I can't use the COUNTIF function because that only allows you to specify one condition and the COUNTIFS isn't available because I am using excel 2002.
Please Help.
This is a job for an array formula. In your case you can do:
=SUM((A1:A6=1)*(B1:B6=2))
entered as an array forumla (Ctrl-Shift-Enter)
The equality tests each return an array of boolean values, and the multiplication does an element-wise multiply (so a logical AND in this case). SUM coerces booleans to numbers when it adds up the resulting array.
EDIT:
In an answer to this similar question:
https://stackoverflow.com/questions/576569/use-2-conditions-in-excel-sumif
Jon Fournier posted a link to:
http://www.cpearson.com/excel/ArrayFormulas.aspx
which has a lot more detail on this sort of thing.
You could create a 3rd column which joins the two other column values with (say) a space or other special character, and then test for the combined value with COUNTIF.
If you don't want to add another column on your worksheet, you could put it on a different worksheet - or even create a dynamic named range.
Personally I love the SUMPRODUCT function but I'm not sure if you have it in Excel2002
Here's a very good resource for multiple conditions:
http://www.ozgrid.com/Excel/sum-if.htm
The DCOUNT function allows for multiple criteria.
The formula would look like this
=DCOUNT(A2:B8,"ColumnA",A11:B12)
if you have your criteria below the data.
Screenshot of data and formula http://img31.imageshack.us/img31/1093/dcount.png

Resources