Why do we use "&cell_reference&" in Excel? - excel-formula

Lets say we have a table like this:
A
B
1
Aaron
20
2
Bob
30
What is the difference between =""&A1&" has "&B1&" cakes" and = A1 & " has " & B1 & " cakes"?
Why do we use "&cell_reference&" anyways?

Related

How to compare the number of matching words in two cells? Excel / Google Sheets

Is it possible to create a formula in Excel or Google Sheets that compares two cells with text values and returns the number of words that match.
Example:
Cell A2 = This is an apple
Cell B2 = This was a rotten apple
Value returned: 2
With Excel 365 you could do:
=SUM( --ISNUMBER( MATCH( UNIQUE( TEXTSPLIT( A2, " "), 1 ),
UNIQUE( TEXTSPLIT( B2, " "), 1 ),
0 ) ) )
google sheet sample_1
google sheet sample_2
Since you didn't mention if it counts or not when a same word appears more than one time in the string, here are two samples, where sample_1 do count duplicates, and sample_2 don't:
// sample_1:
=ArrayFormula(SUM(INT(
LAMBDA(COL,ROW,
REGEXREPLACE(SEQUENCE(COUNTA(ROW))&"","[0-9]+",COL)=ROW
)(SPLIT($A$2," "),TRANSPOSE(SPLIT($B$2," ")))
)))
// sample_2:
=ArrayFormula(SUM(INT(
LAMBDA(COL,ROW,
REGEXREPLACE(SEQUENCE(COUNTA(ROW))&"","[0-9]+",COL)=ROW
)(UNIQUE(SPLIT($A$2," ")),TRANSPOSE(UNIQUE(SPLIT($B$2," "))))
)))
This is not the most elegant solution but it works as the question was originally posted.
= query(query(transpose(split(A2 & " " & B2, " ")),
"Select count(Col1) group by Col1 label count(Col1) '' "),
"Select count(Col1) where Col1 != 1 group by Col1 label count(Col1) 'Total matching' ")

How to keep the cell reference in VBA

Lets say I have the following data:
A B C
1 =B1
2 =C2
3 =C3
I want to write a macro than can add text around the values in column A without losing the reference.
For example
A B C
1 [Hello].[1] 1
2 [Hello].[20] 20
3 [Hello].[10] 10
As an example for A1 I use:
.cells(1,1) = "[Hello].[" & .cell(1,2) & "]"
This give me the wanted value but in the end I lose the cell reference in A1.
I rather want this:
A B C
1 [Hello].[B1] 1
2 [Hello].[C2] 20
3 [Hello].[C3] 10
Of course with the actual value of the reference and not just the addresses.
With .cells(1,1)
.value = "=" & .cell(1,2).address
.format= ""[Hello].[" 0 "]""
End With
this should do. more info on this excel exotic syntax on that random website https://exceljet.net/custom-number-formats
best of luck, but consider using another language than vba

Formula to determine error code based on binary text output

