Subtract Ranges

Ornithopter

New Member
Joined
Jul 5, 2004
Messages
32
Is there a quick way to subtract ranges?

Example:
RangeA = A1:A20
RangeB = A1:A10

RangeC = subtract(RangeA, RangeB)

Now RangeC = A10:A20

Is there something like that?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
This is what I am trying... doesn't work.
The UsedRange is still larger than the PrintArea:

Code:
    'Remove anything that is not in the PrintArea
    Dim garbage As Range
    Set garbage = SubtractRanges.subractRanges(ActiveSheet.UsedRange, Range(ActiveSheet.PageSetup.PrintArea))
    garbage.Clear
    garbage.Delete
    
    ActiveWorkbook.Save
    ActiveSheet.UsedRange.Select
    Exit Sub

Thoughts?
 
Upvote 0
Maybe try this:

'Remove anything that is not in the PrintArea
Dim garbage As Range
Set garbage = SubtractRanges.subractRanges(ActiveSheet.UsedRange, Range(ActiveSheet.PageSetup.PrintArea))
garbage.Clear
garbage.Delete
Set garbage = Nothing

ActiveWorkbook.Save
ActiveSheet.UsedRange.Select
Exit Sub

HTH
 
Upvote 0
I have chosen to use the following instead of
the Flawed Used Range approach:

Code:
Function lastColumn(ws As Worksheet) As Long
      Dim findRange As Range

    Set findRange = ws.Cells.Find(what:="*", _
      SearchDirection:=xlPrevious, _
      SearchOrder:=xlByColumns)
   
    If findRange Is Nothing Then
        lastColumn = 0
    Else
        lastColumn = findRange.column
    End If
End Function

Function lastRow(ws As Worksheet) As Long
    Dim findRange As Range
   
    Set findRange = ws.Cells.Find(what:="*", _
      after:=ws.Range("A1"), _
      SearchDirection:=xlPrevious, _
      SearchOrder:=xlByRows)
    If findRange Is Nothing Then
        lastRow = 0
    Else
        lastRow = findRange.Row
    End If
End Function

Using the above 2 functions, which actually work,
I create the correct Range that I need.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,214,572
Messages
6,120,306
Members
448,955
Latest member
Dreamz high

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top