LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Get smaller range from within another range using VBA

Not that this only works with simple ranges...

Sub test()
Dim rng As Range

Set rng = SubRange(Range("A1:A100"))
MsgBox rng.Address
Set rng = SubRange(Range("A1:A15"))
MsgBox rng.Address

End Sub


Public Function SubRange(ByVal Target As Range) As Range
Dim lng As Long

lng = 20
If Target.Count < 20 Then lng = Target.Count - 1

Set SubRange = Range(Target.Cells(Target.Count - lng),
Target.Cells(Target.Count))

End Function

--
HTH...

Jim Thomlinson


"Craig" wrote:

Hello,

I am new to VBA in Excel, although I have some experience. I am trying to
write a function that asks the user to input a range of cells. From that
range of cells, I want to extract the last 20 cells in the range (or all the
cells, if the range is smaller than 20 cells) and use that 20-cell subrange
to perform various calculations.

E.G.

Function Example (Input As Range) As Variant

Dim subrange as Range

??

End Function

My two questions a

1. Is there a property of the Range object that can tell you how many cells
are in it?

2. Once I know how many cells are in it, what is the best syntax to extract
the last 20 cells in Input (above) and assign that range to subrange (above)?
I would then use subrange to perform the necessary calculations.

Any help is greatly appreciated!

Thanks,
Craig

 
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
Distribute values over larger (smaller) range AK Excel Discussion (Misc queries) 0 December 9th 09 05:45 PM
How do I enter formula sum(range+range)*0.15 sumif(range=3) tkw Excel Discussion (Misc queries) 2 October 1st 09 09:17 PM
Excel Addin:Setting the range to the Excel.Range object range prop Rp007 Excel Worksheet Functions 5 November 24th 06 04:30 PM
Range.Find returns cell outside of range when range set to single cell Frank Jones Excel Programming 12 June 10th 04 04:22 AM
how to? set my range= my UDF argument (range vs. value in range) [advanced?] Keith R[_3_] Excel Programming 2 August 11th 03 05:55 PM


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