I am trying to translate some error codes using 22 different formulas and concatenating them afterward to report all errors.
Here is an example of the error code (it is 22 1's and 0's):
0000000000000000001000
0000000000001000000000
0000100000001000000010
Each 1 represents a different error.
Here is one example of the formula I am using:
=IF(NUMBERVALUE(LEFT(D61,1))>=1, " ERROR 1","")
I have 22 of these formulas and the only differences in any of them are the result and which character it looks for.
My issue is a code with multiple different errors (i.e. more than one 1 is present in the code) will show every error code after the first occurrence of a 1. So in the third error code example I listed above, my formula shows every error code after that first 1. Also, it may not matter but I am using this to combine the resulting errors into 1 column:
=CONCATENATE(F2,G2,H2,I2,J2,K2,L2,M2,N2,O2,P2,Q2,R2,S2,T2,U2,V2,W2,X2,Y2,Z2,AA2)
I am trying to get my formula to show just the corresponding errors that go with the 1's, not everything after it. I hope this makes sense as it's kind of hard to explain.
If it helps at all the data is being extracted from text files that come in daily and I had to format the cells as "Text" in order for them to show the 0's before the 1's.
Thanks!
---- EDIT FOR ANYONE CURIOUS ----
This formula ended up working, I placed it in column D, the 22 character code is in column E.
=IF(MID(E2,1,1) > "0","ERROR 1 ","") & IF(MID(E2,2,1) > "0","ERROR 2 ","") & IF(MID(E2,3,1) > "0","ERROR 3 ","") & IF(MID(E2,4,1) > "0","ERROR 4 ","") & IF(MID(E2,5,1) > "0","ERROR 5 ","") & IF(MID(E2,6,1) > "0","ERROR 6 ","") & IF(MID(E2,7,1) > "0","ERROR 7 ","") & IF(MID(E2,7,1) > "0","ERROR 7 ","") & IF(MID(E2,8,1) > "0","ERROR 8 ","") & IF(MID(E2,9,1) > "0","ERROR 9 ","") & IF(MID(E2,10,1) > "0","ERROR 10 ","") & IF(MID(E2,11,1) > "0","ERROR 11 ","") & IF(MID(E2,12,1) > "0","ERROR 12 ","") & IF(MID(E2,13,1) > "0","ERROR 13 ","") & IF(MID(E2,14,1) > "0","ERROR 14 ","") & IF(MID(E2,15,1) > "0","ERROR 15 ","") & IF(MID(E2,16,1) > "0","ERROR 16 ","") & IF(MID(E2,17,1) > "0","ERROR 17 ","") & IF(MID(E2,18,1) > "0","ERROR 18 ","") & IF(MID(E2,19,1) > "0","ERROR 19 ","") & IF(MID(E2,20,1) > "0","ERROR 20 ","") & IF(MID(E2,21,1) > "0","ERROR 21 ","") & IF(MID(E2,22,1) > "0","ERROR 22 ","")
put this in F2 and copy/drag over:
=IF(--MID($D2,column(A:A),1)=1," ERROR " & COLUMN(A:A),"")
If you have Office 365 Excel, you can combine them all into one Array formula that will test each part and return the concatenated values:
=TEXTJOIN(" ",TRUE,IF(--MID($D2,ROW($1:$22),1) = 1, "ERROR " & ROW($1:$22),""))
This one being an array formula needs to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.
With a typical 22 bit value in cell A1, in B1 enter:
=IF(MID($A$1,ROWS($1:1),1)="1","Error # "&ROWS($1:1),"")
and copy down:
To improve the results (remove the ugly empties), in C1 enter the array formula:
=IFERROR(INDEX($B$1:$B$22,SMALL((IF(LEN($B$1:$B$22),ROW(INDIRECT("1:"&ROWS($B$1:$B$22))))),ROW(A1)),1),"")
and copy down:
Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key. If this is done correctly, the formula will appear with curly braces around it in the Formula Bar.
EDIT#1:
Put the following User Defined Function UDF() in a standard module:
Public Function ErrorLister(s As String)
Dim arr(1 To 22) As String
Dim i As Long, j As Long
For i = 1 To 22
arr(i) = ""
Next i
j = 1
For i = 1 To 22
If Mid(s, i, 1) = "1" Then
arr(j) = "Error " & i
j = j + 1
End If
Next i
ErrorLister = arr
End Function
Then select B1 through W1, click in the Formula Bar, and enter the array formula:
=ErrorLister(A1)
then copy B1 through W1 downwards:

to get last row of each day from excel sheet

