I have a spreadsheet that uses VBA to retrieve data from a database and copy it into a worksheet. I need to compare two columns to see if they match. The possible values are "TRUE", "FALSE", or a blank cell.
I have a simple formula to flag the row if the values are different: =IF(I2=J2,"Same","Difference")
This formula works fine if the values match, or if they are TRUE/Blank or TRUE/FALSE, but it does not evaluate as expected when there's a FALSE and a blank cell!
If I type in the word "FALSE" the formula evaluates properly (like in the image below). The formula only seems to be tripped up when the data is pasted from the database by VBA.
I suspect this has something to do with Excel treating FALSE and blank cells as both equivalent to "0", but I cannot figure out how to easily workaround this "feature." A couple things I tried:
I confirmed the cell types are set to "Text" for all cells and I tried using VBA to force the format to be text by using Range("I:J").NumberFormat = "#". Still evaluates to "Same."
I tried manually recalculating the formula by using the "Calculate Now" option in the Formulas ribbon. Still evaluates to "Same."
I can write an ugly formula in the IF statement to check if the cells match: =IF(OR(AND(I2="FALSE",J2="FALSE"), AND(I2="TRUE",J2="TRUE"), AND(I2="",J2="")),"Same","Difference") Is there a way to simplify this formula or force the I2=J2 formula to calculate correctly in case we ever have to handle data other than true/false/blank?
You can use EXACT:
=IF(EXACT(A1,B1),"Same","Different")
The reason is that it is defined as FALSE = 0 and TRUE = 1 (in formulas) or TRUE = -1 (in VBA).
If you compare A1=B1 you compare the values of these cells. And the value of FALSE is 0 exactly as the value of a blank cell is 0. So their value is equal and therefore they are considered the same.
In addition to only check if the values are the same A1=B1 also check if the data type is the same TYPE(A1)=TYPE(B1) using the TYPE function:
=IF(AND(A1=B1,TYPE(A1)=TYPE(B1)),"same","different")
This prevents implicit data type conversion (for example from a boolean FALSE to a integer 0 and vice versa).
TYPE() can distinguish between the following data types:
1 = Number
2 = Text
4 = Logical value (Boolean)
16 = Error value
64 = Array (Matrix)
128 = Compound data
Related
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
I have a simple formula that outputs as TRUE when I put it in a cell but when I put the exact same formula in a conditional format the highlight does not show up. There are no other rules being applied to this cell.
=OR(AND(OR(LEFT(I56,1)="U", RIGHT(I56,1)="U"), H$5="Total",H56>$F56),
AND(OR(LEFT(I56,1)="U", RIGHT(I56,1)="U"), H$5="OC-Normalized",H56>$D56))
I71 = "U" (TRUE),
H5 = "Total" (TRUE),
H71 = 90,
F71 = 30,
So H71 > F71 (TRUE)
I have about 170 rows and columns A thru AFG. The conditional is only being applied to the first column of data (column H) currently.
I cant figure out how to get the excel table posted here and I don't want to spend half an hour trying. Unless it really helps. The big question for me is WHY would this formula return TRUE in a cell and not in conditional format on the cell?
Returns TRUE but conditional formatting doesn't get applied to the cell. I have tested each individual part of the formula and they all of one of the OR sections returns TRUE.
Blach! I figured it out. I wish the 10 "how to guides" I read mentioned that you have to use the first cells address in your range when applying formatting. Otherwise it gets wonky. Once I did that it all seems to work
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
I am trying to format cells based on values entered in cell. The cells represent multiplication sheet. After entering data B2 cell, if result is multiple of its row and column I.e 2 x 2 = 4 it should be green , otherwise it should be red . In short, I want to make this a multiplication trainer sheet. I tried using an if condition, but couldn't figure out how to put color for true and false.
The conditional formatting works fine when each cell of dest sheet dest sheet to test multiplication skills is referred whith source sheet from which tables can be learned referred with each cell of source sheet . When I tried to specify ranges in condition of formatting it throws an error and it says only map one cell instead of range. Its rather hard to map each and every cell to compare result . Please help me to format to differentiate true and false after entering value into cell.
you can download my file from here . http://s000.tinyupload.com/?file_id=00186543650788471896
Select the range you would like to format, enter conditional formatting:
I'm working on an excel spreadsheet and want an if statement in a cell that allows user input if a certain condition is met, and calculates a value otherwise. Something like
=if(condition true, whatever user wants, 5*$A$1,)
Is there a way to do this?
You won't be able to have the user-input in the same cell as your formula. (without using VBA)
To do it without VBA you will need to use at least 2 cells, one with your formula, and one for the user value
There are a couple of ways you can do it with VBA
Heres a simple one, but would not really recomment it, if lots of cells use this it you'll get lots of inputboxes!
usage: =IF(condition, UserInput(), false result)
Public Function UserInput() As Integer ' used integer as an example
Dim Result As Variant
Result = Application.InputBox("Enter an Integer", "Input Required", , , , , , 1) ' inputbox, the final 1 makes it only accept numbers
If VarType(Result) = vbBoolean Then
UserInput = 0 ' the default value
Else
UserInput = CInt(Result) ' make sure its an integer
End If
End Function
Another one, would involve using the selection change and cell change events to read the initial value of the cell being changed, and allow the change (adding the value into the initial formula's "true" block or deny the changes by reverting the cells formula to the initial one.
You either need to use a Macro to update only null columns or you need to allow user to enter values in another column and then merge the values in this column, third option is to fill it with formulas and allow people to edit it to any value if they want only values
=IF(C11="Economic",120,IF(C11="DBServer",480,IF(C11="Gamer",120,IF(C11="Custom",M15,"null"))))
My example was to build an optimal computer given certain constraints. There was a drop down with Economic, DBServer, Gamer, and Custom as options. If you chose economic, then 120 would show up in the cell, DbServer meant 480, etc. If you selected custom, then it would refer to cell M15 which was a user input that didn't affect the code of the cell you wanted the final number in.