Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

I need help selecting a vairable range in VBA

Posted on 2006-11-15
3
Medium Priority
?
151 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

715 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