i need to find the last record of each day in excel if there are multiple entries
*******intention is to get EOD balance ROW OF EACH DATE********
like i have data in excel something like this
date CR_DR amount EOD balance
----------------------
7/9/2017 19:09 CR 10 10
7/10/2017 18:37 CR 25 35
7/10/2017 21:06 DR 10 25
7/11/2017 19:21 CR 15 40
7/15/2017 14:17 DR 20 20
7/17/2017 17:12 CR 100 120
7/18/2017 7:44 DR 30 90
7/18/2017 14:08 DR 50 40
7/18/2017 16:52 CR 120 160
for which i need to get data like (get the last row of each day)
7/9/2017 19:09 CR 10 10
7/10/2017 21:06 DR 10 25
7/11/2017 19:21 CR 15 40
7/15/2017 14:17 DR 20 20
7/17/2017 17:12 CR 100 120
7/18/2017 16:52 CR 120 160
Solution 1
Enter the following formula in Cell F2
=IFERROR(MAX(IF(INT($A$2:$A$10)=INT(INDEX($A$2:$A$10, MATCH(0, FREQUENCY(IF(EXACT(INT($A$2:$A$10), TRANSPOSE(INT($F$1:F1))), MATCH(ROW($A$2:$A$10), ROW($A$2:$A$10)), ""), MATCH(ROW($A$2:$A$10), ROW($A$2:$A$10))), 0))),$A$2:$A$10,0)),"")
This is an array formula so commit it by pressing Ctrl+Shift+Enter. Drag/Copy down as required.
Then in Cell G2 enter
=VLOOKUP($F2,$A$2:$D$10,COLUMN(C1)-COLUMN($A$1),FALSE)
Drag this formula across (to right) till Cell I2 and down as required.
See image for reference.
Solution 2
Instead of using an ugly looking long formula, here we'll use a helper column.
In Cell F2 enter the following formula
=MAX(IF(INT($A$2:$A$10)=INT(A2),$A$2:$A$10,0))
This is an array formula so commit it by pressing Ctrl+Shift+Enter. Drag/Copy down as required.
Then in Cell G2 enter
=IFERROR(INDEX($F$2:$F$10,MATCH(0,INDEX(COUNTIF($G$1:G1,$F$2:$F$10),0,0),0)),"")
Drag/Copy down as required.
Finally, in Cell H2 enter
=VLOOKUP($G2,$A$2:$D$10,COLUMN(C1)-COLUMN($A$1),FALSE)
Drag this formula across (to right) till Cell J2 and down as required.
See image for reference.
EDIT : As per comment.
Instead of
=VLOOKUP($G2,$A$2:$D$10,COLUMN(C1)-COLUMN($A$1),FALSE)
use below formula in Cell H2
=INDEX(B$2:B$10,MAX(IF($A$2:$A$10=$G2,ROW($A$2:$A$10)-ROW(INDEX($A$2:$A$10,1,1))+1)))
This is an array formula so commit it by pressing Ctrl+Shift+Enter. Drag this formula across (to right) till Cell J2 and down as required. Drag/Copy down as required.
Screen-Shot :
Ok, this is crude and I'm sure there are "proper" ways to do this, but it works for what you want to do. I used columns A thru D for the data. In column F I used the formula =Left(A3,5) and copied it down. You will have to modify according to your situation. Hope this helps or gets you headed in right direction. Good Luck
Sub FindLast()
'FIND LAST LISTED DATE AND COPY
Dim myCount, myCount2, myRange, myRow, x, y, r
r = 3
myCount = Sheets("Sheet1").UsedRange.Rows.Count - 2
MsgBox "Rows used are " & myCount
For x = 3 To myCount Step 1
y = Range("F" & x).Value
MsgBox "Value of x is " & x
MsgBox "Value of y is " & y
myCount2 = WorksheetFunction.CountIf(Range("F3:F" & myCount + 2), y)
MsgBox "myCount2 value is " & myCount2
Max_date = Application.WorksheetFunction.Max(Range("A" & x & ":" & "A" & x + myCount2 - 1))
myRow = Range("A" & x + myCount2 - 1).Row
MsgBox "myRow number is: " & myRow
MsgBox CDate(Max_date)
Sheets("Sheet1").Range("I" & r).Value = Max_date
Sheets("Sheet1").Range("J" & r).Value = Range("B" & myRow).Value
Sheets("Sheet1").Range("K" & r).Value = Range("C" & myRow).Value
Sheets("Sheet1").Range("L" & r).Value = Range("D" & myRow).Value
r = r + 1
If myCount2 <> 1 Then
x = x + (myCount2 - 1)
Else
x = x
End If
Next x
End Sub
You will notice there are a lot of message boxes. I used these to test the parameters and make sure they were coming out like I wanted. When you are comfortable with how it's working, just comment them out.
Also, the output is set to I3 thru L8 for the example you posted. Please make sure to edit these if your real sheet has any data in these areas. My suggestion is test first to get familiar with what's happening. I did't put any "failsafes" or "exits" in, as I ran out of time. There are plenty of examples on this site to help with that.
Regards
It requires entering the dates in column A (I didn't feel motivated enough to figure out how to pull the unique ones out), but if you are feeling spunky, you could use some array formulas. :-)
Column A Column B C D E
7/9/2017 7/9/2017 19:09 CR 10 10
7/10/2017 7/10/2017 21:06 DR 10 25
7/11/2017 7/11/2017 19:21 CR 15 40
7/15/2017 7/15/2017 14:17 DR 20 20
7/17/2017 7/17/2017 17:12 CR 100 120
7/18/2017 7/18/2017 16:52 CR 120 160
Enter dates in column A. Then in column B enter:
=IFERROR(INDEX(Date,SMALL(IF(ROUNDDOWN(Date,0)=$A1,ROW(Date)-1),COUNTIFS(Date,">="&$A1,Date,"<"&$A1+1))),"")
closing out of the cell with CTRL-SHIFT-ENTER so that the formula in the formula bar looks like
{=IFERROR(INDEX(Date,SMALL(IF(ROUNDDOWN(Date,0)=$A1,ROW(Date)-1),COUNTIFS(Date,">="&$A1,Date,"<"&$A1+1))),"")}
Note: it does not work to enter the curly braces yourself. ;-) You must close out of the cell with CTRL-SHIFT-ENTER!
The Date in the formula is a named range - I don't like typing $A$2:$A$10 all the time. If you don't want to do named ranges, replace Date with $A$2:$A$10 or your applicable range.
Column C:
=IFERROR(INDEX(CR_DR,SMALL(IF(ROUNDDOWN(Date,0)=$A1,ROW(Date)-1),COUNTIFS(Date,">="&$A1,Date,"<"&$A1+1))),"")
Same thing - close out of cell with CTRL-SHIFT-ENTER. Named range again for CR_DR, same rules as for Date above. Copy/paste the for the other columns, changing the range that INDEX is searching as appropriate for the column you want.
Explanation: ROUNDDOWN removes the time from the date in your source data so that it can match it to the date in column A. If the date matches, its row (-1 because the row number of A2 is 2, but it is the first item in $A$2:$a$10) is put into an array for the SMALL function. This chooses the nth smallest item from the array, defined by the COUNTIFS function, which counts how many dates fall between the date in column A and the day after the date in column A (thus actually giving you the largest item). INDEX then uses the range and looks up the row number that SMALL delivered to it. If it errors out, the cell is blank.
If you don't want to hand-enter specific dates, auto-fill for all dates and it will simply be blank on dates with nothing:
7/9/2017 7/9/2017 19:09 CR 10 10
7/10/2017 7/10/2017 21:06 DR 10 25
7/11/2017 7/11/2017 19:21 CR 15 40
7/12/2017
7/13/2017
7/14/2017
7/15/2017 7/15/2017 14:17 DR 20 20
7/16/2017
7/17/2017 7/17/2017 17:12 CR 100 120
7/18/2017 7/18/2017 16:52 CR 120 160

