RageAgainstheMachine
RageAgainstheMachine

Reputation: 949

constant expression required - vba

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

Upvotes: 0

Views: 5157

Answers (2)

Rajesh Sinha
Rajesh Sinha

Reputation: 197

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.

Upvotes: -1

Shai Rado
Shai Rado

Reputation: 33682

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"

Upvotes: 3

Related Questions