Results 1 to 10 of 11

Thread: Copying Data From One Range to Another By Averaging the Intersection

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    15
    Quote Originally Posted by SDruley View Post
    Dear Mr. Admin,

    Looking at this code has made me realize how little I really know about vba. It's like another world out there. Thank you so much for sharing your gift. It works perfectly.
    Let's see if we can "blow your mind" then. Here is a non-looping macro that I am pretty sure does what Admin's code does (although it asks you to pick or specify the ranges dynamically as opposed to making you select them first plus it does not require the two ranges to have the same number of rows)...
    Code:
    Sub AverageRanges()
      Dim FirstRow As Long, LastRow As Long
      Dim AddrA As String, AddrB As String
      Dim RngA As Range, RngB As Range, RowRng As Range
      Set RngA = Application.InputBox("Select Range A", Type:=8) 'Range("C6:F10")
      Set RngB = Application.InputBox("Select Range B", Type:=8) 'Range("I9:L13")
      FirstRow = RngA(1).Row
      LastRow = RngB(1).Offset(RngB.Rows.Count).Row - 1
      AddrA = Intersect(Rows(FirstRow & ":" & LastRow), Columns("C:F")).Address
      AddrB = Intersect(Rows(FirstRow & ":" & LastRow), Columns("I:L")).Address
      Range(AddrA) = Evaluate("IF(" & AddrA & "=""""," & AddrB & ",IF(" & AddrB & _
                              "=""""," & AddrA & ",(" & AddrA & "+" & AddrB & ")/2))")
      Range(AddrA).Replace 0, "", xlWhole
    End Sub
    Note: I should point out that the code assumes no data exists below the end of Range A or above the beginning of Range B. Hopefully that is how your data is set up (if not, let me know and I will see if I can modify the code to make it ignore data outside of the specified ranges).
    Last edited by Rick Rothstein; 11-27-2012 at 01:35 AM.

Similar Threads

  1. Intersection of Overlapping Ranges:Space Operator
    By Transformer in forum Tips, Tricks & Downloads (No Questions)
    Replies: 0
    Last Post: 05-17-2013, 12:32 AM
  2. Copying formulas while keeping formating
    By Bradh in forum Excel Help
    Replies: 1
    Last Post: 12-02-2012, 11:32 AM
  3. Combining data of Two Array or Range
    By princ_wns in forum Excel Help
    Replies: 5
    Last Post: 10-01-2012, 06:52 PM
  4. copying data from multiple workbooks into another
    By rahulcoolz99 in forum Excel Help
    Replies: 1
    Last Post: 08-22-2012, 09:19 PM
  5. Formatting Problem while copying data
    By princ_wns in forum Excel Help
    Replies: 3
    Last Post: 04-03-2012, 07:18 PM

Posting Permissions

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