Remove paragraph mark when copying from excel cell - vba

Please help me copy a string from a listbox when a user hits ctrl+c. I was using the dataobject but for some reason this worked perfectly some times and gave me an error message other times. If you know why this is, stop reading, as the rest of this question is not necessary.
Now I am putting this in a worksheet cell and using range.copy, however, when the string is pasted into a textbox, it retains the paragraph mark that excel seems to put at the end of every cell! Just to make things fun, the paragraph mark cannot be removed by using Left() - it takes everything but the paragraph mark. (Paragraph mark below is represented by P).
s = "stringP"
s = Left(s,len(s)-1)
print s
returns: strinP
Has to be something simple I'm missing.

I haven't tested this but have you tried chopping two characters?
I'm sure it's \r\n or carriage-return + line feed, not just \n you need to chop.

Have you tried trim() function ?
And why do you have to use Range.copy?
Can't you just assign textbox1.value = Range("A1") ?
It works fine without any bugs.

Related

Checking if a Word table's cell is empty - Empty cell containing a "marker"

This Word file has a giant table with 2 columns. I have to make a statement like:
If theCellIsEmpty, then
Fill it
End if
The thing is that when I get the content of the cell through:
FirstCellText = ActiveDocument.Tables(Tbl).Cell(Rw, 1).Range.Text
It doesn't get the "" I'm expecting, but it contains a sort of dot, so the text read is never empty.
Here is the dot I'd like to test for:
Moreover, I have no idea what this dot can be or where it came from. I tried to find it in the characters table without success.
Could someone explain me how to test the presence of this "end of line character" or the emptiness of the row? I don't want to check if the cell only has 1 character as if this character is something put there with a purpose, I don't want to delete it.
Also, every single line has this dot at the end...
What is it? How can I use/avoid it?
All you should need is something like:
Dim Cll As Cell
For Each Cll In ActiveDocument.Tables(1)
With Cll.Range
'if all it has is the end-of-cell marker, it's empty, so fill it
If Len(.Text) = 2 Then .Text = "Filler Text"
End With
Next
Thanks for all your help, I might have a simple solution thanks to Freeman, mostly :-)
I took the ascii number of the character to check what it is (through asc(xxxx) function) and it happens to be a "carriage return" having the ascii number "13"
Therefore, so I just need check if the content of the cell gives the ascii number 13 and it's done. (the "asc(xxxxxx)" gives the ascii number of only the first character of the text chain :) )
Of course, if the cell starts with a carriage return and there is text after, it won't be nice, so checking also if the text contained in the cell is only 2 char long will be a nice thing (this "carriage return" is apparently 2 char long... strange...) :)
If Asc(FirstCellText) = 13 and len(FirstCellText) = 2 Then

Modifying string in cell without getting rid of formatting (underlines)

I'm currently parsing out words in an excel file using VBA, and one of the ways I've decided to split up the words is by using their existing underlines (some are already underlined, some aren't). The line that's giving me trouble is
Rng.Value2 = Replace(Rng.Value2, Left(Rng.Value2, I - 1), "")
Where Rng is referring to one cell and I is a counter in a for loop. This line of code turns this
Euthyatira pudens Dogwood Thyatirid Habrosyne scripta Lettered Habrosyne Habrosyne gloriosa Glorious Habrosyne
into this
Habrosyne scripta Lettered Habrosyne Habrosyne gloriosa Glorious Habrosyne
Except instead of bolded words, they're underlined (this text editor can't do underlines). Any idea why this is happening and how to keep the underlines?
Edit: I can make it work in a long, roundabout way (stepping through the string one character at a time and comparing it to the original string to see if that character should be re-underlined, but this takes the computer a long time. Any faster ways that don't involve stepping though the string one character at a time?
Thanks

Is it possible to use ASCII code in .MoveEndUntil?

I am new in VBA for Word
I was wondering if it is possible to use ASCII code in .MoveEndUntil for example: .MoveEndUntil cset:=Chr(13) & "-", Count:=wdForward
Yes, it is totally possible to use sample code from your question. However, it depends what you are trying to achieve. Your code will move the end of the selection until any of the specified characters are found in the document (see the documentation of the Range.MoveEndUntil method).
That means that moving the end of the range will stop as soon as a dash or a carriage return is reached.
However, this is probably not what you want. It looks like you are trying to extend the range until the next list item? If this should be the case then you can't use a simple Range.MoveEndUntil. You would have to expand the range to the end of the current paragraph and check whether the following paragraph has a list formatting.

Excel file contains invalid hidden characters that can't be removed

I have a peculiar problem with hidden characters in an Excel spreadsheet which uses VBA to create a text file. I've attached a link to a test version of the file, and I'll explain as best I can the issue.
The file creates a plain txt file that can be used to feed data into a System we use. It works well normally, however we've been supplied approximately 15,000 rows of data, and at random points throughout there are hidden characters.
In the test file, there's 1 row and it's cell B11 that has hidden characters at the beginning and end of the value. If you put your cursor at the end of it, and press the backspace key, it will look as if nothing has happened, but actually you've just deleted one of the characters.
As far as Excel is concerned, those hidden characters are question marks, but they're not, as text stream would parse those, but it doesn't, and instead throws up an invalid procedure call error.
I've tried using Excel's CLEAN formula, I've tried the VBA equivalent, tried using 'Replace', but nothing seems to recognise those characters. Excel is convinced they're just question marks, even an ASCII character call gives me the same answer (63), but replace doesn't replace them as question marks, it just omits them!
Any help on this, even if it's just a formula I could apply would be appreciated. In the interests of data protection the data in the file is fake by the way, it's nobody's real NI number.
The excel file with vba code is here
This VBA macro could be run on its own or in conjunction with the ClearFormatting macro. It did strip out the rogue unichars from the sample.
Sub strip_Rogue_Unichars()
Dim uc As Long
With Cells(11, 1).CurrentRegion
For uc = 8000 To 8390
.Replace what:=ChrW(uc), replacement:=vbNullString, lookat:=xlPart
DoEvents
Next uc
End With
End Sub
There's probably a better way to do this and being able to restrict the scope of the Unicode characters to search and replace would obviously speed things up. Turning off .EnableEvents, .ScreenUpdating, etc would likewise help. I believe the calculation was already at manual. I intentionally left a DoEvents in the loop as my first run was several thousand different unichars.

Write a formula in an Excel Cell using VBA

I'm trying to use VBA to write a formula into a cell in Excel.
My problem is that when I use a semicolon (;) in my formula, I get an error:
Run-time error 1004
My macro is the following :
Sub Jours_ouvres()
Dim Feuille_Document As String
Feuille_Document = "DOCUMENT"
Application.Worksheets(Feuille_Document).Range("F2").Formula = "=SUM(D2;E2)"
End Sub
You can try using FormulaLocal property instead of Formula. Then the semicolon should work.
The correct character to use in this case is a full colon (:), not a semicolon (;).
The correct character (comma or colon) depends on the purpose.
Comma (,) will sum only the two cells in question.
Colon (:) will sum all the cells within the range with corners defined by those two cells.
Treb, Matthieu's problem was caused by using Excel in a non-English language. In many language versions ";" is the correct separator. Even functions are translated (SUM can be SOMMA, SUMME or whatever depending on what language you work in). Excel will generally understand these differences and if a French-created workbook is opened by a Brazilian they will normally not have any problem.
But VBA speaks only US English so for those of us working in one (or more) foreign langauges, this can be a headache.
You and CharlesB both gave answers that would have been OK for a US user but Mikko understod the REAL problem and gave the correct answer (which was also the correct one for me too - I'm a Brit working in Italy for a German-speaking company).
I don't know why, but if you use
(...)Formula = "=SUM(D2,E2)"
(',' instead of ';'), it works.
If you step through your sub in the VB script editor (F8), you can add Range("F2").Formula to the watch window and see what the formular looks like from a VB point of view. It seems that the formular shown in Excel itself is sometimes different from the formular that VB sees...