Is it possible to use quotation marks in a string?

Glory

Well-known Member
Joined
Mar 16, 2011
Messages
640
Code:
Dim theSheet As String
theSheet = Workbooks("Augmented Verify.xls").Sheets("Sum Error")
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
This code returns an error because VBA is trying to evaluate the statement after the =, instead of setting the variable as the a string of the value specified.
<o:p> </o:p>
If I could use quotes around the value I want to assign to the variable, then there would be no problem. Is there a way to do that, or a way to set a variable as a series of statements that VBA doesn’t evaluate until it’s asked to do so?
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
What text string exactly are you assigning the string variable named thesheet? So far your code has not done that. Do you want the name of the sheet" If so, do this:

theSheet = Workbooks("Augmented Verify.xls").Sheets("Sum Error").Name

but if you do, seeing as you already know the name of the sheet because you specified it, why not just say
theSheet = "Sum Error"

Not clear what you are after.
 
Upvote 0
Everything following the = sign is the text string I want assigned to the variable “theRows”. I want to be able to put a bunch of statements that I have to use over and over again into a single variable so that I can just write that variable instead of rewriting those statements every time I have to use them.

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
eg: Workbooks("Workbook.xls").Sheets("Sheet").Range("A2:J" & _
Workbooks("Workbook.xls").Sheets("Sheet").Range("A" & Rows.Count).End(xlUp).Row).Select

<o:p></o:p>
becomes

<o:p></o:p>
theRows.Range(“A2:J” & theRows.Range(“A” & Rows.Count).End(xlUp).Select
 
Upvote 0
Glory,

I think you are mixing objects and variables. To use something like therows.Range(..., therows must be a worksheet-object.

To do so you need

Dim therows as Worksheet
Set therows = Sheets("SheetName")

and then you can use, for example, therows.Range("A1"), to refer to the cell A1 of sheet whose name is "SheetName".

If for some reason you do want to assign this
Workbooks("Augmented Verify.xls").Sheets("Sum Error")
to a string variable you have to double the quotes inside. Something like

Code:
Sub test()
Dim MyString As String
 
MyString = "Workbooks(""Augmented Verify.xls"").Sheets(""Sum Error"")"
 
MsgBox MyString
End Sub

M.
 
Upvote 0
You are absolutely correct... I've gotten confused over this before, and I'm sure it won't be the last time.

Thank you.
 
Upvote 0

Forum statistics

Threads
1,224,570
Messages
6,179,611
Members
452,931
Latest member
The Monk

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top