0

I'm trying to create an URL that changes based on what I put in cell "A1", however, I keep getting a compile error "Constant expression required".

The error is on the second line at "banana".

 banana = Range("A1")

 Const URL As String = "http://openinsider.com/screener?s=" & banana & "&o=&pl=&ph=&ll=&lh=&fd=730&fdr=&td=0&tdr=&fdlyl=&fdlyh=&daysago=&xp=1&xs=1&vl=&vh=&ocl=&och=&sic1=-1&sicl=100&sich=9999&grp=0&nfl=&nfh=&nil=&nih=&nol=&noh=&v2l=&v2h=&oc2l=&oc2h=&sortcol=0&cnt=100&page=1"
 Const READYSTATE_COMPLETE As Integer = 4
0

2 Answers 2

3

VBA expects a Const URL, but you are actually trying to use it as a dynamic variable String, not Const.

If you want your URL to be dynamic according to the value in Range("A1"), use the code below:

Dim banana As String
Dim URL As String

banana = Range("A1").Value
URL = "http://openinsider.com/screener?s=" & banana & "&o=&pl=&ph=&ll=&lh=&fd=730&fdr=&td=0&tdr=&fdlyl=&fdlyh=&daysago=&xp=1&xs=1&vl=&vh=&ocl=&och=&sic1=-1&sicl=100&sich=9999&grp=0&nfl=&nfh=&nil=&nih=&nol=&noh=&v2l=&v2h=&oc2l=&oc2h=&sortcol=0&cnt=100&page=1"
-1

Actually you are getting error because when you declare a constant, the value you give it must be constant too. You cannot declare a constant with a variable.

So declare like this,

Dim Banana as String Dim URL as String

Banana = sheets (1).Range ("A1"). Value

Use either If or Select Case here since URL will change as per value changes in A1,

URL = "https://as.com"

Then other code.

3
  • An If or Select Case is not required simply because a variable's value could change. That is why programming languages have variables - so that they can be used in code and that code will use whatever the current value of the variable is.
    – YowE3K
    Apr 17, 2017 at 20:40
  • Please check the original question,, person has written the URL will change if the value in A1 will and this can be executed only by testing THE A1, Logically. Apr 18, 2017 at 9:10
  • No, you can write a statement such as URL = "http://openinsider.com/screener?s=" & banana & "&o=&pl=&page=1", which will set URL to the string "http://openinsider.com/screener?s=" concatenated with the current value of the variable banana concatenated with the string "&o=&pl=&page=1". You obviously can't include a variable when calculating the value of a constant but, providing URL is a variable and not a constant, you can calculate URL using a variable. See the answer by @ShaiRado for the correct way of doing this.
    – YowE3K
    Apr 18, 2017 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.