Solved

I need help selecting a vairable range in VBA

Posted on 2006-11-15
3
149 Views
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?
0
Comment
Question by:tiehaze
[X]
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
3 Comments
 
LVL 81

Expert Comment

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

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

Kevin
0
 

Author Comment

by:tiehaze
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.
0
 
LVL 81

Accepted Solution

by:
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

Kevin
0

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

623 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