+ Reply to Thread
Results 1 to 8 of 8

Sumproduct with different ranges

Hybrid View

Kenny J Sumproduct with different... 11-05-2008, 07:52 PM
sglife Your written down sample... 11-05-2008, 08:13 PM
Kenny J Sumproduct with different... 11-06-2008, 01:46 AM
JBeaucaire These are just... 11-06-2008, 04:15 AM
Kenny J Sumproduct with different... 11-06-2008, 09:50 PM
Paul My suggestion is that you... 11-06-2008, 11:14 PM
Kenny J I stand corrected - my... 11-06-2008, 11:41 PM
Kenny J Solved it, kudos to me: ... 11-09-2008, 10:46 PM
  1. #1
    Registered User
    Join Date
    05-11-2008
    Posts
    7

    Sumproduct with different ranges

    Howdy

    I'm attempting to do a sumproduct with different dimensioned ranges. I've read multiple posts that say that all ranges in a sumproduct must have the same dimensions. I've tried different commands (index/match/lookup, etc) with no luck. Perhaps someone can come up with a solution for me.

    Here is the data:
    Sheet1: Sheet2:
    Widget Parts Location 1 Widget Count Parts
    A 4 A 2 20
    B 1 C 6
    C 2
    Location 2 Widget Count Parts
    B 3 3

    What I'm looking for is the total number of parts at that location, so here the answer would be 20 (2 x 4)+(6 x 2) = 8 + 12 = 20 for location 1, and 3 (3 x 1) = 3 for location 2.

    There are multiple locations and a lot more widgets than written here.

    Any ideas anyone ???

    Thanks

    KJ
    Last edited by VBA Noob; 11-10-2008 at 03:55 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    08-26-2008
    Location
    singapore
    Posts
    626
    Your written down sample confused me.
    Can you attach a dummy file?
    Which cell indicates the location?
    I need your support to add reputations if my solution works.


  3. #3
    Registered User
    Join Date
    05-11-2008
    Posts
    7

    Sumproduct with different ranges

    Ok, attached is a file of an example of what I'm trying to do. The yello highlight is where i want the answer (but this is not important), the blue is what the result should be, and the #N/A is the code I've been trying to get to work (ie: different range dimensions).

    Hope this helps to paint a clearer picture as to what is required.

    Thanks

    KJ
    Attached Files Attached Files

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492
    These are just straightforward VLOOKUP.
    Attached Files Attached Files
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  5. #5
    Registered User
    Join Date
    05-11-2008
    Posts
    7

    Sumproduct with different ranges

    Thanks JBeaucaire, this is one solution I thought of, but there are a few problems:
    1) The names of the widgets are not as nice as the simplistic ones I laid out in my question, they contain letters and numbers and not always in alphabetical order (which vlookup requires). I suppose I could sort the Widget list, however;
    2) the number of widgets at each location could be 10-20, making the formula quite long adding them up and also requires a change to formula if a widget is added to a location.

    This was the reason behind sumproduct.
    I apologise for steering you in the wrong direction with my simplistic data.
    Anyone other suggestions?

  6. #6
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887
    My suggestion is that you post a more realistic example of your data. Also, VLOOKUP does not require that the lookup range be in alphabetical order, and it can certainly find text strings with letters and numbers (and characters, too).

    Look in Excel Help for VLOOKUP, paying close attention to the 4th argument.

  7. #7
    Registered User
    Join Date
    05-11-2008
    Posts
    7
    Quote Originally Posted by pjoaquin View Post
    My suggestion is that you post a more realistic example of your data. Also, VLOOKUP does not require that the lookup range be in alphabetical order, and it can certainly find text strings with letters and numbers (and characters, too).

    Look in Excel Help for VLOOKUP, paying close attention to the 4th argument.
    I stand corrected - my apologies. I suppose the bigger issue is that vlookup can only take a single argument, not an array like sumproduct.

    Further to this, I have been attempting to increase the size of the array so that it matches the other using a function, however it's spitting out a #Value error.

    
    Function filled_array(ref() As Variant, count As Integer) As Variant
       Dim ref()
       Dim count As Integer
       Dim top As Integer
       
       top = UBound(ref, 1)
          
       ReDim ref(count)
       ReDim Preserve ref(count)
       
       For i = ref(top) To UBound(ref, 1)
           ref(i) = 0
       Next
       filled_array = ref
       
    End Function
    As an example, I would use the following call from excel:

    =sumproduct((filled_array(A1:A2,3))*(B1:B3))

    This would increase the first array to the same size as the second and therfore would be able to sumproduct them. If I can get this bit working then I can use the same technique to my original question, that is, sumproduct with different ranges.

  8. #8
    Registered User
    Join Date
    05-11-2008
    Posts
    7
    Solved it, kudos to me:

    Option Explicit
    
    Function fA(ref As Range, count As Integer) As Variant
       
        Dim i As Integer
        Dim top As Integer
        Dim vA() As Variant
        ReDim vA(count - 1)
       
        top = ref.Rows.count
             
        For i = 0 To top - 1
            vA(i) = ref.Cells(i + 1, 1)
        Next i
             
       fA = vA
       
    End Function
    Used as:

    =Sumproduct((fA(A1:A2,3)=(B1:B3))*(C1:C3))



    Works a treat.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1