Excel - Comparing Strings within Two Cells - excel

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

Related

Change cell formatting

How can I change formatting of the cell that uses vba function from the code of that function?
Example I tried:
made vba module (see code below)
put in excel sheet in some cell "=test()"
function "works" - it changes cell value and shows 2 popup windows. But formatting stays the same
Function test()
MsgBox (Application.ThisCell.NumberFormat) ' shows "General"
Application.ThisCell.NumberFormat = "Currency"
'Application.ThisCell.NumberFormat = "#,##0_);[Red](#,##0)"
MsgBox (Application.ThisCell.NumberFormat) ' still shows "General"
test = 12345.6
End Function
How to make it work?
(I need custom formatting rule, not "currency", but custom rule (test example in commented line) doesn't work too)
Please don't kill me for this wacko solution :-)
I have created a function, resulting in the number 4096:
Function very_weird_UDF()
very_weird_UDF = 4096
End Function
Then I have created a conditional formatting rule, based on the present of that UDF (User-Defined Function) inside the formulatext of that cell, as you can see here:
Conditional formatting's formula: =FIND("very_weird_UDF",FORMULATEXT((B2)))
Screenshot of what it looks like:
For your information:
Cell "C3" contains the formula, the other cells in B2:C5 just contain the value 4096.
Cells E2:F5 contain the mentioned =Find(...) formula.
Oh, if you wonder why I use such a weird name for the UDF? Just imagine I used a simpler name, like 'a' or 'f' or something like that. That might highlight a lot of cells who don't use that formula (like a simple =If(...) or an =And(...) or ...

Conditional formatting using the INDIRECT function fails with boolean AND or OR or with cells containing formulas

I have the following function for checking whether column L contains the word "completed" and I use INDIRECT to be able to color the whole row with Conditional Formatting:
=INDIRECT("l"&ROW())="completed"
This function works. However, I need to extend this, I want to use Conditional Formatting based on an extra cell as well, so I tried this:
=AND(INDIRECT("l"&ROW())="completed";INDIRECT("m"&ROW())="duplicate")
When I use this second function inside the Excel worksheet they give the proper TRUE or FALSE.
Furthermore, I needed a Custom Formatting on the result of a formula in a cell. I tried the following:
=INDIRECT("n"&ROW())=123456
This only worked if I removed the formula in the cell with the result itself as a number. Again, the function worked when pasted in an worksheet cell.
Is there a way to make this work inside Excel or is there a limit to what Conditional Formatting functions can do?
In case you ask: AND(1;1) works and makes everything yellow, AND(INDIRECT("n"&ROW())=123456;1) does not work, nor does replacing AND with OR.
The semicolon is because I am in the Dutch locale. Replace it with a comma if you are in an English locale.
Not sure why this wouldn't work in Conditional Formatting. But you can simply replace the AND function with * such as:
=(INDIRECT("l"&ROW())="completed")*(INDIRECT("m"&ROW())="duplicate")
You have to think in terms of xlR1C1 formulas to understand CFRs. A CFR based on a formula thinks of it as =RC12="completed" or more completely =AND(RC12="completed", RC13="duplicate").
The xlR1C1 formula does not change no matter what cell you paste it to; it is in this way that CFRs can be applied to a wide range of cells without expending calculation cycles to update the formula for each individual cell. RC12 means 'the cell in column L on the row you are on'. It does not change if filled down, filled right or copied to any other location.
Now unless you are actually working in xlR1C1 (File, Options, Formulas, Working with Formulas, R1C1 reference style) you have to convert the xlR1C1 to xlA1 style. If you are applying the CFR to a number of rows starting with the first row then the R becomes 1 and the C12 becomes $L.
'xlR1C1
=AND(RC12="completed", RC13="duplicate")
'xlA1
=AND($L1="completed", $M1="duplicate")
If you were applying the CFR to a range starting in row 2 change the $L1 to $L2 and the $M1 to $M2.
Among other reasons for not putting the xlR1C1 style formula directly into the CFR creation dialog when working in xlA1 style is that there actually is a RC12 cell in xlA1.

In Excel, how can I avoid repeating a big part of the formula just to check if the return value is blank?

I have a situation where I am referencing cells in a different worksheet and returning the values of cells from that worksheet. Although it works, I find my current method inefficient because I have to repeat the formula in the logical test part of the IF statement:
=IF(**EXTREMELY LONG COMPLICATED FORMULA** <> "", **EXTREMELY LONG COMPLICATED FORMULA**, "")
As you can see, I must repeat the main part of the formula just to check if it is blank first. If I do not do this, I get a zero in the cell (for blank values in the referenced worksheet). I'm looking for something more like:
=IF(**EXTREMELY LONG COMPLICATED FORMULA** <> "", **RETURN VALUE**, "")
This looks cleaner to me because I won't have to repeat myself. Also, if we ever have to update the formula, I won't have to duplicate my changes to the repeated parts. Is there a way to do this?
The above is actually a simplified version of my problem, but the answer should get me where I need to go. My actual formula has nested IF statements checking along the way for blanks. For reference, here it is:
=IFERROR(IF(SMALL(IF(ImportedData!$H$2:$H$1000>=DataFilters!$A$1,IF(ImportedData!$G$2:$G$1000=DataFilters!$A$15,ROW(ImportedData!A$2:A$1000)-ROW(ImportedData!A$2)+1)),ROWS(ImportedData!A$2:ImportedData!A2))<>"",IF(INDEX(ImportedData!A$2:A$1000,SMALL(IF(ImportedData!$H$2:$H$1000>=DataFilters!$A$1,IF(ImportedData!$G$2:$G$1000=DataFilters!$A$15,ROW(ImportedData!A$2:A$1000)-ROW(ImportedData!A$2)+1)),ROWS(ImportedData!A$2:ImportedData!A2)))<>"",INDEX(ImportedData!A$2:A$1000,SMALL(IF(ImportedData!$H$2:$H$1000>=DataFilters!$A$1,IF(ImportedData!$G$2:$G$1000=DataFilters!$A$15,ROW(ImportedData!A$2:A$1000)-ROW(ImportedData!A$2)+1)),ROWS(ImportedData!A$2:ImportedData!A2))),""),""),"")
The most obvious solution is to use a helper column or cell. Just put EXTREMELY LONG COMPLICATED FORMULA somewhere in your spreadsheet, then refer to that cell in your IF formula.
Edit
To avoid a helper column, here is a trick I've used on occasion:
=IFERROR(VALUE(long_formula&""),"")
What this does is, concatenate the result of long formula with an empty string (which converts it to a string), then take the value of all that (which converts it back to a number if possible), then substitute any errors with a blank. (An error would occur if you attempt to take the value of something that's not numerical.)
This will only work if you either have a numerical result or an empty result. It will fail if you have a text result.
As of March 2020, Excel includes the LET function. You can write:
=LET(ELCF,**EXTREMELY LONG COMPLICATED FORMULA**,IF(ELCF <> "", ELCF, ""))
Where the three parameters are:
the name you will use to refer to your calculation,
the calculation itself, and
the final formula using the calculation.
The function also allows for multiple names to be defined. The general syntax is:
=LET(name1, name_value1, calculation_or_name2, [name_value2, calculation_or_name3...])
https://support.microsoft.com/en-us/office/let-function-34842dd8-b92b-4d3f-b325-b8b8f9908999
Do you need the blank in the cell for further calulations or if-functions or Do you just dont want to see the 0s?
Second case:
Just use a number format for the column like
0,00;-0,00;"";#
First case:
Put the following code in a module:
Option Explicit
Public Function IfEmpty(LongFormula As String) As String
If LongFormula = "" Then
IfEmpty = ""
Else
IfEmpty = LongFormula
End If
End Function
And use it in your worksheet like
=IfEmpty(**EXTREMELY LONG COMPLICATED FORMULA**)

Excel Formunal needed to Merge

Hey All on my excel sheet there is a column that i need to concatenate. I need the combine all the rows in the same colors into one row, I need to do this for a list of 10000+ rows. Any ideas?
ALSO THE COLORS I added in to explain what I need to combine, there are no colors in the spreadsheet.
In a helper column (just any empty column, I Assume AA here, change that to your needs) starting with row 1 (I assume the colored column is F, change that to your needs):
=IF(LEN(A2),F1,F1&", "&AA2)
copy down till the end
copy whole column
paste as values only (to column F)
Set filter to show only rows which are empty in column A
select all
delete all
turn off the filter
finished :)
If you are willing to use VBA it could be as simple as inserting this into a module and then using it with other regular functions to build your string:
Function WhatColor(r As Range) As Long
WhatColor = r.Cells.Interior.Color
End Function
If it has to be a "somewhat" native function, check out the GET.CELL answer here.

Conditional formatting of a column based off having any text in the first column of the same row

I'm not sure if this is even possible without going to VB, but I was trying to do it through conditional formatting. Basically I have a column (Column K) that will always be the same value (345) if there is a record entered in that row. Basically when I populate my reports I simply want the value (345) to be entered into Column K if there is any data in that row. I was trying to just use Column A as a reference. I was messing with =IF(ISTEXT(Col.A location),"345","") but that's getting nowhere. So, I'm looking for ideas outside of vba, but if there are no possibilities then vba is the way to go I suppose. :)
Assuming your data is in columns A to J, and that it starts in row 2, enter this in K2 and copy down as necessary:
=IF(COUNTA(A2:J2),345,"")
Edit: For a conditional formatting formula you don't need the "If" part, because the formatting is already ... conditional:
=COUNTA(A2:J2)
Will this work?
=IF(ISBLANK(A1),"","345")
This code works to tell whether column A has something in it or not COUNTA(INDIRECT("$A$"&ROW()))>0, but I don't think you can set the value of the cell using conditional formatting. But with conditional formatting you have to know ahead of time how far down your data is going to go unless you just put it in all the rows.
Why don't you just put it in your VBA code when you are copying, you can find out what the last row is then put the IF() formula in. You can use this code:
Dim r1 As Range
Set r1 = Range("K1")
r1.NumberFormat = "General"
r1 = "=IF(COUNTA(INDIRECT(""$A$""&ROW())>0,""345"","""")"
r1.AutoFill Destination:=Range(r1, r1.Offset(200))

Resources