VBA throws error 1004 when adding a Hyperlink - vba

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.

Related

How to remove hidden quotes

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)," ")

Working with Unicode in VBA StrConv

I have an Excel file and a userform where user can type in students' details and the form will check for duplication and then add the info onto the last line of a table. I want to improve it further by making the form capitalize the first letter of each name using this code:
Me.Surname.Value = StrConv(Me.Surname.Value, vbProperCase)
Me.Surname.Value is the input of the form, mostly Vietnamese such as Trần, Nguyễn, Thảo, etc. However, they are changed into something like Tr?n, Nguy?n, Th?o after going through StrConv. I read some suggestions and change my locale to Vietnamese but the problem persists.
Do you have any suggestion to fix this? I am thinking of converting the inputs into hex value and then write them down using ChrW(), but I can't find a way to do that.
I played a bit with this and the culprit seems to be StrConv.
It works for me if instead of using StrConv I explicitely set the proper case:
Surname.Text = UCase(Left(Surname.Text, 1)) & Mid(Surname.Text, 2)
or even more precise:
Surname.Text = UCase(Left(Surname.Text, 1)) & Mid(LCase(Surname.Text), 2)

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.

How do I add a subscript to a string?

I need to add a subscript (a little number next to a character, like 2 or 3 - note: not a power of...) to a string [variable]. Is this possible? I don't want the code to be lengthy as I will need to process a lot of formulas, one at a time. Thanks.
You will have to use the character set subscript numbers. Strings do not contain formatting.
http://www.fileformat.info/info/unicode/char/2082/index.htm
Is this possible? No - string variables do not contain formatting.
In order to achieve this you will need to add some sort of formatting to your string and display it in something that can show different formats - for example a RichTextBox control
Try adding a RichTextBox control and running the following line:
RichTextBox1.Rtf = "{\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fswiss\fprq2\fcharset0 Calibri;}{\f1\fnil\fcharset0 Segoe UI;}}\viewkind4\uc1\pard\ltrpar\sa200\sl276\slmult1\f0\fs22 sometext\fs12 subscript\fs22\par\pard\ltrpar\f1\fs17\par}"
I don't claim to know what all the formatting is in here so I will leave you to figure that out yourself - hope that helps...

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...