Excel, VBA, VLookup text value with and without apostrophe - excel

After hours of trial and error, I've determined that doing an Application.Vlookup for a text string in a range returns:
Error 2042 if the text in the search range isn't preceded by an apostrophe
Returns a value in the search range if the text is preceded by an apostrophe
Both search variables are type 8, string, but only the range value with the apostrophe is considered equal, even though the string being searched for, does not have an apostrophe in it.
Is there a way to have Vlookup consider text without an apostrophe equal? And no, I'd rather not have to add an apostrophe to everything.
Thanks in advance.
PS Made a small program, using a different version of Excel but still getting undesired results...
Excel Program and VLookup results
Excel Macro and it's results
In the spreadsheet, all the number values were entered by keyboard.
Then used the format cells option to change them to text.
Then added an apostrophe to the second text value '478901' as denoted by the green triangle, top left-hand corner of the cell.
Vlookup in C2 does not recognize A2 as text, even though it has been formatted as such.
Vlookup in C3 does recognize A3 as text and provides the returned value in column 2.
In the macro, the output pretty much says it all.
myData range is confirmed by address call.
VarType calls for values in A2 & A3 confirms excel is treating them as Double-precision floating-point number (5) and String (8), even though both have been formatted to text, although I suspect this is just for display purposes now.
The Application.Vlookup calls for the values in A2 and A3 return same as spreadsheet Vlookup calls.
In my earlier post, I said that both vartypes returned string (8) but that only the one with the apostrophe worked. This was with the work version of excel, which is earlier than this one. So it looks like someone tried to fix this issue or it just ended up different without attention. Either way, something is broken. "444" doesn't equal "'444" except in excel and "444" equals "444" everywhere else but in excel.
Looks like I'll need to test for both.

You've formatted the worksheet cells as "Text", but if you do that after the value has been entered it does not "take": cell contents will be by default left-aligned, but the values are still numeric for the purposes of formulas etc.
Your value with the ' prefix works in the lookup because it's actually text: if you re-enter the values in the "text format" cells (no need to add the apostrophe) then those will also become non-numeric and will be matched by the vlookup.
Eg see below - first 4 rows are "General" format for "Value"; next 4 rows are formatted as "Text" (after the values had been entered).
501 and 506 are both not recognized as numbers because they have a ' prefix.
504 was re-entered after the Text cell format was applied, and so is seen as non-numeric.
505 and 507 are both still seen by Excel as numbers.
Related: https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-mso_winother-mso_2010/a-number-formatted-as-text-is-really-a-number/085bb189-342b-4eed-bc33-2f149c6db244

Related

Convert text string into a formula in Excel

