Hyperlinks in VBA - vba

I want to create a button in Excel which links to:
http://datafeed.api.productserve.com/datafeed/download/apikey/50f70c7de11f99fe127d7ad4c8e37e31/cid/97,98,142,144,
...
,567,569/fid/4319/columns/merchant_product_id,merchant_category,brand_name,product_name,mpn,search_price,aw_deep_link,specifications,valid_from,valid_to,in_stock,warranty,aw_product_id,merchant_image_url,description/format/csv/delimiter/,/compression/gzip/adultcontent/1/
I've cut out a large section in the middle, but it is just a long sequence of numbers separated by commas. In total the URL is 1939 characters long.
Copying the URL into a browser works fine - it is a download link and the file opens as it should.
The code for the button is simple:
Private Sub download_button_Click()
Dim feed_hyperlink As String
feed_hyperlink = *"http://data... "*
ActiveWorkbook.FollowHyperlink feed_hyperlink
End Sub
When I run the procedure, I get the following error:
Run-time error '5': Invalid procedure call or argument
Hyperlinking a cell restricts the destination URL to 255 characters. Is a character limit what's causing the issue here, or is there another problem?

I think you're right. It's probably too long as the longest one I can use before getting the same error is 1033 characters;
Sub Main()
Dim h As String
h = String(1034, "a")
Debug.Print Len(h)
ActiveWorkbook.FollowHyperlink h
End Sub

Related

Input box Compile Error

