Using Variable to define a range

KerryFSA

New Member
Joined
Dec 5, 2018
Messages
49
The following code is generating an application defined/object defined error
in the Set Range2 and Set Range3 lines.
I have verified n = 71 and m = 13 (row numbers) on the first execution of the code.
The syntax seems to be the same as in the Set Range1 line. Can you identify the problem?
Thanks very much, Kerry

n = ActiveWorkbook.Sheets("MEMBERS").Range("T8") ' last row for additions
m = ActiveWorkbook.Sheets("MEMBERS").Range("U8") + 1 ' first row for additions

' Resort = ActiveWorkbook.Worksheets("MEMBERS").Range("U10") ' checks if previous sort

' Copy names
Set Range1 = ActiveWorkbook.Worksheets("MEMBERS").Range(Cells(m, 21), Cells(n, 21))
Set Range2 = ActiveWorkbook.Worksheets("PREORDER").Range(Cells(m, 5), Cells(n, 5))
Set Range3 = ActiveWorkbook.Worksheets("Member Purchases").Range(Cells(m, 5), Cells(n, 5))

Range1.Copy
Range2.PasteSpecial xlPasteValuesAndNumberFormats
Range3.PasteSpecial xlPasteValuesAndNumberFormats
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
you are trying the identify a range in this block of cade, when you only need a ROW identifier

Code:
Range(Cells(m, 21), Cells(n, 21))

trying changing this

Code:
n = ActiveWorkbook.Sheets("MEMBERS").Range("T8") ' last row for additions
m = ActiveWorkbook.Sheets("MEMBERS").Range("U8") + 1 ' first row for additions

to

Code:
n = ActiveWorkbook.Sheets("MEMBERS").rows("8:8") ' last row for additions
m = ActiveWorkbook.Sheets("MEMBERS").rows("9:9") ' first row for additions
 
Upvote 0
I think your issue is that you haven't qualified your Cells with the worksheet, try...

Code:
    n = ActiveWorkbook.Sheets("MEMBERS").Range("T8")    ' last row for additions
    m = ActiveWorkbook.Sheets("MEMBERS").Range("U8") + 1    ' first row for additions

    ' Resort = ActiveWorkbook.Worksheets("MEMBERS").Range("U10") ' checks if previous sort

    ' Copy names
    With Worksheets("MEMBERS")
        Set Range1 = .Range(.Cells(m, 21), .Cells(n, 21))
    End With
    With Worksheets("PREORDER")
        Set Range2 = .Range(.Cells(m, 5), .Cells(n, 5))
    End With
    With Worksheets("Member Purchases")
        Set Range3 = .Range(.Cells(m, 5), .Cells(n, 5))
    End With

or you could just do the below if the paste is all that you are using Range1 and Range2 for.

Code:
    n = ActiveWorkbook.Sheets("MEMBERS").Range("T8")    ' last row for additions
    m = ActiveWorkbook.Sheets("MEMBERS").Range("U8") + 1    ' first row for additions

    ' Resort = ActiveWorkbook.Worksheets("MEMBERS").Range("U10") ' checks if previous sort

    ' Copy names
    With Worksheets("MEMBERS")
        Set Range1 = .Range(.Cells(m, 21), .Cells(n, 21))
    End With

    Set Range2 = Worksheets("PREORDER").Cells(m, 5)
    Set Range3 = Worksheets("Member Purchases").Cells(m, 5)

    Range1.Copy
    Range2.PasteSpecial xlPasteValuesAndNumberFormats
    Range3.PasteSpecial xlPasteValuesAndNumberFormats
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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