Solved

I need help selecting a vairable range in VBA

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

713 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