Excel formula compare two strings - string

I need to compare a product from a transaction file to a product in a lookup worksheet. The values appear to be equal but do not evaluate as equal. Been trying different approaches in VBA (StrComp, CStr, Trim(), adding a letter to the beginning and end of the strings for comparison) and Excel formulas to try and solve this.
In the photo,
Cell C4 was copied from the transaction work book, and cell D4 was copied from the lookup workbook. The formula evaluates to False. If I just type this string value into two different cells and apply the formula, it evaluates to True, but when I copy the two cells from other workbooks (a transaction worksheet, and a lookup worksheet), the formula evaluates to false.
Any ideas?
Excel Formula: =IF(TRIM(UPPER(C4))=TRIM(UPPER(D4)),TRUE, FALSE)
A5314A A5314A

In Excel using the CLEAN function like Chris Neilsen suggested was the answer:
=IF(TRIM(UPPER(CLEAN(C4)))=TRIM(UPPER(CLEAN(D4))),TRUE, FALSE)
As Sam suggested in VBA using this function also worked and the StrComp method evaluated to 0:
a = "A" & Application.WorksheetFunction.Clean(Trim(CStr(Sheet1.Cells(i, 1).Value2))) & "A"`
b = "A" & Application.WorksheetFunction.Clean(Trim(CStr(Sheet2.Cells(i2, 1).Value2))) & "A"
If StrComp(a, b) = 0 Then

Related

Excel - Identify if Worksheet exists between two worksheets?

I have 5 worksheets: (1) Summary (2) 2020 (3) 2019 (4) 2018 (5) 2017
In my "Summary" worksheet, I have Column A with cells each containing the name of each worksheet (A1: 2020, A2: 2019, A3: 2018, A4: 2017) and Column B with the number "1" in each corresponding row.
Let's say I delete the "2019" worksheet so that it is no longer between worksheets "2020" and "2017" - is there any formula that I could use to flag that in row 2? Perhaps through some sort of "if(..,1,0)" formula for Column B? I do not want to use VBA.
There are various error-checking functions available in Excel, and most of them would be appropriate for your situation. The simplest would be to use IFERROR and just refer to a cell on one of the sheets, like this:
=IFERROR('2020'!A1,"Missing")
If the formula receives an error value because it can't find the cell, it will display the text "Missing"; otherwise, it will show the value of the cell.
The disadvantage of doing it this way, hard-coding the sheet name into the function, is that if the sheet is deleted the sheet name will be replaced in the formula with #Ref!, so even if the sheet is restored the formula will need to be changed.
We can enhance the functionality by using an INDIRECT along with the IFERROR. This will take a text string and turn it into a cell reference. So assuming we have worksheet names in column A, we could do this:
=IFERROR(Indirect(A1 & "!A1"),"Missing")
The INDIRECT function takes the value in A1 and combines it with the text string, and reads the whole thing as a cell reference. This has the same result, but is more robust since deleting the sheet and then replacing it will clear the error.
For a slightly more elaborate result, we could next IF and ISERROR instead of using IFERROR. It gives us more control over the result.
=IF(ISERROR(INDIRECT(A1 & "!A1")),"Missing","Found")
ISERROR returns TRUE if the first argument throws an error, an FALSE if it does not. This will return "Found" if the sheet is present, or "Missing" if it isn't.

Excel - Comparing Strings within Two Cells

NEW IMAGE Updated - CLICK ME!
I am comparing two corresponding columns of data in excel. This is just my example simplified. In the first row, it passes because A and B are in column 2. It does not have to contain C, but it can only be any of the letters contained in cell 1:1. For row three, it does not pass because A is not an option within cell 3:1. What conditional formatting would I do for a large data set
You could try:
Conditional formatting rule for A1:B3:
=ISERROR(SUMPRODUCT(FIND(" "&FILTERXML("<t><s>"&SUBSTITUTE($B1," ","</s><s>")&"</s></t>","//s")&" "," "&$A1&" ")))
But since conditional formatting is volatile I don't know if it's wiser to use conditional formatting or just apply the formula to a 3rd column if you are using it on a large dataset. Up to you.
As you might need to run through all entries within one cell, I'm afraid that you might need VBA for solving this one. It would mean that you write an UDF (User-Defined Function) for doing the checking and use that as a basis for a formula for your conditional formatting.
Your function should be something like (not tested):
Function chars_inside_string(characters, inside_string) As Boolean
Dim present As Boolean
present = True
test = Split(characters, "-")
For i = 1 To UBound(test)
present = present AND (InStr(test(i), inside_string) > 0)
Next i
chars_inside_string = present
End Function

Combining cells unless a cell contains certain text

I have an Excel spreadsheet with 7 cells (in a column) with data in, these are C13 to C19
I have a formula that combines all the data in these cells together and puts it into one cell, this formula is =C13&", "&C14&", "&C15&", "&C16&", "&C17&", "&C18&", "&C19 and works fine. However, can I alter this formula to miss out on any cell that contains the text "Nothing else carby"?
You may, in Excel 2019, use:
=TEXTJOIN(", ",,IF(C13:C19<>"Nothing else carby",C13:C19,""))
If "Nothing else carby" can be a substring inside a cell's value, try:
=TEXTJOIN(", ",,IF(ISNUMBER(FIND("Nothing else carby",C13:C19)),"",C13:C19))
Confirm through CtrlShiftEnter
The formula should not be that long, as you can see:
=TEXTJOIN(",",TRUE,IF(C13:C19="Nothing","", C13:C19))
(I just used "Nothing" instead of the whole word for readability reasons.)
Explanation of the parameters:
"," : delimiter: between every cell content, a comma is added.
TRUE : treatment of blank cells (don't put them).
IF (...) : In case cell content is "Nothing", put an empty string.
In combination with the previous parameter,
just one comma will be put in the result:
"a,b,c,e,f,g" and not "a,b,c,,e,f,g" (see result).
Used data:
a
b
c
Nothing
e
f
g
Result:
a,b,c,e,f,g

Excel Formula - Check if cell has formula

What formula do you use to check if another cell has formula? For example, I have 2 columns, A has cells which contains either a formula or a value.
(Column A usually contains Formulas but other users try to change their values by directly typing and replacing the formula that was previously there)
In Column B I want to add a formula that will say "HasFormula" if the cell on Column A has formula and say "PlainValue" if it contains a value.
I'm thinking maybe using =ISNUMBER() but that may not be accurate.
I am using Excel 2010.
Excel actually has a builtin ISFORMULA() function.
Say A1 has a formula and you want to check that. In say B1, you can use:
=If(ISFORMULA(A1),"HasFormula","PlainValue")
Edit: Per your comment, you don't have ISFORMULA(). An alternative is to create a quick UDF, and use the custom function in the worksheet.
In a workbook module, put this code:
Function isFormula(ByVal target As Range) As Boolean
isFormula = target.hasFormula
End Function
Then you can call it like this: =isFormula(A1) and it will return TRUE if A1 has a formula.
If you can't use VBA, then you can use this formula:
=IF(ISERROR(FORMULATEXT(A1)),"PlainText","HasFormula")
The MrExcel website (link below) has this method which uses old code from Excel 4 (which is still present for backward compatibility)...
Define a NAME such as "CellToLeftHasFormula" and in the "refers to" box put
=GET.CELL(48,OFFSET(INDIRECT("RC",FALSE),0,-1))
Then in column B use the formula =CellToLeftHasFormula which will return TRUE if it has.
Be aware that this will mean your Excel will now contain a macro and so will need to be saved as such (xlsm). I use this in Excel 2010.
For full explanation (and other .CELL options, besides 48) see MrExcel link: https://www.mrexcel.com/forum/excel-questions/20611-info-only-get-cell-arguments.html
You can use the Range.HasFormula property.
https://learn.microsoft.com/en-us/office/vba/api/excel.range.hasformula
EDIT:
Text and code from the above link:
"True if all cells in the range contain formulas; False if none of the cells in the range contains a formula; null otherwise. Read-only Variant. ..."
Worksheets("Sheet1").Activate
Set rr = Application.InputBox( _
prompt:="Select a range on this worksheet", _
Type:=8)
If rr.HasFormula = True Then
MsgBox "Every cell in the selection contains a formula"
End If
You can restrict the user by protecting the column A.
You can directly check if a cell contains a formula by using a shortcut Ctrl + `.
You can use vba and write a user defined function :
1. Press alt + F11
2. Insert module in workbook
3. Paste this code
Function IsFormula(cell_ref As Range)
IsFormula = cell_ref.HasFormula
End Function
4. Now, use Isformula in the cell wherever you want.

