Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Intersect, Union... where's Deduct?

Hi everybody!
I wonder if anyone knows how to deduct range from other range. You can
add ranges (Union method) or have their intersection (Intersect method)
but I didn't find a way to do something like I think it should be done
- Deduct(Range("A:A"),Range("A1:A2")) which would result with the range
A3:A65536.
Can anyone help?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Intersect, Union... where's Deduct?

'-----------------------------------------------------------------
Function DeductRange(SetRange As Range, UsedRange As Range) As Range
'-----------------------------------------------------------------
Dim saveSet
saveSet = SetRange.Formula
SetRange.ClearContents
UsedRange = 0
Set DeductRange = SetRange.SpecialCells(xlCellTypeBlanks)
SetRange = saveSet
End Function

Sub testIt()
MsgBox DeductRange(Range("A:A"), Range("A1:A2")).Address
End Sub


--
HTH

Bob Phillips

"rumi" wrote in message
oups.com...
Hi everybody!
I wonder if anyone knows how to deduct range from other range. You can
add ranges (Union method) or have their intersection (Intersect method)
but I didn't find a way to do something like I think it should be done
- Deduct(Range("A:A"),Range("A1:A2")) which would result with the range
A3:A65536.
Can anyone help?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default Intersect, Union... where's Deduct?

Very nice code, just one minor warning. You save the contents - Delete the
contents - Get your range - then replace the contents. My issue is when you
replace the contents. (It is an uncommon error but it can happen). If you had
a cell formated as a number or general but populated with 1234 as a text
string, then when the contents are replaced Excel will do an implicit
conversion of the text and make it into a number.

That being said I am going to save this function for future use as it could
be really useful. Thanks Bob...
--
HTH...

Jim Thomlinson


"Bob Phillips" wrote:

'-----------------------------------------------------------------
Function DeductRange(SetRange As Range, UsedRange As Range) As Range
'-----------------------------------------------------------------
Dim saveSet
saveSet = SetRange.Formula
SetRange.ClearContents
UsedRange = 0
Set DeductRange = SetRange.SpecialCells(xlCellTypeBlanks)
SetRange = saveSet
End Function

Sub testIt()
MsgBox DeductRange(Range("A:A"), Range("A1:A2")).Address
End Sub


--
HTH

Bob Phillips

"rumi" wrote in message
oups.com...
Hi everybody!
I wonder if anyone knows how to deduct range from other range. You can
add ranges (Union method) or have their intersection (Intersect method)
but I didn't find a way to do something like I think it should be done
- Deduct(Range("A:A"),Range("A1:A2")) which would result with the range
A3:A65536.
Can anyone help?




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Intersect, Union... where's Deduct?

Note that this will give a run-time error if the activesheet is
unpopulated, or if nothing outside UsedRange is populated, i.e., if
UsedRange encompasses the Activesheet's used range. The OP also asked for

DeductRange(Range("A:A"), Range("A1:A2"))

to return A3:A65536 - in XL97 (IIRC) and MacXL, the macro only returns
cells in the used range.

It also gives a weird answer for this situation.

In a blank sheet, enter 1 in A1, 2 in A2.

Run

Public Sub testit()
MsgBox DeductRange(Range("A:A"), Range("A1:C1")).Address
End Sub

The MsgBox returns "$A$2"

This function isn't necessarily fast, but it will work in all versions:

Public Function DeductRange(rBase As Range, _
rDeduct As Range) As Range
Dim rIntersect As Range
Dim rCell As Range
Dim rBuild As Range
If rBase.Address = rDeduct.Address Then
Set DeductRange = Nothing
Else
Set rIntersect = Intersect(rBase, rDeduct)
If rIntersect Is Nothing Then
Set DeductRange = rBase
Else
For Each rCell In rBase
If Intersect(rCell, rDeduct) Is Nothing Then
If rBuild Is Nothing Then
Set rBuild = rCell
Else
Set rBuild = Union(rBuild, rCell)
End If
End If
Next rCell
Set DeductRange = rBuild
End If
End If
End Function