Need help creating a user defined function in Excel with VBA

I'm totally stuck on creating a user defined function in Excel VBA for the following problem. Any help would be greatly appreciated.
My excel file looks like this (only adding a small portion for the sake of brevity):
A B C D
1 Susan Reagan Smith
2 Jill L Taylor
3 Sarah Sullivan Williams
4 Roger J Lopez
I would like a function that determines: If A1 = Susan OR Jill OR Sarah, AND LEN (length of string) of B1 >1 THEN D1 = A1 /2 B1 C1 ELSE A1 /2 C1
So the output in column D would look like:
A B C D
1 Susan Reagan Smith Susan /2 Reagan Smith
2 Jill L Taylor Jill /2 Taylor
3 Sarah Sullivan Williams Sarah /2 Sullivan Williams
4 Roger J Lopez Roger /2 Lopez
I want to use VBA because I have many names to add and don't really want a huge formula in cell D1 with a lot of nested if statements. I've been working on it myself but it's just a mess and I'm too embarrassed to post it here. Thanks guys!
Public Function JoinNames(A, B, C)
If FirstNameMatches(A) And Len(B) > 1 Then
JoinNames = A & " /2 " & B & " " & C
Else
JoinNames = A & " /2 " & C
End If
End Function
Private Function FirstNameMatches(N) As Boolean
Select Case N
Case "Susan", "Jill", "Sarah"
FirstNameMatches = True
End Select
End Function
Have a list of names somewhere else on a sheet.
Use IF(ISNA(MATCH(A1, list_of_names, 0)), "Not found", "Found") to figure out if the name is recognized.

Resources