Using a Variable references to indicate a range of cells to merge

Tyron

Active Member
Joined
Dec 5, 2012
Messages
258
Hey Everyone,

I was wondering if it were possible to use variables to reference cell values in a range to merge?

I am using Excel 2013.

Here is my current code:
Code:
Sub CompanyHeader()

Dim StartRange as String
Dim EndRange as String

StartRange = "A1"
EndRange = "J3"

Range("StartRange:EndRange").Merge

End Sub

I am currently getting the following error:
Runtime error 1004
Method 'Range' of object '_Global' failed

I have been searching the web and have seen most people using code such as:
Code:
Range(Cells(1,1), Cells(3,4))

but would prefer not to do if I can. Want to be able to change the StartRange and EndRange on the fly if I have to.

I have in mind to use these variables later in the function as well for cell formatting.

Thanks in advance

later

Ty
 
The quote marks around "A1" are not part of the text that will be assigned to the StartRange variable.. they are there to tell VB that all the characters between the quotes should be assigned to the variable. Why do it this way? If you did not use quotes around the characters, you would have this...

StartRange = A1

Doing it this way, A1 looks like a variable name to VB, so it would attempt to find a variable by that name and assign it contents to StartRange. To stop VB from seeing A1 as a variable name, you put quotes around it which tells VB that what is between them has no meaning other than as a collection of individual characters.


Hi Rick,
. I think that is one 0f the things I was trying to say
....
. So this does not work to give you the valuein the first cell in a spreadsheet<o:p></o:p>
<o:p>
Code:
Sub RangeBasics1()

MsgBox "First cell value is " & Range(A1).Value

End Sub
</o:p>

Because A1 is anundefined variable<o:p></o:p>
<o:p></o:p>These two do because youhave defined that variable to a string of the form that the Range objectexpects as it's argument.<o:p></o:p>
<o:p></o:p>
<o:p>
Code:
Sub Rangebasics2()

Dim A1 As String

Let A1 = "A1"

MsgBox "First cell value is " & Range(A1).Value

End Sub
</o:p>

<o:p>
Code:
Sub RangeBasics3()

Dim A1 As String

Let A1 = "$A$1"

MsgBox "First cell value is " & Range(A1).Value

End Sub
</o:p>



But I think you explained it a bit more concisely,
Thanks
Alan
 
Last edited:
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hey Guys,

Thanks to all of you for clarifying the situation.

Andrew: Yeah, good catch on my mistake. I did mean to put quotes not parenthesis. Sorry about that.

Doc: I started to get a little confused, but it helped me understand Andrew's two posts better when I went back to the top and started reading the thread from the begining.

Rick: The information you gave me regarding why VBA needs quotes in order to distinguish between Values and Variables also opened my eyes a bit more as well.

So this subject is definitely a lot clearer and has helped me to understand why Range works the way it does.

Thanks to all again.

later

Ty
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,346
Members
452,638
Latest member
Oluwabukunmi

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