+ Reply to Thread
Results 1 to 4 of 4

Vlookup Looping through columns and rows returning sum in total column

Hybrid View

  1. #1
    Registered User
    Join Date
    08-07-2013
    Location
    Monroeville, Pa
    MS-Off Ver
    Excel 2010
    Posts
    22

    Vlookup Looping through columns and rows returning sum in total column

    First, I would like to thank anyone who can possibly assist me with the VBA for this spreadsheet.

    The worksheet consists of 10 columns (A-J) and 13 rows (1-13). The first row is a header row that contains eight codes that I need to look up the value in a table and multiple it by the count in each of the cells below. The result of the vlookup for each column needs to be added together and return a total in column J. I have a formlua that works well, however, I would like to convert the formula to VBA and For Each loops confuse me every time. The formula is:

    =SUM(VLOOKUP(B$1,$P$2:$Q$9,2,FALSE)*B2,VLOOKUP(C$1,$P$2:$Q$9,2,FALSE)*C2,VLOOKUP(D$1,$P$2:$Q$9,2,FALSE)*D2,VLOOKUP(E$1,$P$2:$Q$9,2,FALSE)*E2,VLOOKUP(F$1,$P$2:$Q$9,2,FALSE)*F2,VLOOKUP(G$1,$P$2:$Q$9,2,FALSE)*G2,VLOOKUP(H$1,$P$2:$Q$9,2,FALSE)*H2,VLOOKUP(I$1,$P$2:$Q$9,2,FALSE)*I2)
    A sample workbook is attached.

    Any help is greatly appriciated.

    Thank you,
    Eric
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,274

    Re: Vlookup Looping through columns and rows returning sum in total column

    Here are two macros - one using your formula, the other using a loop:

    Sub Macro1()
       With Range("J2:J13")
        .FormulaR1C1 = "=SUM(" & _
        "VLOOKUP(R1C[-8],R2C16:R9C17,2,FALSE)*RC[-8]," & _
        "VLOOKUP(R1C[-7],R2C16:R9C17,2,FALSE)*RC[-7]," & _
        "VLOOKUP(R1C[-6],R2C16:R9C17,2,FALSE)*RC[-6]," & _
        "VLOOKUP(R1C[-5],R2C16:R9C17,2,FALSE)*RC[-5]," & _
        "VLOOKUP(R1C[-4],R2C16:R9C17,2,FALSE)*RC[-4]," & _
        "VLOOKUP(R1C[-3],R2C16:R9C17,2,FALSE)*RC[-3]," & _
        "VLOOKUP(R1C[-2],R2C16:R9C17,2,FALSE)*RC[-2]," & _
        "VLOOKUP(R1C[-1],R2C16:R9C17,2,FALSE)*RC[-1])"
        .Value = .Value
        End With
    End Sub
    
    Sub Macro2()
        Dim c As Range
        Dim lngC As Long
        Dim lngR As Long
        Dim temp As Double
        
        For Each c In Range("J2:J13")
            temp = 0
            For lngC = 2 To 9
                temp = temp + Application.VLookup(Cells(1, lngC).Value, Range("P2:Q9"), 2, False) * Cells(c.Row, lngC).Value
            Next lngC
            c.Value = temp
        Next
        
    End Sub
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    08-07-2013
    Location
    Monroeville, Pa
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: Vlookup Looping through columns and rows returning sum in total column

    Macro 2 is perfect, thank you! I really appriciate your help.

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,274

    Re: Vlookup Looping through columns and rows returning sum in total column

    Great! Happy that you liked it...

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Looping through rows, finding 2 matching columns, returning the 3rd column as a variable
    By orionanomaly in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-27-2018, 09:36 AM
  2. Looping through rows then columns
    By shaggyjh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-10-2018, 05:31 AM
  3. [SOLVED] Checking column for values and returning a running total.
    By Anarza in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-11-2017, 05:53 AM
  4. [SOLVED] Vlookup column change. columns to rows
    By b19upj in forum Excel General
    Replies: 5
    Last Post: 04-06-2016, 11:16 AM
  5. Looping Through Columns And Rows
    By slimjm911 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-08-2012, 08:41 PM
  6. Replies: 4
    Last Post: 01-03-2012, 12:00 PM
  7. [SOLVED] Looping through Columns then Rows
    By JCP in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-30-2005, 12:27 AM

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