Problems selecting variable range in VBA

Sc0ut

New Member
Joined
Sep 24, 2014
Messages
6
Hello,

I'm really hoping someone can help me with this. I have been working on it all day and I can't seem to get it to work. I need to use variables to reference and copy a range of cells.

I'm getting the following error with this code that I have been working on:
"Run-time error '1004': Method 'Range' of object '_Worksheet' failed"

Here is my code:
Code:
Sub test()Dim StartrowC1 As String
Dim EndrowC1 As String
Dim C1Selection As String


StartrowC1 = (ActiveCell)
EndrowC1 = (ActiveCell.Offset(8, 0))
Let C1Selection = StartrowC1 & ":" & EndrowC1


'Selection.Copy
Sheet18.Range(C1Selection).Select


'and that's as far as I can get at the moment :(


End Sub

Please help! :eek:
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try

Code:
StartrowC1 = ActiveCell.Address
EndrowC1 = ActiveCell.Offset(8).Address
Let C1Selection = StartrowC1 & ":" & EndrowC1
 
Upvote 0
Here you go:

Rich (BB code):
Sub test()


    Dim StartrowC1 As String
    Dim EndrowC1 As String
    Dim C1Selection As String
    
    StartrowC1 = (ActiveCell.Address)
    EndrowC1 = (ActiveCell.Offset(8, 0).Address)
    Let C1Selection = StartrowC1 & ":" & EndrowC1
    
    
    'Selection.Copy
    Sheet1.Range(C1Selection).Select
    
    
    'and that's as far as I can get at the moment :(




End Sub
 
Upvote 0
You're a genius! Thank you BOTH so much. I wish I had asked earlier! So much wasted time. I can't thank you enough.
 
Upvote 0
Try

Code:
StartrowC1 = ActiveCell.Address
EndrowC1 = ActiveCell.Offset(8).Address
Let C1Selection = StartrowC1 & ":" & EndrowC1

Hello again,

I seem to be having that same problem with the next array of data to be copied. As this array only requires 4 separate cells to be copied, I'm trying to copy them to one variable that I would then paste from. Could this be the problem?

Error:
"Run-time error '1004': Method 'Range' of object '_Worksheet' failed"

Here is the code:
Code:
Sub test()
'Select data to be copied
Dim Cr1 As String
Dim Cr2 As String
Dim Cr3 As String
Dim Mrt As String
Dim MRSelection As String

'YEAR/MONITORING RESULTS
If Sheet1.Range("H17").Value = "" Then
Exit Sub
Else

'Prepare copy
Cr1 = ActiveCell.Offset(8, 0).Address
Cr2 = ActiveCell.Offset(8, 5).Address
Cr3 = ActiveCell.Offset(8, 10).Address
Mrt = ActiveCell.Offset(8, 15).Address
Let MRSelection = Cr1 & Cr2 & Cr3 & Mrt

'Execute copy and paste of selection
Sheet18.Range(MRSelection).Copy
Sheet1.Range("E26:H26").PasteSpecial (xlPasteValues)

End If

End Sub
Thanks in advance! :cool:
 
Last edited:
Upvote 0
Thank you! It works perfectly!

I'm am very grateful for you help. :rolleyes:
 
Last edited:
Upvote 0
One last thing if you can spare a second.

Is there any way to background the changing between sheets as it is processing the code? At the moment, when I activate the macro on the master sheet it takes me to a sheet that I don't want to see.

I have been looking into this and while it seems that you can make applications invisible (visible = false), the calling up of sheets in the same workbook seems to be harder to hide.

Very grateful for you help!
 
Upvote 0
Application.Screenupdating=false
'your code here
applicationj.screenupdating=true
 
Upvote 0

Forum statistics

Threads
1,213,532
Messages
6,114,177
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top