Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Newbie Q: Range from two cells

Hi All:

I'm pretty familiar with Word VBA and am now blundering around Excel. A
few successes so far. I find the trickiest thing so far is adapting
myself from Word's range object to Excel's.

An elementary question. I have two variables, representing two cells in
the same column. What is the easy way to return a range = to all the
cells between (and including) the two? So far my untutored brain can
only think of a complicated statement involving offsets and resizes.


Oh.. if there are any traps-for-young-players a newbie should know, I'd
appreciate it.

Thanks for your time,
Garry

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 414
Default Newbie Q: Range from two cells

Assuming the two variables you are refering to are defined as ranges
then maybe like this:

Sub test1()
Dim Cell1 As Range
Dim Cell2 As Range
Dim myRange As Range

Set Cell1 = Range("A10")
Set Cell2 = Range("A20")


Set myRange = Range(Cell1.Address & ":" & Cell2.Address)
Debug.Print myRange.Address

End Sub

Otherwise if you have variables representing row numbers then:

Sub test2()
Dim sRow As Long
Dim eRow As Long
Dim myRange As Range

sRow = 10
eRow = 20

Set myRange = Range(Cells(sRow, "A"), Cells(eRow, "A"))
Debug.Print myRange.Address

End Sub

Hope this helps
Rowan

Garry wrote:
Hi All:

I'm pretty familiar with Word VBA and am now blundering around Excel. A
few successes so far. I find the trickiest thing so far is adapting
myself from Word's range object to Excel's.

An elementary question. I have two variables, representing two cells in
the same column. What is the easy way to return a range = to all the
cells between (and including) the two? So far my untutored brain can
only think of a complicated statement involving offsets and resizes.


Oh.. if there are any traps-for-young-players a newbie should know, I'd
appreciate it.

Thanks for your time,
Garry

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Newbie Q: Range from two cells

Thanks for that Rowan. I'm a bit surprised that one converts things to
a string then back to a range. Is this a common technique in Excel?

Again, thanks for your response,
Garry

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Newbie Q: Range from two cells

Thanks for that Rowan. I'm a bit surprised that one converts things to
a string then back to a range. Is this a common technique in Excel?

Again, thanks for your response,
Garry

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 414
Default Newbie Q: Range from two cells

Hi Garry

If you refer to a range using the A1 notation then you would use a
string to define the address of that range e.g.
Range("A1").font.bold = true
So the methods I used to accomplish your requirements were the most
intuitive way to do this for me. There are other ways of doing it e.g.

Sub test3()
Dim CellA As Range
Dim CellB As Range
Dim myRange As Range

Set CellA = Range("A10")
Set CellB = Range("A20")

Set myRange = CellA.Resize(CellB.Row - CellA.Row + 1, 1)
Debug.Print myRange.Address

End Sub

More on working with ranges at:
http://j-walk.com/ss/excel/tips/tip20.htm

Regards
Rowan

Garry wrote:
Thanks for that Rowan. I'm a bit surprised that one converts things to
a string then back to a range. Is this a common technique in Excel?

Again, thanks for your response,
Garry



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default Newbie Q: Range from two cells

you don't need test strings or addresses.

Sub test()
Dim cell_A As Range
Dim cell_B As Range
Dim target As Range

Set cell_A = Cells(4, 5)
Set cell_B = Cells(12, 5)

Set target = Range(cell_A, cell_B)

target.Select

End Sub

"Garry" wrote:

Hi All:

I'm pretty familiar with Word VBA and am now blundering around Excel. A
few successes so far. I find the trickiest thing so far is adapting
myself from Word's range object to Excel's.

An elementary question. I have two variables, representing two cells in
the same column. What is the easy way to return a range = to all the
cells between (and including) the two? So far my untutored brain can
only think of a complicated statement involving offsets and resizes.


Oh.. if there are any traps-for-young-players a newbie should know, I'd
appreciate it.

Thanks for your time,
Garry


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
subscript out of range (newbie) Michael A Excel Programming 4 March 7th 05 01:28 AM
Looping through range newbie question Frank Rizzo Excel Programming 1 November 4th 04 12:09 AM
Newbie: Multiple Cells Range Dan Excel Programming 4 November 1st 04 03:44 AM
Newbie stuck on Passing a Variable into a Range Dave Baranas Excel Programming 0 August 13th 03 07:54 AM
Newbie stuck on Passing a Variable into a Range Bob Kilmer Excel Programming 0 August 13th 03 02:38 AM


All times are GMT +1. The time now is 08:22 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"