I have in a cell two numbers "=5+4" as a text. This is a result of another operation.
I took a part of another formula and concatenated it with "equal" symbol:
= "=" & RIGHT(FORMULATEXT(V8);LEN(FORMULATEXT(V8))-SEARCH("+"; FORMULATEXT(V8)))
I want to get the result of 5+4 in a cell- which means "9" ;)
I DO NOT WANT TO USE VBA code.
my initial problem was to extract all numbers except the first one from an equation and sum in another cell: A1: "=6+5+4". A2: "9". Maybe it can be solved without VBA?
You could try (assuming only addition):
Formula in B1:
=SUM(FILTERXML("<t><s>"&SUBSTITUTE(MID(A1,2,LEN(A1)),"+","</s><s>")&"</s></t>","//s[position()>1]"))
In fact, if you don't want to use the 1st number we could also discard taking '=' into account:
=SUM(FILTERXML("<t><s>"&SUBSTITUTE(A1,"+","</s><s>")&"</s></t>","//s[position()>1]"))
And since SUM() will ignore text in the final answer, we can now even further simplify this (thanks #JosWoolley):
=SUM(FILTERXML("<t><s>"&SUBSTITUTE(A1,"+","</s><s>")&"</s></t>","//s"))
If you are always adding 2 numbers, then you can use this:
=VALUE(VALUE(MID(A1;2;SEARCH("+";A1)-2))+VALUE(MID(A1;SEARCH("+";A1)+1;9999)))
My column A is formatted as text, and all values follow same pattern: =Value1+Value2
So the formula extracts Value1 and Value2 as text with MID functions, based on the position of + symbol. Then Excel convert both values into numeric with VALUE function. Notice there are 3 values, the third one is to make sure the cell stays at standar format (in some versions of Excel, like mine 2007, when involving text formatted cells into formulas, the formulated cell autoformat itself to text format, making the formula to work just once).
As you can see in the image, it works perfectly but this is just for pattern =Value1+Value2. For different patters you'll need different formulas.
If you want to make a formula to work with all patterns, then indeed you need VBA.

How to get a numeric sum for a "character number" within a string in Excel

I have 4 character cells that contain a "number" in positions 1 and 4, and a character in positions 2 and 3. I want to get a numeric sum of the numbers for positions 1 and 4 for the range of cells.
My cells look like this:
I have tried SUM(MID(VALUE(B4:B21,1,1)) which gives me an invalid type error
Also tried SUM(MID(B4:B21,1,1)*1) which works with a single cell but not for a range.
=SUM(IFERROR(1*MID(B4:B21,{1,4},1),0))
OR
=SUMPRODUCT(IFERROR(1*MID(B4:B21,{1,4},1),0))
If you have Excel O365 with dynamic arrays, then the first formula will work OK.
If you have an earlier version, you may need to either confirm the first formula as an array formula with ctrl+shift+enter, or use the second formula entered normally.
The IFERROR takes care of any values in the range that do not have the pattern of [0-9][A-Z][A-Z][0-9] If you would prefer to detect that by returning an error, just remove the IFERROR part of the formula:
=SUM(1*MID(B4:B21,{1,4},1))
SUMPRODUCT of LEFT and RIGHT
=SUMPRODUCT(VALUE(LEFT(B4:B21,1)))+SUMPRODUCT(VALUE(RIGHT(B4:B21,1)))
With error handling included, but possibly (Excel 2019) confirming with CTRL+SHIFT+ENTER:
=SUMPRODUCT(IFERROR(VALUE(LEFT(B4:B21,1)),0))+SUMPRODUCT(IFERROR(VALUE(RIGHT(B4:B21,1)),))

Excel SUM function is not working (shows 0), but using Addition (+) works

I'm stumped in Excel (version 16.0, Office 365). I have some cells that are formatted as Number, all with values > 0, but when I use the standard SUM() on them, it always shows a result of 0.0 instead of the correct sum. When I use + instead, the sum shows correctly.
For example:
SUM(A1:A2) shows 0.0
A1 + A2 shows 43.2
I don't see any errors or little arrows on any of the cells.
Excel is telling you (in an obscure fashion) that the values in A1 and A2 are Text.
The SUM() function ignores text values and returns zero. A direct addition formula converts each value from text to number before adding them up.
Using NUMBERVALUE() on each cell fixed it. Even though each cell was formatted as a Number, since the data was originally extracted from text, the cell contents apparently were NOT being treated as a Number. Yet another flaw in Excel.
I get a similar issue while importing from a csv.
Selecting the cell range and formatting as number did not help
Selected the cell range then under:
Data -> Data Tools -> Text to Columns -> next -> next -> finish
did the job and numbers are now turned into numbers that excel consider as numbers !
This avoids use of NUMBERVALUE()
There is a much faster way you just need to replace all the commas by points.
Do control-F, go to "Replace" tab, in "Find what" put "," and in "Replace with" field put "."
I noticed that if the sum contains formulas if you have any issue with circular references they won't work, go to Formulas-->Error checking-->Circular References and fix them
If there's a tab in your .csv then Excel will interpret it as text rather than as white space. A number next to a tab will then be seen as text and not as a number. Convert your tabs to spaces with a text editor before letting Excel at it.

Excel formatting for making values equal

I am working in Excel and I am wanting to find out if there are matches in one sheet based on a list in the other sheet.
I have pasted the values in a list and want to return their corresponding value from another sheet. The cells that contain letters and numbers work correctly (example: D5765000), but for some reason if the cell has only numbers, Excel is not able to find it in the other sheet, even though it exists.
I pasted a value 745‐3000 in the list and I am looking for this corresponding value in another sheet. It shows as #N/A on the lookup indicating it doesn't exist in the other sheet. However, if I delete the 745‐3000 and manually type 745-3000, then Excel somehow does recognize the value and find it in the other sheet.
The formatting is exactly the same in both and there are no spaces in either list. I cannot understand why Excel won't recognize the pasted value of 745-3000. Any ideas?
Excel is reading the "-" with a different code than it is with a manually typed "-"
Isolate the "-" and use to find and replace. Does some data contain multiple offending "-"'s?
=REPLACE(H4,FIND(G4,H4),1,"-")
EDIT: Switched from code to Unicode
TY #ron
We find the offending char using the set up pictured below:
Some of the formulas used:
Yellow Cells:
=UNICODE(MID($A$3,C4,1))
Light Blue Cells:
=UNICODE(MID($E$3,C4,1))
At the bottom we check to see what code we get from the value. I get a VALUE mark with from the unicode value of "-"

moving or deleting cells caused an invalid cell reference Excel

Today I have a problem with my excel spreadsheet. I am getting cells displaying #REF! error on the result cell! I checked the formula and there is nothing with it at all.I need to select a calculation from text box and a value from a second text box to give me a value from a table based on my formula! My formula is as follows:
=IF(F55="Select Size","",VLOOKUP(F55,INDRECT(CONCATENATE($F$51,"s")),3,FALSE))
Any help would be much appreciated!
The second parameter in VLOOKUP is a table_array, i.e. a 2-dimensional area in in your spreadsheet defined by the top left corner and bottom right corner, so in your spreadsheet it could be something like $G$1:$I$10.
So for your formula to work using INDIRECT and CONCATENATE, $F$51 would have to contain the first part of a range e.g. the text "$G$1". Then you would need to concatenate this with the second part of the range e.g. ":$I$10" to get a valid range like "$G$1:$I10" as a string which INDIRECT would return as a valid reference like $G$1:$I$10.
But in the formula that you've currently got, whatever is in $F$51 is concatenated with the character "s". I don't know what's in $F$51, but it could only return a valid reference if it's something like "Q:" and you're specifying three whole columns - anything else would give a "#REF!" error.
To see what's happening, go to Formulas | Evaluate Formula.

Resources