Consulting

Results 1 to 5 of 5

Thread: intersect function

  1. #1
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    intersect function

    hello
    i want excel to sum cells of two ranges ,only if they intersect.
    if the do not i want excel to present a msgbox
    [VBA]
    Dim isect
    Dim rng1 As Range
    Dim rng2 As Range
    With ActiveSheet
    rng1 = Application.InputBox("select first range", Type:=8)
    rng2 = Application.InputBox("select second range", Type:=8)
    Set isect = Application.intersect(Range("rg1"), Range("rg2"))
    If isect Is Nothing Then
    MsgBox "Ranges do not intersect"
    End If
    End With
    ActiveCell.FormulaArray = "=Application.WorksheetFunction.Sum(rng1, rng2))"

    [/VBA]
    thanks
    moshe

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Do you mean the sum of the cells in the intersection, or all cells of the union of the ranges?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location
    hello
    the sum of the intersection.
    thanks
    moshe

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Dim isect
    Dim rng1 As Range
    Dim rng2 As Range
    With ActiveSheet
    Set rng1 = Application.InputBox("select first range", Type:=8)
    Set rng2 = Application.InputBox("select second range", Type:=8)
    Set isect = Application.Intersect(rng1, rng2)
    If isect Is Nothing Then
    MsgBox "Ranges do not intersect"
    End If
    End With
    If isect Is Nothing Then
    MsgBox "Ranges do not intersect"
    Else
    ActiveCell.Formula = "=SUM(" & isect.Address & ")"
    End If
    [/vba]

  5. #5
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Lior

    Why are you using FormulaArray to try and put an Application.WorksheetFunction in a cell?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •