Excel VBA Replace Character - vba

I tried looking around for this particular problem, but couldn't find it.
I am trying to replace an assortment of characters in a string.
I tried looping through the characters using their character codes, but my code will ?randomly? delete the entire string instead of just the special characters. (basically I just want the letters and numbers from the string) EG. ABC-3.3%H14T-6 I would just want ABC33H14T6
What is strange about this (to me) is that my code will delete the string on a random character, it's not always chr(41) or whatever, it changes each time I run the code.. is it maybe a formatting issue?
For Char = 33 To 47
Sheets("Sheet1").Cells(FRow, 44).Replace What:=Chr(Char), Replacement:="", LookAt:=xlPart
Next Char
I would give you the bulk of my code, but it is work-sensitive.
Any advice would be appreciated!
Thanks guys.

I'm dumb Chr(42) = *
which would replace everything. durr

Related

Using Find and Replace function together in VBA

I have a problem with an .CSV file.
Some of the values are prices with a comma to separate units and decimals, all the other fields are separated with a comma too.
So, as expected, it is impossible to convert my csv file like this. (If there is a way, please tell me)
Therefore, I am trying to write a vba macro that will replace the comma by a dot.
More specifically, I need to replace the 9th occurence of "," to a "." IF AND ONLY IF the character next to the comma respects a specific condition. That is why I need a macro to do so.
In excel, I was using the following formula to find the position of my comma:
=Find(char(160),substitute(A2;",";char(160),9))
This formula gives me the position of the 9th comma, that's perfect. I would like to know how to code this in VBA
Thanks in advance !
Alex

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

VBA throws error 1004 when adding a Hyperlink

I have a Macro that creates a Mailto Link with email addresses input in one cell, and the body of the email made up of 3 different cells, the mailto Strin in the code snippet. This link should then be placed into a certain cell.
With Sheets("Menu")
.Hyperlinks.Add _
anchor:=.Cells(row, 8), _
Address:=mailTo, _
TextToDisplay:="mailTo", _
ScreenTip:="MailTo link"
End With
However I sometimes get an
error 1004
in the above code. Removing parts of the email body resolves the issue so I originally thought that it might be related to the length of the mailTo String. However this does not seem to be the case since I've gotten it to work with different length strings.
What other possible reasons are there that could result in the Hyperlinks.Add function failing?
There could be any of several reasons. For example, Row may be 0:
There is a maximum worksheet hyperlink limit of 65536. You could be running up against this*.
If so, an alternative is to use Excel's HYPERLINK formala.
*(see https://support.office.com/en-us/article/Excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3?CorrelationId=6fdacf7e-c207-4932-a46d-b82e7fa0679b&ui=en-US&rs=en-US&ad=US&ocmsassetID=HP010342495)
It seems that there is a limit to how long the hyperlink can be, 2012 characters as returned by the len function.
The reason I didn't originally discover this was because I was only looking at the mail text before converting to a mailto.
The conversion replaces new lines and spaces with %0A and %20 respectively. So "Hello World" is 11 characters long but in the Mailto it would look like this "Hello%20World" and have a length of 13. Since I was looking at the Text before the conversion and adding/removing spaces it seemed to me like mailtos of different length where working.
Thank you for everyone that offered suggestions even though they weren't what I was looking for. I hope that this can be of help some one else in the future.

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.

Replace a 'bel' character in a file in vb.NET

I've come across a character in one of my data feeds, which I have never encountered before
The images above are the data feed in Notepad++ and Notepad view. As you can see it appears as 'BEL' in Notepad++ and a sort of 'bullet point' in Notepad.
How would I go about replacing this character in vb.NET?
I've tried a simple replace in a SSIS Script Task by copying and pasting the character into the replace function, e.g.
text = text.Replace("copy and pasted character", "")
and this gives this error
All help is extremely appreciated,
Thanks
I’ve got no idea what SSIS is but since you wanted to know a solution in VB.NET, the code you’ve tried will work here. That is:
text = text.Replace("copy and pasted character", "")
will work just fine in VB. Alternatively, you can use the following:
text = text.Replace(Chr(7).ToString(), "")
Find out what the Ascii value of the character is and then use the Chr function to eliminate it
i.e.
text = text.Replace(Chr(n), "")
[Bell] is probably character 7