Range returns unexpected value.

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,072
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
In Module1
Public iRange As Range
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Row > 1 Then
    Cancel = True
    Set iRange = Range("A" & Target.Row & ":CV" & Target.Row)
end if

Debug.print iRange(Target.Row, "C")
Target.Row is 5534.

I expect to see the value of Col C at row 5534 but instead it shows the Contents of "C" at row 11070
Excel must be right and I'm wrong, but how ?
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Ah, got it. Thanks... although ? iRange(2,3) instead of an error prints Col C for Target.row+1
I would have thought Set iRange = Range("A" & Target.Row & ":CV" & Target.Row) would limit the range to just one row.
The idea being that range could be used anywhere and contain the data in that row.
 
Upvote 0
I'm not sure how to explain it without making it more confusing.
I am suprised you say it returned row 11070 it should have been $C$11067.

? Range("A5534:CV5534").Address
returns $A$5534:$CV$5534

But you are using
? iRange(5534,"C").Address (you could use 3 instead of C)
which should have returned
$C$11067

Since iRange = Range("A5534:CV5534"), you have effectively put
? Range("A5534:CV5534")(5534,"C").Address

Where
? Range("A5534:CV5534")(1,"A").Address
$A$5534
? Range("A5534:CV5534")(1,"C").Address
$C$5534
? Range("A5534:CV5534")(2,"A").Address
$A$5535

Further reading: Range.Item property (Excel)
 
Upvote 0
Yes, you're right it was $C$11067. and it is a bit confusing.
First time I'd used Range like this and I had it all wrong, but seemed logical.
But right result with iRange (1, Col) instead of (LineNum, Col).
And I presume no way without the "1"?
 
Upvote 0
If you want to describe what you are trying to do we might be able to give you some other suggestion but in terms of your last question this might work.
Row position left blank but you still need the comma before the C
? Range("A5534:CV5534")(,"C").Address
$C$5534
or in your case
? iRange(,"C").address
$C$5534
 
Upvote 0
Ah,OK. I'll use that, thanks.
I'm doing stuff in VBA with data in a certain row, I send In Target to my functions but that didn't (easily) give any column so trying this
global Range object which looks like it'll do what I want. I have every column on tap now.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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