If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Assigning Range - Relative reference Vs Worksheet reference

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-29-09, 07:41
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Assigning Range - Relative reference Vs Worksheet reference

Hi

I have this in a Worksheet module:
Code:
Range(Cells(1, 2), Cells(12, 10)).Value = 1
Works fine.
Change it to:
Code:
Worksheets("tester").Range(Cells(1, 2), Cells(12, 10)).Value = 1
or
Code:
ActiveSheet.Range(Cells(1, 2), Cells(12, 10)).Value = 1
and it fails.

Any way I can use this syntax when referring to a specific sheet? I would prefer to avoid using "B1" notation since I am iterating using integers however I'm open to other suggestions

Thanks
poots
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #2 (permalink)  
Old 10-29-09, 07:53
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Ignore for now - I'm being a dumbass
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #3 (permalink)  
Old 10-29-09, 08:30
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 477
Just in case someone else researching this issue happens to read this thread....

Qualify the cells properties in the same way as the range property:

Code:
ActiveSheet.Range(Cells(1, 2), Cells(12, 10)).Value = 1
---->

Code:
ActiveSheet.Range(ActiveSheet.Cells(1, 2), ActiveSheet.Cells(12, 10)).Value = 1
Reply With Quote
  #4 (permalink)  
Old 10-29-09, 08:34
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Ok - what I was looking for was:
Code:
Worksheets("tester").Range(Worksheets("tester").Cells(1, 2), Worksheets("tester").Cells(12, 10)).Value = 1
However, now I've solved that simplification of my problem I am stuck again.

So, adding stuff in slowly that want to use:

This works
Code:
Worksheets("Tester").Range(Worksheets("Tester").Cells(1, 1), Worksheets("Tester").Range("A1").End(xlToRight)).Value = 1
However this fails:
Code:
Worksheets("Tester").Range(Worksheets("Tester").Cells(1, 1), Worksheets("Tester").Range(Worksheets("Tester").Cells(1, 1)).End(xlDown)).Value = 1
This works:
Code:
Worksheets("Tester").Range("A1").End(xlDown).Value = 1
This fails:
Code:
Worksheets("Tester").Range(Worksheets("Tester").Cells(1, 1)).End(xlDown).Value = 1
Is the short of this that I can't assign a range property using .Cells if I want to use the .End method?
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #5 (permalink)  
Old 10-29-09, 08:35
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Quote:
Originally Posted by Colin_L View Post
Qualify the cells properties in the same way as the range property:
Yup - that was the point I realised I was being a dumbass.

Just relieved I didn't ask my follow up question (where the solution was not to confuse xlRight and xlToRight)
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #6 (permalink)  
Old 10-29-09, 08:43
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 477
Does this work for you?

Code:
Sub foo()

    Worksheets("Tester").Range( _
        Worksheets("Tester").Cells(1, 1), _
        Worksheets("Tester").Cells(1, 1).End(xlDown) _
                                ).Value = 1


End Sub
Reply With Quote
  #7 (permalink)  
Old 10-29-09, 08:48
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Ah crap - of course

I was editing some existing code. Obviously, the use of .Range there was because it was referring to the cell as "A1". I was just nesting statements without realising I didn't need to.

Awesome - thanks Colin - I've been battling with that for ages.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #8 (permalink)  
Old 10-29-09, 08:53
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 477
Coolio.

I know exactly what you mean, nested Ranges and Cells can very quickly get confusing, especially when you're editing existing code.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On