Wrong range selected using .range.cells

cbrf23

Board Regular
Joined
Jun 20, 2011
Messages
241
Not sure whats going on here... but with the code below, if I type:
Code:
?rngwhole.Address
in the immediate window I get:
Code:
$A$20:$M$20 '[[correct answer]]
Then if I type:
Code:
?rngwhole.Range(.Cells(1, 1), .Cells(1, 2)).Address
I get:
Code:
$A$39:$B$39 '[[correct column, BUT +19 rows off!]]
Anyone have any idea whats going on?

Code in question:
Code:
Sub testy()
    Dim rngWhole As Range
 
    Set rngWhole = Range("A20:M20")
 
    With rngWhole
            With .Range(.Cells(1, 1), .Cells(1, 2))
                .Select
                .HorizontalAlignment = xlCenterAcrossSelection
                .VerticalAlignment = xlBottom
                .WrapText = True
            End With
            With .Cells(1, 3)
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlBottom
                .WrapText = True
            End With
            With .Range(.Cells(1, 10), .Cells(1, 12))
                .Select
                .HorizontalAlignment = xlCenterAcrossSelection
                .VerticalAlignment = xlBottom
                .WrapText = True
            End With
     End With
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Code:
?rngwhole.Range([COLOR="Red"].[/COLOR]Cells(1, 1), [COLOR="Red"].[/COLOR]Cells(1, 2)).Address
The .Cells are using the With statement as well. It is equivalent to...
Code:
?rngwhole.Range([COLOR="Red"]Range("A20:M20")[/COLOR].Cells(1, 1), [COLOR="Red"]Range("A20:M20")[/COLOR].Cells(1, 2)).Address

I think you want this...
Code:
?rngwhole.Range(Cells(1, 1), Cells(1, 2)).Address
 
Upvote 0
OK, let's break it down.

If rngWhole = A20:M20
Then
rngWhole.Cells(1,1) = A20
rngWhole.Cells(1,2) = B20

With me so far

So that translates to rngWhole.Range(A20:B20)
The A20:B20 is RELATIVE to the original address of rngWhole (A20:M20)
So it's basically 20 rows from A20 (including A20) = A39


This is why most of us dislike doing Range(...).Range(...)
It gets very confusing.


I think what you want is
rngWhole.Resize(1,2)


Hope that helps.
 
Upvote 0
AlphaFrog - genius! thank you.

JonMo1 - I am not super familiar with the resize method...I'll have to check it out on the msdn and see what I can find out about it. Thank you for the suggestion.
 
Upvote 0
Remove the dot from the two argument Range
Code:
Sub testy()
    Dim rngWhole As Range
 
    Set rngWhole = Range("A20:M20")
 
    With rngWhole
            With Range(.Cells(1, 1), .Cells(1, 2)): Rem <<<<<<<
                .Select
                .HorizontalAlignment = xlCenterAcrossSelection
                .VerticalAlignment = xlBottom
                .WrapText = True
            End With
            With .Cells(1, 3)
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlBottom
                .WrapText = True
            End With
            With Range(.Cells(1, 10), .Cells(1, 12)): Rem <<<<<<<
                .Select
                .HorizontalAlignment = xlCenterAcrossSelection
                .VerticalAlignment = xlBottom
                .WrapText = True
            End With
     End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,289
Members
452,902
Latest member
Knuddeluff

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