How to remove hidden quotes - vba

I've Excel sheet that has few text columns. These text columns are Email Messages. The data from this sheet will be used to send mails.
There data looks fine in Excel but when the message is copied to the Email body quotes are appearing in the beginning and end of the message.
I researched online and found out that these are unwanted characters. I tried removing the " using following formula.
=SUBSTITUTE(SUBSTITUTE(CLEAN(K1),CHAR(127),""),CHAR(160),"")
However the problem is that there are multiple columns with this problem so this method is not very feasible option for me. Also another problem is that after this the cell loses the formatting.
Please help me resolve this, I'm looking for a Find and Replace method if possible. Worst case scenario would be a macro.
Thanks in advance.

Cells.Replace What:=Chr(127), Replacement:=vbNullString
Cells.Replace What:=Chr(160), Replacement:=vbNullString

Your cells in your excel sheet contains multiple lines of data within a data, which means all lines in the cell are entered with carriage return. (Enter Key)
If you copy and paste such cells to a txt file, you will get the text within a " ". The " " are not actually quotes, but text with carriage return.
Just use the formula and let me know if it works,
=SUBSTITUTE(A1,CHAR(10)," ")

Related

Data imported to Excel as "general", text started with "=", getting "#name?" Is there a way to get it back to text?

On of our offices started their text data with an equals sign, which, when imported into the spreadsheet, gave the #NAME? error.
Now, I know I can change the cell format to text and then click in the formula bar, and excel recognizes the cell as text, but is there a way to do this through VBA?
EXAMPLE:
Cell display value: #NAME?
cell text =-Notice, ABC
Desired OUTPUT: Notice, ABC
now, normally, I'd do something like
sub convert_it()
if left(cells(1,1).value,2) ="=-" then cells(1,1).value = _
right(cells(1,1).value, len(cells(1,1).value) -2)
end sub
but that returns a type mismatch error.
Is there some way I can get the text in the cell without manually clicking?
I can click a few thousand times and then Excel recognizes it as text again, and not as an error, but I'd rather avoid that.
You can do this with VB, or just highlight the whole sheet, hit CTRL-H, type = in the Find and '= in the Replace, then click Replace All. This will make Excel stop thinking that these are formulas.
Instead of using .Value you have to use .Formula, since it's being interpreted as such here:
sub convert_it()
if left(cells(1,1).formula,2) ="=-" then cells(1,1).formula = _
right(cells(1,1).formula, len(cells(1,1).formula) -2)
end sub
Naturally you would place your code within a loop for the column. But I agree that it might be easier to just do a large find/replace.

Preventing excel from altering cell contents to take literal values

I have scripted out a module that reads data I paste in from a SQL dump, and converts it into a data insert script to SQL. It is working great, only problem is for cells that contain items like:
11-20
instead of filling my value as '11-20', it is converting it to '20-Nov'.
I have adjusted how i read the cell from text to value to value2, Text comes the closest to right (rest do a math calc that tosses my overall sheet off even worse, namely dates). I have also tried such things as a Range("X:X").clear and clearformat as well. This also does not do the trick.
How do I force my string read of this cell to be the literal CSV content, and ignore the formula/calculations that excel is tossing at me?
EDIT:
Thanks to BraX for the solution!
I was unable to accomplish this by copying from one tab to the next within Excel, but i did get it to work by pausing my operation with a message box prompting the user to simply navigate to SQL and put the contents of the data in to the clip board. This works perfectly now!
Cells.Select
Selection.NumberFormat = "#"
MsgBox "Please navigate to SQL and copy your data to be insurted, including headers. When done click OK"
Range("A1").Select
ActiveSheet.Paste
Before adding the data to the sheet, set the NumberFormat to # for the affected columns.
Example:
ActiveWorkBook.WorkSheets("Sheet1").Range("A:A").NumberFormat = "#"
That will format the values to Text and prevent them from being interpreted as a Date when the data is added. Once Excel decides it's a date, it's too late.
i am pretty sure all it takes is to format the cells, right click on the range of cells you want formatted as plain text, click "FORMAT CELLS" and under the "NUMBER TAB" it shows the "CATEGORY" of the type of formatting, e.g: "General, Number, Currency, etc" select where it says "Text" and click ok, any text will be evaluated the same way you write it, so even if you write "11-5-18" it will be just that and won't be considered a date or anything.

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.

Excel VBA; Search rows grabbing values and pasting them into another worksheet

I have two workbooks;
(WB1) with two sheets; "Input" and "Output"
and
(MacroWB) with the macro and a "Column Header" list.
Example file: "Messy" sheet = input, "Organized" = output
https://drive.google.com/file/d/0B-leh2Ii2uh9bDBFbDBHbGcxbUU/view?usp=sharing
I need help coding a macro to do the following:
1) Create a loop to go through each row of the "Input" sheet searching for values matching cells in the "Column Header" list.
2) When a matching value is found; take the data from the cell immediately to it's right (in the "Input" sheet) and paste it into the corresponding column of the "Output" worksheet.
3) Once every "Column Header" item has been searched/pasted for that row; move to the next row of the "Input" sheet. Rinse and repeat until all rows of the "Input" sheet have been searched/pasted.
Here is an example, the letters are to be column headers and the numbers are to be copied to the appropriate "Output" sheet column.
https://drive.google.com/file/d/0B-leh2Ii2uh9TXRGTnFDRU1jY0U/view?usp=sharing
Keep in mind that the actual data file has ~50 columns and ~3000 rows.
Also that the data is not all Letter/Numbers like the table above, it is more like the data in the linked .xlsx file.
If there is anything I haven't been clear about, please ask and I will try my best to clarify. Also I may be WAY over thinking this, if so.. please let me know.
THANK YOU ANYONE THAT CAN GET ME GOING IN THE CORRECT DIRECTION!!!
-Joe
Skip the the VBA and use Text to Columns the Data tab. I'malways copying html and its works 99% of the time. If the html is pretty and properly formated you may get away with using the fixed width option, otherwise gor for the delimted and choose "tab". If tab doesn't work try using spaces, assuming that your cells don't contain spaces.
The other option that I've had work on rare occasions that text to columns doesn't is simply saving the text in word and saving as rtf and then opening that in notepad++ (which everyone should have.) Copy from ++ to excel and that usually fixes the problem.
EDIT: If you right click before pasting and click "paste special" this regularly helps with html pasting.
In your sample file, I used the following formula in A2 of Organized sheet (assumed 50 as max columns in Messy):
=IFERROR(OFFSET(Messy!A1,0,MATCH(Organized!A$1,OFFSET(Messy!A1,0,0,1,50), )),"")
Dragging it to H11 produced the following result:
The sample data is not complete, and some 'tags' in Messy sheet are not consistent (SiteID vs SITE_ID), but it should help you get started.