Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default Question about a range

Suppose I want to select a specific range, such as:

Range("B10:Q10").Select

The above range exists on row #10, between
Column B and Column Q.

In my VBA program, the user is allowed to pick only
a row number, such as row 10. If I know the row
number, what are the possible ways to convert this
value into the above range?

The only thing that I can think of is the following code:

Range("B" & row_n & ":Q" & row_n).Select

In the above code, the variable "row_n" stores the
value of the row that a user can pick. I'm pretty sure
this will work. I just somehow think there are other
ways to achieve the same result, as opposed to
concatenating a bunch of strings to form a range string.

Are there other techniques here?

thanks



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Question about a range

Hi Robert,

Am Thu, 16 May 2013 01:47:13 -0700 schrieb Robert Crandal:

Range("B" & row_n & ":Q" & row_n).Select


try:
Intersect(Columns("B:Q"), Rows(ActiveCell.Row)).Select


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default Question about a range

"Claus Busch" wrote

try:
Intersect(Columns("B:Q"), Rows(ActiveCell.Row)).Select


Wow, thanks Claus. That's a good one.

Just curious, but is it possible to specify that same range above
using other objects such as Cells(), etc?? It seems like Excel has
tons of ways to specify the same range.

Thanks again for your awesome help! 8)


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Question about a range

Hi Robert,

Am Thu, 16 May 2013 02:06:06 -0700 schrieb Robert Crandal:

Just curious, but is it possible to specify that same range above
using other objects such as Cells(), etc?? It seems like Excel has
tons of ways to specify the same range.


the arguments for Intersect are ranges. You can specify 2 ranges up to
30 ranges. You can use all objects that returns a range.


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 829
Default Question about a range

"Robert Crandal" wrote:
If I know the row number, what are the possible ways to
convert this value into the above range?
The only thing that I can think of is the following code:
Range("B" & row_n & ":Q" & row_n).Select


"Robert Crandal" wrote:
try: Intersect(Columns("B:Q"), Rows(ActiveCell.Row)).Select

[....]
Just curious, but is it possible to specify that same range
above using other objects such as Cells(), etc?? It seems
like Excel has tons of ways to specify the same range.


I believe your original form is the most efficient. But to answer your
question, the following will work:

Range(Cells(row_n,"b"),Cells(row_n,"q"))

But AFAIK, each reference to a range involves some interprocess
(interthread) communication between Excel and VBA. So the
Range(Cells,Cells) form is probably slower than the
Range(string-concatenation) form.

For similar reasons, I would not use Intersect for this purpose. Not only
because of the potential for increased intercommunication with Excel, but
also because it is overkill. Intersect is intended for much more complex
situations. So I doubt that it is as efficient as even the
Range(Cells,Cells) form.

BTW, you should give serious thought to using the Select property. Recorded
macros (over)use it a lot. But usually, it is avoidable, and it is
significantly better to avoid it.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 147
Default Question about a range

Robert Crandal wrote:
Suppose I want to select a specific range, such as:

Range("B10:Q10").Select

The above range exists on row #10, between
Column B and Column Q.

In my VBA program, the user is allowed to pick only
a row number, such as row 10. If I know the row
number, what are the possible ways to convert this
value into the above range?

The only thing that I can think of is the following code:

Range("B" & row_n & ":Q" & row_n).Select

In the above code, the variable "row_n" stores the
value of the row that a user can pick. I'm pretty sure
this will work. I just somehow think there are other
ways to achieve the same result, as opposed to
concatenating a bunch of strings to form a range string.

Are there other techniques here?

thanks





Range (cells (row, 2), cells (row,17)).Select

check 17 :)


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default Question about a range

"joeu2004" wrote

BTW, you should give serious thought to using the Select property.
Recorded macros (over)use it a lot. But usually, it is avoidable, and it
is significantly better to avoid it.


Hi joeu2004,

I programmed a search feature into my VBA program. Each time a search
hit is found I use the "Select" operation simply to highlight a particular
row.
So, I'm using Select just to make a row easily visible to users.