Since the OP gave only one-dimensional ranges, and so is a bit
ambiguous, this function may also be of interest as it returns the union
of all cells that aren't in the intersection of the two ranges:

Public Function AntiUnion(rng1 As Range, rng2 As Range) As Range
Dim rCell As Range
Dim rIntersect As Range
Dim rUnion As Range
Dim rBuild As Range
If rng1.Address = rng2.Address Then
Set AntiUnion = Nothing
Else
Set rUnion = Union(rng1, rng2)
Set rIntersect = Intersect(rng1, rng2)
If rIntersect Is Nothing Then
Set AntiUnion = rUnion
Else
For Each rCell In rUnion
If Intersect(rCell, rIntersect) Is Nothing Then
If rBuild Is Nothing Then
Set rBuild = rCell
Else
Set rBuild = Union(rBuild, rCell)
End If
End If
Next rCell
Set AntiUnion = rBuild
End If
End If
End Function

It could obviously be optimized for cells outside the used range, and it
could use some error checking to ensure that rng1 and rng2 are both on
the same worksheet.

In article ,
"Bob Phillips" wrote:

'-----------------------------------------------------------------
Function DeductRange(SetRange As Range, UsedRange As Range) As Range
'-----------------------------------------------------------------
Dim saveSet
saveSet = SetRange.Formula
SetRange.ClearContents
UsedRange = 0
Set DeductRange = SetRange.SpecialCells(xlCellTypeBlanks)
SetRange = saveSet
End Function

Sub testIt()
MsgBox DeductRange(Range("A:A"), Range("A1:A2")).Address
End Sub

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 477
Default Intersect, Union... where's Deduct?

This is my favorite Personal.xls Macro
Highlite Area 1, Hold down Control key and
Highlite Area2, then run:

Sub Difference()
If Selection.Areas.Count 2 Then
MsgBox "Only Two Areas can be Selected. Try Again"
Else
MsgBox "Difference is " & Format(Application.Sum(Selection.Areas(1))
- Application.Sum(Selection.Areas(2)), "#,###.00")
End If
End Sub

"rumi" wrote:

Hi everybody!
I wonder if anyone knows how to deduct range from other range. You can
add ranges (Union method) or have their intersection (Intersect method)
but I didn't find a way to do something like I think it should be done
- Deduct(Range("A:A"),Range("A1:A2")) which would result with the range
A3:A65536.
Can anyone help?




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 477
Default Intersect, Union... where's Deduct?

Obviously, I can't read;
My suggestion was totally OFF COURSE!!
Jim

"Jim May" wrote:

This is my favorite Personal.xls Macro
Highlite Area 1, Hold down Control key and
Highlite Area2, then run:

Sub Difference()
If Selection.Areas.Count 2 Then
MsgBox "Only Two Areas can be Selected. Try Again"
Else
MsgBox "Difference is " & Format(Application.Sum(Selection.Areas(1))
- Application.Sum(Selection.Areas(2)), "#,###.00")
End If
End Sub

"rumi" wrote:

Hi everybody!
I wonder if anyone knows how to deduct range from other range. You can
add ranges (Union method) or have their intersection (Intersect method)
but I didn't find a way to do something like I think it should be done
- Deduct(Range("A:A"),Range("A1:A2")) which would result with the range
A3:A65536.
Can anyone help?


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
HOW DO I DEDUCT A % EXCEL DISCOUNT FORMULA New Users to Excel 4 August 29th 08 12:10 AM
Complex Union Intersect [email protected] Excel Worksheet Functions 3 May 23rd 07 12:23 PM
Equivalent of Minus in Excel. Also Union, Intersect. KARL DEWEY Excel Worksheet Functions 0 January 27th 06 10:47 PM
sumproduct - deduct one from another. vipa2000 Excel Worksheet Functions 6 August 5th 05 09:48 PM
deduct tax from a total Deltaecho New Users to Excel 4 March 11th 05 07:24 PM


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