Help with Grabbing Cell Value from Range

  • Thread starter Thread starter Chris C
  • Start date Start date
C

Chris C

This is the code I have:

' Grab the database name
Dim strDb As Range
Set strDb = Worksheets("Dashboard").Range("C2")
Dim strDBString As String
Set strDBString = strDb.Text

The range is assigned without a problem; however, extracting the actual text
value seems never to work. Can anyone clarify exactly how this should happen?

My underlying motivation is to pull a cell value in VBA, and then use that
in a SQL query within VBA as well.
 
hi
looks like your are trying to set a string as an object. can't do this.
try this
Sub tttest()
Dim strDb As Range
Set strDb = Sheets("sheet1").Range("C2")'range is an object
Dim strDBString As String
strDBString = strDb.Text 'string is not an object
MsgBox strDBString
End Sub

regards
FSt1
 
The issue is that it's throwing me an "Object Required" error. I understand
that it's not being assigned properly, but the API seems to indicate the the
"Text" property of the Range object is what should be used for grabbing the
value from the range.

I looked to make sure that the strDb is getting assigned a value, and it is,
and it's the exactly the value I'm looking for.
 
hi
i suspected that that was the error you were getting.
you "set" objects to equal somthing using the set command.
but a string is not an object so you just have it equal somthing WITHOUT the
set command. this is why your are getting the error. a string is not an
object, more like a value. a range is an object that contains the value.
other than that, your code works fine.

regards
FSt1
 
Back
Top