Are you saying that I should NOT use this "Select" operation because it
causes interprocess communication between Excel and VBA? Is this
going to cause problems or bugs in my program? Just curious....

Robert


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 147
Default Question about a range

Robert Crandal wrote:
"joeu2004" wrote

BTW, you should give serious thought to using the Select property.
Recorded macros (over)use it a lot. But usually, it is avoidable, and
it is significantly better to avoid it.


Hi joeu2004,

I programmed a search feature into my VBA program. Each time a search
hit is found I use the "Select" operation simply to highlight a
particular row.
So, I'm using Select just to make a row easily visible to users.

Are you saying that I should NOT use this "Select" operation because it
causes interprocess communication between Excel and VBA? Is this
going to cause problems or bugs in my program? Just curious....

Robert




He is saying that everywhere you have construction

range(....).Select
Selection.something()

it can be replaced by

range(...).something()

selecting is not needed there and is used for nothing.
Macro recorder does that because you click cell so it thinks you want to
select it.

..Select can be used if you really want to show something to user.

Just be careful.
Run you code when worksheet is hidden or protected. You will be surprised.



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 829
Default Question about a range

"Robert Crandal" wrote:
I programmed a search feature into my VBA program. Each time a search
hit is found I use the "Select" operation simply to highlight a particular
row. So, I'm using Select just to make a row easily visible to users.


That is a valid use of Select. I do the same thing sometimes.


"Robert Crandal" wrote:
Are you saying that I should NOT use this "Select" operation because
it causes interprocess communication between Excel and VBA?


No. We cannot escape the interprocess communication. But it is good to
minimize it when it makes sense to do so. The operative words are "when it
makes sense".

Many people take a recorded macro and either modify it or think it serves as
a good programming model. This results in an over-use of Select and other
unnecessary features, which is grossly ineffiecient. Recorded macros are a
good way to see how to do something __in_general__. But usually, they can
and should be optimized.

For example, when entering 1, 2, 3 and 4 into A1, A2, A3 and A4, the
recorded macro is:

Range("A1").Select
ActiveCell.FormulaR1C1 = "1"
Range("A2").Select
ActiveCell.FormulaR1C1 = "2"
Range("A3").Select
ActiveCell.FormulaR1C1 = "3"
Range("A4").Select
ActiveCell.FormulaR1C1 = "4"

Obviously, the following is better (and could be optimized further):

Range("A1") = 1
Range("A2") = 2
Range("A3") = 3
Range("A4") = 4

But that comment does not seem to apply to your use of Select.


"Robert Crandal" wrote:
Is this going to cause problems or bugs in my program?


No way to say without seeing the full macro. But generally, no. The
over-use of Select is just inefficient, usually not wrong.

  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 277
Default Question about a range

On Thu, 16 May 2013 11:14:52 +0200, Claus Busch
wrote:

Hi Robert,

Am Thu, 16 May 2013 02:06:06 -0700 schrieb Robert Crandal:

Just curious, but is it possible to specify that same range above
using other objects such as Cells(), etc?? It seems like Excel has
tons of ways to specify the same range.


the arguments for Intersect are ranges. You can specify 2 ranges up to
30 ranges. You can use all objects that returns a range.


Regards
Claus Busch



Just give it a name.

highlight the cell range in question, and place the cursor in the cell
address box (upper left corner)and TYPE the range name you want to give
it. from then on, a reference to that range name is ONLY that specified
range. In the new excel (not sure on older versions) you can click on a
drop down in the cell address box and any named ranges will be listed.
Clicking on one will take you there, and highlight it.
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Range question greg Excel Programming 4 July 11th 09 12:56 PM
Range question Barb Reinhardt Excel Programming 14 July 11th 06 04:12 PM
Range Question / error 1004: method Range of object Worksheet has failed Paul Excel Programming 3 April 7th 05 02:56 PM
range: question Simon Sunke Excel Programming 2 February 23rd 04 03:25 PM
Range.Formula and Range question using Excel Automation [email protected] Excel Programming 0 September 19th 03 04:53 AM


All times are GMT +1. The time now is 01:31 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"