I have been trying to get the name of a file name when someone uses this Macro. But for some reason every time I run the Macro I get the error:
Compile error:
Wrong number of arguments or invalid property assignment
I have looked at tons of videos and other responses but none of them have helped me deal with this error.
Sub inputbox()
Dim x As Variant
x = inputbox("Please enter your file name:", "File name")
MsgBox ("Your file name is" & x)
End Sub
Here is your procedure, corrected: (Copy & paste into a new module.)
Option Explicit
Sub MyInputBoxTest()
Dim x As Variant
x = inputbox("Please enter your file name:", "File name")
MsgBox ("Your file name is " & x)
End Sub
Explanation:
Variant was misspelled. Note that since Variant is the default data type, you actually don't need to specify it. Dim x is the same as Dim x as Variant.
You can't use names like InputBox OR MsgBox as the name of your procedure. They are reserved words -- already used by another procedure built-in to VBA, so it confuses the compiler since it doesn't know which one you're referring to.
Added a space after the word is. (purely cosmetic)
Functions like MsgBox and InputBox can be called different ways depending on whether you use brackets and whether you need to return a value.
InputBox "Hi"
InputBox ("Hi")
...either of these will run fine (but won't return the what the user enters.)
Dim x
x = InputBox ("Hi")
...will return the value to variable x, however:
x = InputBox "Hi"
...will throw an error.
It's highly recommended that, especially while learning, you add the line Option Explicit to the very top of every module. This will help "force" you to properly declare and refer to variables, objects, etc, by generating compile errors when you try to compile (F9) or run (F5) the code.

Userform throws error when using "." or "-" or "backspace"

Hello my userform in Excel keeps throwing an error whenever I try to input anything that isnt a number i.e. "." or "-" or if I backspace my number to clear the row.
I believe it because I defined the variable that the userform will be assigned to as a long.
This prevents me from inputting negative numbers or decimals without throwing an error.
Any idea's as to how I can allow my userform to accept the Long variable type while still allowing me to enter other characters that aren't directly a number?
To be clear the error occurs while entering the number into the UF not after I action the Userform.
EDIT:
Code for this is quite simple
Public LTB As Long
Public Sub LotTextBox_Change()
LTB = LotTextBox.Value
End Sub
Error Msg is - Run time error 13 - Mismatch.
The problem that you are facing is because you are trying to store whatever is being typed in the textbox which is a String at runtime to a Long Variable.
So if you want to type -123 then the moment you type -, you will get an error as you are trying to store a String i.e - in a Long variable.
Try this
Public LTB As Long
Private Sub LotTextBox_Change()
On Error GoTo Whoa
LTB = Val(LotTextBox.Value)
Whoa:
'Debug.Print LTB
End Sub
What this does is, it tries to store the textbox value to the variable and if it is not of correct datatype, it exits gracefully.

Range accepts sometimes only semicolons instead of commas

I have reduced my problem to the following code example. I am using a German Excel version in which separators in normal Excel formulas are semicolons ";" instead of "," (e.g. =SUMME(A1;A3) instead of =SUM(A1,A3)).
Now the code which works different from time to time:
Sub CommasDoNotWorkAnymore()
Dim a()
Dim i%
a = Array("A1,A3,A5", "B1", "B2")
i = 0
Debug.Print Sheets(1).Range(a(i)).Address
End Sub
Normally, when starting Excel, this code works. But sometimes Excel seem to switch the accepted separators used in the Range() to semicolons untill I restart Excel. This occurs most times when rerunning the code after a runtime error.
Is this a general Excel bug? Does anybody know what is behind this behaviour? Is there some Excel-wide "local option" for the Range class?
EDIT: I just tried to convert the a(i) with CStr(a(i) but this does also not work. So no ByRef kind of problem...
If you want to control it, check first what separator is currently in use. What I guess is that you want to know the list separator:
Application.International(xlListSeparator)
Check other separators here:
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/application-international-property-excel
The other time I had a problem with identifying decimal separator in VBA. Finnally I was able to get it in this way:
Function GetVBAdecimalSep()
Dim a(0) As Variant
a(0) = 1 / 2
GetVBAdecimalSep = Mid(a(0), 2, 1)
End Function
Changing separator not always works. Please see this: Changing decimal separator in VBA (not only in Excel)
The best solution is to check/change locale, even temporary.
Application.LanguageSettings.LanguageID(msoLanguageIDUI)
gives the LCID which would be 1033 for English (US)

VBA check for ENTER character from clipboard

First of all I have little to no knowledge about VBA.. probably none at all. However I was asked to create a VBA program that paste text from clipboard in different cells. My text has the following format:
seminar: name of Seminar (in cell(1,1))
first name: participant's first name (in cell(1,2))
last name: participant's last name (in cell(1,3)) etc..
So far I was able to read the text from clipboard. Then I found the position of the ":" in order to paste only what is AFTER it in the cell.
At this point I thought to find the position of the RETURN character in order to know where the first line ends(ex. "name of Seminar") with this line of code which I found online:
end_str = InStr(str, vbCrLf) - 1
and with the Right (string, length) function to get the relative text.
This is not working. I think because there are not return character in the string variable that holds the data? I don't know.
My question is: Is it possible to check the RETURN character somehow or Is there a better way to create this program?
Thank you in advance.
An easy way would be to use the split function to get each line separately:
Suppose you have a function called ClipBoard_GetData that returns the text from ClipBoard, you could use something like this:
Dim lines() As String
lines = Split(ClipBoard_GetData, vbNewLine)
For Each Line In lines
' Parse each line to get whatever parts you want
Next
This should work fine.. and if you don't -already have a function that gets what's in the clipboard, you could refer to this link
Hope that helps :)
Most likely the Ascii code you're after is 10 (ie newline). So you could find the position of the newline like so:
i = Instr(str, Chr(10))
However, are you aware that you don't need to parse that clipboard text at all. You can write arrays directly into worksheet cells. So all you'd need to do is use the Split function. The procedure below will complete everything you need:
Public Sub PasteText(str As String)
Dim arr() As String
Dim cols As Integer
arr = Split(str, Chr(10))
cols = UBound(arr) + 1
Sheet1.Range("A1").Resize(, cols).Value = arr
End Sub

Compile Error Expected Function Or Variable

I'm new and trying to learn VBA. When I'm typing in the code I get Compile Error Expected Function Or Variable.
Is something regarding the activecell, but can't figure it out.
Sub Testare()
Dim FilmName As String
Dim FilmLenght As Integer
Dim FilmDescription As String
Range("b10").Select
FilmName = ActiveCell.Value
FilmLenght = ActiveCell.Offset(0, 2).Value
If FilmLenght < 100 Then
FilmDescription = "Interesant"
Else
FilmDescription = "Suficient"
End If
MsgBox FilmName & " is " & FilmDescription
End Sub
This error happens also when a Sub is called the same as variable (i.e. in one Sub you have for loop with iterator "a", whilst another Sub is called "a").
This gives an error that fits to your description.
Regards
It is possible to make your code fail in two different ways:
Place a very large value in D10
Place a text value in D10
This will result in either an overflow error or type mismatch error.
I know this was asked a while ago, but I ended up with the same error when I created a Sub within a worksheet with the Sub Name the same as the Worksheet name.
Unfortunately when this happens, no error is highlighted by a compile; the error only appears at run time. Also, the offending line is not highlighted, which makes it a bit difficult to find.
Changing the Sub name solves it though.
Here is what caused this error in my case:
Inside a new Public Function that I was starting to write, I began to type a statement, but needed to copy a long varname from another location, so I just inserted the letter a to prevent Excel from popping-up the uber-annoying Compile Error: Expected Expression dialog. So I had this:
myVarName = a
I then attempted to step-through my code to get to that point, and when Excel entered that function I received the message "Compile Error: Expected Function Or Variable". To resolve, I just changed the placeholder to a number:
myVarName = 1
And all continued just fine.