Results 1 to 9 of 9

how to use lookup to sum columns

Threaded View

  1. #1
    Registered User
    Join Date
    02-15-2010
    Location
    melbourne, Australia
    MS-Off Ver
    Excel 2003
    Posts
    9

    how to use lookup to sum columns

    Hi all,
    I have a VBA macro used in autocad which uploads data to an excel spread sheet.
    I was wondering if someone can help me.
    Column B contains a number which could be 1,2,3,4 etc
    I would like to be able to get VBA to lookup col B and for every instance of 1 then add the corresponding row data for columns F,H I J and place the sum for each at the side of the sheet. see attached file

    In Anticipation Thanks
    John B

    The VBA code from autocad which loads the spreadsheet is as below
    Option Explicit
    
    'Extracts attributes from Tendon ID bubble to excel
    
    Sub Pmark()
    Dim Excel As Object
    Dim ExcelSheet As Object
    Dim RowNum As Integer
    Dim Array1 As Variant
    Dim cnt As Integer
    Dim NumberOfAttributes As Integer
    Dim Ssnew As AcadSelectionSet
    Dim Sheet As Object
    Dim Max As Integer
    Dim Min As Integer
    Dim NoOfIndices As Integer
    Dim blkRef As AcadBlockReference
    Dim objAtt As AcadAttributeReference
    Dim objEnt As AcadEntity
    Dim x As Long
    ' Start Excel if not running
    On Error Resume Next
    Set Excel = GetObject(, "Excel.Application")
    If Err <> 0 Then
    Err.Clear
    Set Excel = CreateObject("Excel.Application")
    If Err <> 0 Then
    MsgBox "Could Not Load Excel!", vbExclamation
    End
    End If
    End If
    On Error GoTo 0
    Excel.Visible = True
    Excel.Workbooks.Add '............................
    Excel.Worksheets(1).Select
    
    Set ExcelSheet = Excel.ActiveSheet
    ExcelSheet.Name = "BOM"                               '<-- change sheet name to suit
    ''Clear the cells
    ExcelSheet.range("A12", "DZ100").Clear
    ExcelSheet.range("A12:O12").Font.Bold = True
    
    'Get Selection Set of Specific Block with Attributes
    RowNum = 12
    Dim Header As Boolean
    Header = False
    ' The following sets up a selection set from the user for all objects
    On Error Resume Next
    ' create set
    Set Ssnew = ThisDrawing.SelectionSets.Add("BOM")
    'if statement handles possible selection set error
    If Err.Number <> 0 Then
       Set Ssnew = ThisDrawing.SelectionSets.Item("BOM")
       Ssnew.Clear
    End If
    
    Dim GC(0 To 1) As Integer
    Dim GV(0 To 1) As Variant
    Dim atribs
    GC(0) = 0
    GV(0) = "INSERT"
    GC(1) = 2
    '---------------------------------------------------
    'Revise the block name "cable1" for your application
    GV(1) = "cable1"
    '---------------------------------------------------
    Ssnew.Select acSelectionSetAll, , , GC, GV
    For Each objEnt In Ssnew
       Set blkRef = objEnt
       Array1 = blkRef.GetAttributes
    For cnt = LBound(Array1) To UBound(Array1)
       Set objAtt = Array1(cnt)
       If Header = False Then
       ExcelSheet.Cells(RowNum, cnt + 1).Value = objAtt.TagString
    End If
    Next cnt
    RowNum = RowNum + 1
    For cnt = LBound(Array1) To UBound(Array1)
       Set objAtt = Array1(cnt)
       ExcelSheet.Cells(RowNum, cnt + 1).Value = objAtt.TextString
    Next cnt
    Header = True
    Next
    'some formatting goes here:
    With ExcelSheet.UsedRange
        .Columns.AutoFit
        Dim headRng As Excel.range
    Set headRng = .range(Cells(1, 1), Cells(1, UBound(Array1) + 1))
       With headRng
           .Borders.LineStyle = xlContinuous
           .Interior.ColorIndex = 35
           .Font.ColorIndex = 5
       End With
    Dim dataRng As Excel.range
        Set dataRng = .range(Cells(2, 1), Cells(.Rows.Count, UBound(Array1) + 1))
             dataRng.Select
        With Excel.Selection
            .Sort Key1:=range("B12"), Order1:=xlAscending, Key2:=range("A12") _
            , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
            False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
            :=xlSortNormal
            .Borders.LineStyle = xlContinuous
            .Font.ColorIndex = 9
            .Interior.ColorIndex = 34
    
    
    x = 4.2366
    
    Debug.Print (Round(x, 2))
    
        
        End With
    End With
    
    Set dataRng = Nothing
    Set headRng = Nothing
    Set ExcelSheet = Nothing
    Ssnew.Delete
    End Sub
    Attached Files Attached Files
    Last edited by vlady; 12-13-2016 at 09:37 PM. Reason: code tags added. 12/14/2016

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. How to lookup a value in a table using a range in the lookup columns
    By premis in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 02-03-2016, 02:47 PM
  2. Creating a three way lookup with columns and row lookup matches
    By jimbob23 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 12-19-2014, 10:58 PM
  3. Replies: 3
    Last Post: 04-08-2014, 03:11 AM
  4. [SOLVED] Multiple lookup adding columns, then multiplying, then lookup, add, and subtract
    By mamig in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-08-2013, 12:55 AM
  5. Lookup 2 columns and return value based on another 2 columns
    By rlsublime in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-13-2012, 11:16 AM
  6. Replies: 7
    Last Post: 06-19-2011, 12:51 PM
  7. Lookup adjacent column to multiple lookup columns.
    By JAMES4228 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-13-2009, 03:19 PM

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