Excel Cell Text Seems to Change when Double Clicking Cell - excel

The problem I am having here or hope to understand is that the characters in a cell seem to change from a programmatic perspective between macro produced values then double clicking the cell (probably explaining this horribly). To explain further, I have a macro that pulls variable paragraph sized strings and puts them in each cell within a given range. The next thing I try do is to use another macro on the newly produced range to remove extra newlines (2+ consecutive newlines). For whatever reason, the macro fails and seems to not be interpreting the newlines. Once I manually doubleclick inside a cell though to where the text cursor appears then leave that cell to run the newline macro, it is then able to detect the newlines.
What's going on here? Do the characters have different uni-code values or something prior to double-clicking the cell? I have similar problems when trying to analyze the characters of a cell for other words as well only when the cell text was generated from a macro.

I suspect some of what you call newlines are carriage return characters ASCII 13. When you enter a carriage return character in a cell manually excel converts it into a new line character (ASCII 10). After reading your question, I verified this by entering the formula "=CHAR(13)" into cell A1 and then copied and pasted its value in place. I entered the formula "=CODE(A1)" in cell A2 to evaluate the ASCII code of the character in A1 (13 at this point). I then double clicked into A1 and pressed Enter and (guess what?) the character was changed into a new line character (ASCII 10). I suspected the same would happen with CHAR(11), but that was not the case.
To solve your problem I suggest you first convert all occurances of Chr(13) to Chr(10) using something like
MyString = Replace(MyString, Chr(13), Chr(10))
' and if it were me, I would also add (just in case)
MyString = Replace(MyString, Chr(11), Chr(10))
In my experience some programs (e.g. Access reports if I am not mistaken) need a sequence of Chr(10) & Chr(13) (or the other way around) to physically display a new line in reports and therefore, if you pull a paragraph that has what appears to be 2 new line characters, then the actual string to achieve this would be Chr(10) & Chr(13) & Chr(10) & Chr(13) and in excel you end up with 4 new line characters. Hope this helps.
Edit:
In case this persists, a way I would use to debug the situation is by printing all chars with their values and read the character code at the place that's causing the issue with something like this (code not tested):
Dim s As String
Dim i as Integer
For i = 1 to Len(MyString)
s = Mid$(MyString, i, 1)
Debug.Print s & ", " & Asc(s) 'Or perhaps AscW(s) for unicode
Next i

Related

use SPLIT function to seperate a list of numbers from a userform

