I need help selecting a vairable range in VBA

Posted on 2006-11-15
Last Modified: 2010-07-27
I am trying to use variables in vba to copy a range and then paste it into "sheet2". Here is a simplified example, please tell me what I am doing wrong.

Sub Macro ()

Dim r1 As String
Dim r2 As String
r1 = Range("A1").address
r2 = Range("B2000").Address
[r1:r2].Copy Worksheets("Sheet2").[a1]
End Sub

I want to know how to use the two variables to copy cells A1:B2000 and copy them to sheet2. I think it is actually selecting cells "R1" through "R2". Can anone help me fix this?
Question by:tiehaze
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 17949641
Try this:

   [A1:B2000].Copy Worksheets("Sheet2").[A1]


Author Comment

ID: 17960780
What I need to know is how to select a range with variables... above was just a simplified version of what I am trying to do.
LVL 81

Accepted Solution

zorvek (Kevin Jones) earned 500 total points
ID: 17961221
Got it...

Sub Macro ()

Dim r1 As String
Dim r2 As String
r1 = Range("A1").address
r2 = Range("B2000").Address
Range(Range(r1),Range(r2)).Copy Worksheets("Sheet2").[a1]
End Sub


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question