PC Review


Reply
Thread Tools Rate Thread

Different Ways To Reference Ranges

 
 
Ryan H
Guest
Posts: n/a
 
      11th Dec 2009
I need a very short way to refer to single cell ranges in a worksheet. I
have a calculator that I use in VBA and it references cells in many
worksheets. So I want to keep the code as clean as possible. For example,

'this is what I currently do
Sheets("Labor").Range("B4").Value * Sheets("Labor").Range("C6").Value

Is there a shorter way to write this? I think I have seen people use [B4] *
[C6] or something like that, but how can I include the worksheet?

Thanks in Advance!
--
Cheers,
Ryan
 
Reply With Quote
 
 
 
 
Mike
Guest
Posts: n/a
 
      11th Dec 2009
Try this
Dim ws As Worksheet
Set ws = Worksheets("Labor")

With ws
.Range("C4").Value = _
.Range("B4").Value * .Range("C6").Value
End With

"Ryan H" wrote:

> I need a very short way to refer to single cell ranges in a worksheet. I
> have a calculator that I use in VBA and it references cells in many
> worksheets. So I want to keep the code as clean as possible. For example,
>
> 'this is what I currently do
> Sheets("Labor").Range("B4").Value * Sheets("Labor").Range("C6").Value
>
> Is there a shorter way to write this? I think I have seen people use [B4] *
> [C6] or something like that, but how can I include the worksheet?
>
> Thanks in Advance!
> --
> Cheers,
> Ryan

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      11th Dec 2009
You could use:

Worksheets("Sheet1").[a1]
or even
['Sheet 99'!x88]
or if you know the code name for that sheet
Sheet1.[a1]

But it turns out, that the [] notation is slower than the range(...).value
syntax.

Personally, I find the range(...).value easier to read/debug/write--even though
there are more characters that I have to type.

Ryan H wrote:
>
> I need a very short way to refer to single cell ranges in a worksheet. I
> have a calculator that I use in VBA and it references cells in many
> worksheets. So I want to keep the code as clean as possible. For example,
>
> 'this is what I currently do
> Sheets("Labor").Range("B4").Value * Sheets("Labor").Range("C6").Value
>
> Is there a shorter way to write this? I think I have seen people use [B4] *
> [C6] or something like that, but how can I include the worksheet?
>
> Thanks in Advance!
> --
> Cheers,
> Ryan


--

Dave Peterson
 
Reply With Quote
 
Ryan H
Guest
Posts: n/a
 
      14th Dec 2009
Thanks for the replay Dave. Just curious, how do you know it is slower with
the brackets?
--
Cheers,
Ryan


"Dave Peterson" wrote:

> You could use:
>
> Worksheets("Sheet1").[a1]
> or even
> ['Sheet 99'!x88]
> or if you know the code name for that sheet
> Sheet1.[a1]
>
> But it turns out, that the [] notation is slower than the range(...).value
> syntax.
>
> Personally, I find the range(...).value easier to read/debug/write--even though
> there are more characters that I have to type.
>
> Ryan H wrote:
> >
> > I need a very short way to refer to single cell ranges in a worksheet. I
> > have a calculator that I use in VBA and it references cells in many
> > worksheets. So I want to keep the code as clean as possible. For example,
> >
> > 'this is what I currently do
> > Sheets("Labor").Range("B4").Value * Sheets("Labor").Range("C6").Value
> >
> > Is there a shorter way to write this? I think I have seen people use [B4] *
> > [C6] or something like that, but how can I include the worksheet?
> >
> > Thanks in Advance!
> > --
> > Cheers,
> > Ryan

>
> --
>
> Dave Peterson
> .
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      14th Dec 2009
This is not the first discussion for this topic. A few people who tested it,
reported their results.

(Even if it were faster, I wouldn't use it. I find the other syntax easier to
read.)


Ryan H wrote:
>
> Thanks for the replay Dave. Just curious, how do you know it is slower with
> the brackets?
> --
> Cheers,
> Ryan
>
> "Dave Peterson" wrote:
>
> > You could use:
> >
> > Worksheets("Sheet1").[a1]
> > or even
> > ['Sheet 99'!x88]
> > or if you know the code name for that sheet
> > Sheet1.[a1]
> >
> > But it turns out, that the [] notation is slower than the range(...).value
> > syntax.
> >
> > Personally, I find the range(...).value easier to read/debug/write--even though
> > there are more characters that I have to type.
> >
> > Ryan H wrote:
> > >
> > > I need a very short way to refer to single cell ranges in a worksheet. I
> > > have a calculator that I use in VBA and it references cells in many
> > > worksheets. So I want to keep the code as clean as possible. For example,
> > >
> > > 'this is what I currently do
> > > Sheets("Labor").Range("B4").Value * Sheets("Labor").Range("C6").Value
> > >
> > > Is there a shorter way to write this? I think I have seen people use [B4] *
> > > [C6] or something like that, but how can I include the worksheet?
> > >
> > > Thanks in Advance!
> > > --
> > > Cheers,
> > > Ryan

> >
> > --
> >
> > Dave Peterson
> > .
> >


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reference to external ranges eggpap Microsoft Excel Programming 1 23rd Dec 2007 12:01 PM
variable reference ranges =?Utf-8?B?U0M=?= Microsoft Excel Misc 2 14th Sep 2007 05:18 AM
Reference Ranges Chris Microsoft Excel Programming 1 15th Aug 2006 08:44 PM
How to Variable Reference Row Ranges? =?Utf-8?B?VmljV2VzdFZhbg==?= Microsoft Excel Programming 5 26th Jul 2006 04:50 PM
Re: Ways to reference a named area Peo Sjoblom Microsoft Excel Worksheet Functions 3 27th Aug 2003 03:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:07 PM.