How to merge rows in a column into one cell in excel?

E.g
A1:I
A2:am
A3:a
A4:boy
I want to merge them all to a single cell "Iamaboy"
This example shows 4 cells merge into 1 cell however I have many cells (more than 100), I can't type them one by one using A1 & A2 & A3 & A4 what can I do?
If you prefer to do this without VBA, you can try the following:
Have your data in cells A1:A999 (or such)
Set cell B1 to "=A1"
Set cell B2 to "=B1&A2"
Copy cell B2 all the way down to B999 (e.g. by copying B2, selecting cells B3:B99 and pasting)
Cell B999 will now contain the concatenated text string you are looking for.
I present to you my ConcatenateRange VBA function (thanks Jean for the naming advice!) . It will take a range of cells (any dimension, any direction, etc.) and merge them together into a single string. As an optional third parameter, you can add a seperator (like a space, or commas sererated).
In this case, you'd write this to use it:
=ConcatenateRange(A1:A4)
Function ConcatenateRange(ByVal cell_range As range, _
Optional ByVal separator As String) As String
Dim newString As String
Dim cell As Variant
For Each cell in cell_range
If Len(cell) <> 0 Then
newString = newString & (separator & cell)
End if
Next
If Len(newString) <> 0 Then
newString = Right$(newString, (Len(newString) - Len(separator)))
End If
ConcatenateRange = newString
End Function
Inside CONCATENATE you can use TRANSPOSE if you expand it (F9) then remove the surrounding {}brackets like this recommends
=CONCATENATE(TRANSPOSE(B2:B19))
Becomes
=CONCATENATE("Oh ","combining ", "a " ...)
You may need to add your own separator on the end, say create a column C and transpose that column.
=B1&" "
=B2&" "
=B3&" "
In simple cases you can use next method which doesn`t require you to create a function or to copy code to several cells:
In any cell write next code
=Transpose(A1:A9)
Where A1:A9 are cells you would like to merge.
Without leaving the cell press F9
After that, the cell will contain the string:
={A1,A2,A3,A4,A5,A6,A7,A8,A9}
Source: http://www.get-digital-help.com/2011/02/09/concatenate-a-cell-range-without-vba-in-excel/
Update: One part can be ambiguous. Without leaving the cell means having your cell in editor mode. Alternatevly you can press F9 while are in cell editor panel (normaly it can be found above the spreadsheet)
Use VBA's already existing Join function. VBA functions aren't exposed in Excel, so I wrap Join in a user-defined function that exposes its functionality. The simplest form is:
Function JoinXL(arr As Variant, Optional delimiter As String = " ")
'arr must be a one-dimensional array.
JoinXL = Join(arr, delimiter)
End Function
Example usage:
=JoinXL(TRANSPOSE(A1:A4)," ")
entered as an array formula (using Ctrl-Shift-Enter).
Now, JoinXL accepts only one-dimensional arrays as input. In Excel, ranges return two-dimensional arrays. In the above example, TRANSPOSE converts the 4×1 two-dimensional array into a 4-element one-dimensional array (this is the documented behaviour of TRANSPOSE when it is fed with a single-column two-dimensional array).
For a horizontal range, you would have to do a double TRANSPOSE:
=JoinXL(TRANSPOSE(TRANSPOSE(A1:D1)))
The inner TRANSPOSE converts the 1×4 two-dimensional array into a 4×1 two-dimensional array, which the outer TRANSPOSE then converts into the expected 4-element one-dimensional array.
This usage of TRANSPOSE is a well-known way of converting 2D arrays into 1D arrays in Excel, but it looks terrible. A more elegant solution would be to hide this away in the JoinXL VBA function.
For those who have Excel 2016 (and I suppose next versions), there is now directly the CONCAT function, which will replace the CONCATENATE function.
So the correct way to do it in Excel 2016 is :
=CONCAT(A1:A4)
which will produce :
Iamaboy
For users of olders versions of Excel, the other answers are relevant.
For Excel 2011 on Mac it's different. I did it as a three step process.
Create a column of values in column A.
In column B, to the right of the first cell, create a rule that uses the concatenate function on the column value and ",". For example, assuming A1 is the first row, the formula for B1 is =B1. For the next row to row N, the formula is =Concatenate(",",A2). You end up with:
QA
,Sekuli
,Testing
,Applitools
,Visual Testing
,Test Automation
,Selenium
In column C create a formula that concatenates all previous values. Because it is additive you will get all at the end. The formula for cell C1 is =B1. For all other rows to N, the formula is =Concatenate(C1,B2). And you get:
QA,Sekuli
QA,Sekuli,Testing
QA,Sekuli,Testing,Applitools
QA,Sekuli,Testing,Applitools,Visual Testing
QA,Sekuli,Testing,Applitools,Visual Testing,Test Automation
QA,Sekuli,Testing,Applitools,Visual Testing,Test Automation,Selenium
The last cell of the list will be what you want. This is compatible with Excel on Windows or Mac.
I use the CONCATENATE method to take the values of a column and wrap quotes around them with columns in between in order to quickly populate the WHERE IN () clause of a SQL statement.
I always just type =CONCATENATE("'",B2,"'",",") and then select that and drag it down, which creates =CONCATENATE("'",B3,"'",","), =CONCATENATE("'",B4,"'",","), etc. then highlight that whole column, copy paste to a plain text editor and paste back if needed, thus stripping the row separation. It works, but again, just as a one time deal, this is not a good solution for someone who needs this all the time.
I know this is really a really old question, but I was trying to do the same thing and I stumbled upon a new formula in excel called "TEXTJOIN".
For the question, the following formula solves the problem
=TEXTJOIN("",TRUE,(a1:a4))
The signature of "TEXTJOIN" is explained as TEXTJOIN(delimiter,ignore_empty,text1,[text2],[text3],...)
I needed a general purpose Concatenate With Separator (since I don't have TEXTJOIN) so I wrote this:
Public Function ConcatWS(separator As String, ParamArray cell_range()) As String
'---concatenate with seperator
For n = LBound(cell_range) To UBound(cell_range)
For Each cell In cell_range(n)
If Len(cell) <> 0 Then
ConcatWS = ConcatWS & IIf(ConcatWS <> "", separator, "") & cell
End If
Next
Next n
End Function
Which allows us to go crazy with flexibility in including cell ranges:
=ConcatWS(" ", Fields, E1:G2, L6:M9, O6)
NOTE: "Fields" is a Named Range and the separator may be blank

Resources