Solved

I need help selecting a vairable range in VBA

Posted on 2006-11-15
3
135 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now