Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
DaveP
 
Posts: n/a
Default Range used in a macro needs to be variable

I want to highlight a range by starting in the top left hand corner(fixed),
doing an 'end' 'down' and then right 6, because the number of rows is
variable each time, can this be down in a macro. It seems the macro records
the actual cell references it found whilst recording the macro and therefore
only works the first time.

I read the article under Making Excel Macros where you can use the Variables
DIM and SET to name the range, but it relies on the user highlighting the
selection before running the macro.

I am using Excel 2000 and 2003.

Thanks
  #2   Report Post  
Bob Phillips
 
Posts: n/a
Default Range used in a macro needs to be variable

Dave,

Dim rng As Range
Set rng = Range(Selection, Selection.End(xlDown)).Resize(, 6).Select

works assuming there is a selected range to start with.

If you want to use a static start point, then replace both Selection by say
Range("H1").

The 6 can be replaced by a variable with a number in it.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"DaveP" wrote in message
...
I want to highlight a range by starting in the top left hand

corner(fixed),
doing an 'end' 'down' and then right 6, because the number of rows is
variable each time, can this be down in a macro. It seems the macro

records
the actual cell references it found whilst recording the macro and

therefore
only works the first time.

I read the article under Making Excel Macros where you can use the

Variables
DIM and SET to name the range, but it relies on the user highlighting the
selection before running the macro.

I am using Excel 2000 and 2003.

Thanks



  #3   Report Post  
DaveP
 
Posts: n/a
Default Range used in a macro needs to be variable

Hi Bob

Sounds like just the thing, but I must be doing something wrong now. I am
trying to use your second suggestion of replacing the 2 Selection by
Range("A3") like below, and then tries to sort the range 'myrange'

Dim myRange As Range
Set myRange = Range(Range("A3"), Range("A3").End(xlDown)).Resize(,
6).Select
myRange.Sort Key1:=Range("A4"), Order1:=xlAscending,
Key2:=Range("D4") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom

but get Run-Time Error '424' Object Required.


Dave


"Bob Phillips" wrote:

Dave,

Dim rng As Range
Set rng = Range(Selection, Selection.End(xlDown)).Resize(, 6).Select

works assuming there is a selected range to start with.

If you want to use a static start point, then replace both Selection by say
Range("H1").

The 6 can be replaced by a variable with a number in it.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"DaveP" wrote in message
...
I want to highlight a range by starting in the top left hand

corner(fixed),
doing an 'end' 'down' and then right 6, because the number of rows is
variable each time, can this be down in a macro. It seems the macro

records
the actual cell references it found whilst recording the macro and

therefore
only works the first time.

I read the article under Making Excel Macros where you can use the

Variables
DIM and SET to name the range, but it relies on the user highlighting the
selection before running the macro.

I am using Excel 2000 and 2003.

Thanks




  #4   Report Post  
Bob Phillips
 
Posts: n/a
Default Range used in a macro needs to be variable

Sorry Dave, I made an error in changing my test code to that which I posted.
You don't set and Select.

This should work for you

Dim myRange As Range
Set myRange = Range(Range("A3"), Range("A3").End(xlDown)) _
.Resize(, 6)
myRange.Sort Key1:=Range("A4"), _
Order1:=xlAscending, _
Key2:=Range("D4"), _
Order2:=xlAscending, _
Header:=xlGuess, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom

--

HTH

RP
(remove nothere from the email address if mailing direct)


"DaveP" wrote in message
...
Hi Bob

Sounds like just the thing, but I must be doing something wrong now. I am
trying to use your second suggestion of replacing the 2 Selection by
Range("A3") like below, and then tries to sort the range 'myrange'

Dim myRange As Range
Set myRange = Range(Range("A3"), Range("A3").End(xlDown)).Resize(,
6).Select
myRange.Sort Key1:=Range("A4"), Order1:=xlAscending,
Key2:=Range("D4") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1,

MatchCase:= _
False, Orientation:=xlTopToBottom

but get Run-Time Error '424' Object Required.


Dave


"Bob Phillips" wrote:

Dave,

Dim rng As Range
Set rng = Range(Selection, Selection.End(xlDown)).Resize(, 6).Select

works assuming there is a selected range to start with.

If you want to use a static start point, then replace both Selection by

say
Range("H1").

The 6 can be replaced by a variable with a number in it.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"DaveP" wrote in message
...
I want to highlight a range by starting in the top left hand

corner(fixed),
doing an 'end' 'down' and then right 6, because the number of rows is
variable each time, can this be down in a macro. It seems the macro

records
the actual cell references it found whilst recording the macro and

therefore
only works the first time.

I read the article under Making Excel Macros where you can use the

Variables
DIM and SET to name the range, but it relies on the user highlighting

the
selection before running the macro.

I am using Excel 2000 and 2003.

Thanks






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
Locating variable range to copy Eric C New Users to Excel 3 August 12th 05 10:23 AM
Summing a variable range of columns Richard Buttrey Excel Worksheet Functions 9 July 28th 05 06:52 PM
How do I set a date range for conditional formatting in a macro? billo Excel Worksheet Functions 3 February 7th 05 06:19 PM
empty variable range S1 Excel Discussion (Misc queries) 5 January 28th 05 02:33 PM
Macro for Show/Hide Range Bill Excel Discussion (Misc queries) 3 December 1st 04 05:33 PM


All times are GMT +1. The time now is 06:46 PM.

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"