I have a userform that has a list of numbers entered in column style with no separator other than being on a separate line. Entry into the user form will look something like this:
2172223333
2172223334
2172223335
2172223336
2172223337
How do I use the SPLIT function to separate this bulk entry into the user form into separate cells in the same column on a spreadsheet? I'm familiar with how it works if there is a comma or space to look for between the terms but will it somehow recognize a separate line as a delineator and if so, what symbol do I use to represent that in the code?
I want to be able to read this input and have it entered in a workbook, say ("workbookA") and sheets(1) and put it in Column A starting at row 2 and going downward staying in column A.
Try this:
myString = ...
data = split(myString, vbcrlf)
workbooks("workbookA").worksheets(1).range("A2").resize(ubound(data)+1,1).value = application.transpose(data)
myString is the String with your data.
In VBA you have the constants vbLf, vbCr, vbCrLf for linefeed, carriage return and linefeed + carriage return (https://en.wikipedia.org/wiki/Newline), which represent the characters.

excel function to combine cells into single line of text?

I'm new to stack overflow so I apologize if this is a horrendously stupid question. I am wondering if there is a function or way to code a function in excel that will combine a column of cells with plain text and convert them into one cell with the text on a single line? Specifically I want to convert a column of random numbers into a single line of text and insert SPACE+AND+SPACE between them.
Ex.
15133484
12345188
12345888
to
15133484 AND 12345188 AND 12345888
Currently I am copying and pasting all this information into google and then into Word and using find/replace and it is taking forever everytime. If it is possible to just get Excel to do this for me that would be amazing.
Thanks!
If you have Office 365 Excel use TEXTJOIN():
=TEXTJOIN(" AND ",TRUE,A:A)
otherwise one would have to use:
=A1 & " AND " & A2 & " AND " & A3
Or one can use a helper column, B1 put:
=A1
put this in B2 and copy down:
=IF(A2<>"",B1 & " AND " & A2,B1)
And grab the last cell in column B.
A little late, but still:
Reference here
Step 1:
=concatenate(transpose(rngBeg:rngEnd & " AND "))
Step 2:
highlight the transpose statement and then press F9, which substitutes the actual values for the formula.
Step 3:
Remove the curly braces, { }, from the formula. The cell will display the range of reference cells combined with whatever separator chosen after the ampersand sign.
Not a "live" formula, but still far easier than manually concatenating a range of values.
Press ALT+F11 to open Microsoft Visual Basic for Applications,
Insert-> Module
Paste this:
Function Combine(WorkRng As Range, Optional Sign As String = " AND ") As String
Dim Rng As Range
Dim OutStr As String
For Each Rng In WorkRng
If Rng.Text <> "," Then
OutStr = OutStr & Rng.Text & Sign
End If
Next
Combine = Left(OutStr, Len(OutStr) - 5)
End Function
In any cell type =Combine(Range)
i.e.
=Combine(A1:A500)
use concat function if you can add an additional column in the excel like this:
=CONCAT(D3:E5)
Attached sample image with input, additional column, output and formula
I assume you want to merge the data in the 3 cells into a single cell with a space between the 3 data set.
If that is the case then you can do it simply by using the Concatenate function in excel.
In the above example, you have data in Cells A1, A2 & A3.
Cell C1 has the merged data. As you can see, we have used CONCATENATE Function.
The space has been defined in Double quotes. So if you need a Hyphen (-), you can put that in Double Quotes with space “ - ” and it will display the result with Sanjay - Singh - Question
Hope this helps.

Find/Replace not finding cell value after formatting

There is a column of values that are moved from one Excel spreadsheet to another, in the same workbook, by a macro. The values should be five characters, only numbers, including leading zeros.
There are several ways that have successfully replaced the lost leading zeros from the auto formatting that Excel does. With a strange result.
For every cell that the macro has formatted the cells, the Find/Replace tool refuses to recognize any searches that include zeros.
Example:
Before Macro = 9093
After Macro = 09093
The Find/Replace window will find a search value of 9093 but will not find a search value of 09093. A Find/Replace window will find a positive hit after deleting the macro formatted 09093 and hand keying 09093 into the cell.
I have not tried code checking each value for the desired number of characters then concatenating leading zeros until the right number of characters has been reached. My hesitation stems from my assumption that a macro running this code will run very slow when having to go through 1000 or so rows.
Code blocks below are two attempts:
''Masks for a five character sequence.
' Corrects for leading zeros being dropped for Product Code column.
' Currently does not work.
Columns("F:F").Select
Selection.NumberFormat = "00000"
''Alternative method for keeping correct format of Product Code
' (with leading zeros) and searchable with Find window.
' Also not functioning.
Dim a
Dim l As Long
With Range("F2", "F" & lastUsedRow)
.NumberFormat = "#"
a = .Value
For l = 1 To UBound(a, 1)
a(l, 1) = Right("0000" & a(l, 1), 6)
Next l
.Value = a
End With
The actual value of the cell you are trying to find is 9093, even though it is shown as 09093 through formatting. The find/replace tool will look for a string value of 09093 while the actual value is 9093 and thus cannot find it. Presumably when you key in the 09093 it is formatted as text rather than a number to preserve the leading 0s.
If you don't actually use the numbers in the newly created column for analysis, might I suggest the line below. This way you can find the cell with the leading 0's from the Find/Replace dialog as the entire product number including the leading 0's are a string.
Selection.NumberFormat = "#" 'This formats the selected cell as text

Squeeze a CONCATENATE up if field is empty, so space gap isn't so large

I have a problem where I/we have created a CONCATENATE from various field in Excel (see below) and all have text currently which is fine and we have applied single spaces between each join.
Problem I have is if a field is blank my single space becomes a double triple space and doesn't look very tidy in a standard text string.
Is there a way to squeeze this up if a field/cell is empty?
=CONCATENATE(M30," ",M31," ",M32," ",M33," ",M34," ",M35," ",M36," ",M37," ",M38," ",M39," ",M40," ",M44)
Wrap your CONCATENATE in =TRIM.
Write a macro function and use it in the sheet.
Function NoSpaceConcat(oRngJoin As Range) As String
Dim oRng As Range, sTxt As String
sTxt = ""
For Each oRng In oRngJoin
If Not IsEmpty(oRng) Then sTxt = sTxt & oRng.Value & " "
Next
NoSpaceConcat = Trim(sTxt)
End Function
In the worksheet, put in formula =NoSpaceConcat( then highlight the Range you want to join and press Enter. Much neater than any built in formula I would say.
I have an answer, but it isn't pretty. You can replace the blank splaces with if and isblank clauses. If the the cell is empty then the don't do a blank space otherwise do.
Your formular would then be
=CONCATENATE(M30,IF(ISBLANK(M30),""," "),M31,IF(ISBLANK(M31),""," "),M32,IF(ISBLANK(M32),""," "),M33,IF(ISBLANK(M33),""," "),M34,IF(ISBLANK(M34),""," "),M35,IF(ISBLANK(M35),""," "),M36,IF(ISBLANK(M36),""," "),M37,IF(ISBLANK(M37),""," "),M38,IF(ISBLANK(M80),""," "),M39,IF(ISBLANK(M39),""," "),M40,IF(ISBLANK(M40),""," "),M44)

How to get Excel to ignore apostrophe in beginning of cell

I'm writing a tool that syncs a simple database with Excel sheets. Each item in a table in the database corresponds to one row in the worksheet. I read the Excel sheet into the tool using C# and the Excel interop com interface, then compared the items' values (i.e. one of the columns in the excel sheet) after the sync just to make sure that they are equal.
Yesterday I found a case where the comparison wasn't true:
"'<MedalTitle>' Medal - <MedalDescription>"
"<MedalTitle>' Medal - <MedalDescription>"
The second is the one I've read in from Excel, and as you can see it's skipped the first apostrophe. Is there a way to tell Excel to treat the cell as just text (no, just setting the cell's formatting doesn't help)?
I even tried to copy the value ( 'hello' ) of a cell in VBA like this:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Target.Offset(1, 0).Value = Target.Worksheet.Range("b2").Value
Target.Offset(2, 0).Value = Target.Worksheet.Range("b2").Formula
Target.Offset(3, 0).Formula = Target.Worksheet.Range("b2").Formula
Target.Offset(4, 0).Formula = Target.Worksheet.Range("b2").Value
End Sub
The result was that the value of target cell is always hello'
If there is no way, I'll have to do something ugly like
if (dbitem.value[0] == ''' )
{
// stuff
}
else
{
// regular comparison
}
I'm afraid the apostrophe ' is a special character for Excel when it appears as the first character in a cell as you've found. It tells Excel to treat the rest of the string as text, so that you can enter something like '34.2 in the cell, and it'll treat it as the string instead of the number (for formatting and so on).
I suggest doing something similar to what you've suggested, except that where you're putting it into Excel, check the first character, and add an extra ' if there's one there already.
Alternatively, you could prepend an apostrophe to all values - if you want them all as text that is. That way you don't need the extra first character check.
Look at the PrefixCharacter property of the Range object which corresponds to that cell
From the help:
If the TransitionNavigKeys property is
False, this prefix character will be '
for a text label, or blank. If the
TransitionNavigKeys property is True,
this character will be ' for a
left-justified label, " for a
right-justified label, ^ for a
centered label, \ for a repeated
label, or blank.
The TransitionNavigKeys part relates to Lotus 1-2-3 compatibility so it's more than likely going to be False
Answer based on article at:
http://excel.tips.net/Pages/T003332_Searching_for_Leading_Apostrophes.html
(warning: slightly annoying pop-up may appear)
edit: actually this probably isn't going to be any use because PrefixCharacter is read-only :(
edit2: I was right the first time. PrefixCharacter only gets populated if the value added to the cell started with ' so just read back PrefixCharacter plus Value and concatenate. As long as TransitionNavigKeys is False, that is
try targetcell.Value instead. .Formula is the formula seen in the formula bar while .Value is the evaluated value of the cell.
So, I am guessing that you would have used .Formula in your original code as well. Changing that should work.
EDIT: Ok, it did not work (embarrassed).
Excel treats the starting single quote specially.. so specially that even obscure cell / range properties do not have access. The only workaround I could find is essentially the same as what you thought initially. Here goes:
If VarType(cell) = 8 And Not cell.HasFormula Then
GetFormulaI = "'" & cell.Formula
Else
GetFormulaI = cell.Formula
End If
You might try pre-pending a single quote to your text fields ( '''' + dbField ) in your query so that for fields with embedded single quotes your query would return:
"''stuff in single quotes'"
which when placed in an Excel cell would convert to:
"'stuff in single quotes'"
for characters that weren't in quotes you would get:
"'stuff that wasn't in quotes"
which when placed in an Excel cell would convert to:
"stuff that wasn't in quotes"
Worth a shot. :-)

Resources