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 > .offset question - hopefully and easy one

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-06-10, 13:21
mike703 mike703 is offline
Registered User
 
Join Date: Mar 2005
Posts: 23
.offset question - hopefully and easy one

I am trying to get excel to go from the activecell ("A1") and select range "C2:d9" based on the following code:

Code:
Range(ActiveCell.Offset(1, 2), ActiveCell.Offset(8, 3)).Select
However, when the compiler gets to that line, I get an error stating "Method 'Range of object'_Worksheet' failed". I'm sure I might be forgetting something, but can not figure out why excel will not select the cell range.
Reply With Quote
  #2 (permalink)  
Old 12-06-10, 13:38
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
probably best to ask this question in the Excel forum...
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 12-06-10, 13:52
mike703 mike703 is offline
Registered User
 
Join Date: Mar 2005
Posts: 23
nevemind, found the problem...compiler was gettting confused because the code is to work across multiple sheets and couldn't decipher which sheet to run the code on. Once I specified Activesheet.Range(Activecell.offset... ) all was fine.
Reply With Quote
  #4 (permalink)  
Old 12-06-10, 15:20
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
Hi Mike,

Why not just use this?
Code:
ActiveSheet.Range("C2:D9").Select
__________________
Colin

RAD Excel Blog

Other tutorials:
Array Formulas | Deleting Rows with VBA

Last edited by Colin Legg; 12-06-10 at 15:26.
Reply With Quote
  #5 (permalink)  
Old 12-07-10, 11:53
mike703 mike703 is offline
Registered User
 
Join Date: Mar 2005
Posts: 23
I know that would've been the simple solution, but I try not to use absolute ranges in my code if at all possible. I tend to get less errors that way
Reply With Quote
  #6 (permalink)  
Old 12-07-10, 14:09
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
What makes your code error prone is that it uses ActiveCell etc... which then relies upon particular ranges being selected.

In a general sense, rather than using Offset within Range, Cells is better.
Code:
Sheet1.Range(Sheet1.Cells(2, 3), Sheet1.Cells(10, 5)).Select
(Not that I'm recommending the Select method attached on the end - I'm concentrating on the Range syntax). This syntax is useful in loops, for example. But there's nothing error prone about the last example I posted.
__________________
Colin

RAD Excel Blog

Other tutorials:
Array Formulas | Deleting Rows with VBA
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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