Variable Range

gwandalf

New Member
Joined
Feb 16, 2011
Messages
30
Hi everyone!!!
First of all thanks in advance to help me with this problem

I think I have a problem with the syntax of the code but I dont understand where and why. I am trying to use the application LookUp, which is working nice. The problem is when I am trying to make my range able to change. I mean that Arg2 and Arg3 can change

THIS CODE IS WORKING without any problem but it is not what I need

Sub Set_New()

Arg2 = MPA_Input.Range("A2:AHK2")
Arg3 = MPA_Input.Range("A3:AHK3")
Arg1 = MPA_Mechanical.Range("T9")

X = Application.Lookup(Arg1, Arg2, Arg3)

End Sub


THIS ONE IS NOT WORKING and I will need something like that
Note that is part of a bigger code and I define before all the variables as Long. When I was debugging the variables has their values.

Sub Set_New()

Arg2 = MPA_Input.Range(Cells(rowIndex_T, startColIndex), Cells(rowIndex_T, endColIndex))
Arg3 = MPA_Input.Range(Cells(rowIndex_S, startColIndex), Cells(rowIndex_T, endColIndex))
Arg1 = MPA_Mechanical.Range("T9")

X = Application.Lookup(Arg1, Arg2, Arg3)

End Sub

But is giving me an error in Arg2 ( I guess will be the same for Arg3 )
Method 'Range' of object'_Worksheet'failed


ANOTHER ONE

I also try with this syntax but is giving me an error but in different poing

Sub Set_New()

Arg2 = "='" & wsheet(1) & "'!R" & rowIndex_Time & "C" & startColIndex & ":R" & rowIndex_Time & "C" & endColIndex & ""
Arg3 = "='" & wsheet(1) & "'!R" & rowIndex_Speed & "C" & startColIndex & ":R" & rowIndex_Speed & "C" & endColIndex & ""
Arg1 = MPA_Mechanical.Range("T9")

X = Application.Lookup(Arg1, Arg2, Arg3)

End Sub

but in this case "X" is giving me ERROR 2015

THANKS TO ALL OF YOU
Any suggestion will be apreciate
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
You need to qualify the Cells property as well as the Range property. Example:

Rich (BB code):
Arg2 = MPA_Input.Range(MPA_Input.Cells(rowIndex_T, startColIndex), MPA_Input.Cells(rowIndex_T, endColIndex)).Value

To save repeating it you can use a With ... End With construct:

Rich (BB code):
With MPA_Input
    Arg2 = .Range(.Cells(rowIndex_T, startColIndex), .Cells(rowIndex_T, endColIndex)).Value
End With

You still need the dot qualifier for both properties.

Edit:

If you want to assign the range to an object variable:

Rich (BB code):
Dim Arg2 As Range
With MPA_Input
    Set Arg2 = .Range(.Cells(rowIndex_T, startColIndex), .Cells(rowIndex_T, endColIndex))
End With
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,525
Messages
6,179,317
Members
452,905
Latest member
deadwings

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