+ Reply to Thread
Results 1 to 12 of 12

sumif issue

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-05-2006
    Location
    Canada
    MS-Off Ver
    2003, 2007
    Posts
    581
    You can try this macro.

    Basically, it loops through all your sheets to gather data (excluding the one specified in the SUMMARY_NAME constant.

    It then puts the summary information in the SUMMARY_NAME sheet.

    It makes a couple of assumptions:

    First, that your employee ids are all numeric and below 50,000. You can adjust this upward, but it won't work with non-numeric employee ids.

    Second, that your description for being done is "complete".

    Third, the sheet you want to skip is "summary".

    Fourth, the columns listed in the example are the columns that you are actually using.

    Plus, some other little things in comments.

    Option Explicit
    
    Const MAX_EMPLOYEE_ID = 50000
    Const DESC_COMPLETE = "complete"
    Const SUMMARY_NAME = "summary"
    
    Public Sub Summarize()
      Dim WS As Worksheet
      Dim Start As Long
      Dim Finish As Long
      Dim Data(MAX_EMPLOYEE_ID, 1) As Long
      Dim i As Long
      Dim j As Long
      Dim current As Long
    
      'Gathering data... this goes through all sheets except the SUMMARY
      'sheet.
      For Each WS In Worksheets()
        If WS.Name <> SUMMARY_NAME Then
          Start = 2   'Assuming each sheet's data starts on Row 2
          Finish = WS.Range("A65536").End(xlUp).Row
              
          For i = Start To Finish
            With WS
              current = .Cells(i, 1)
              If (.Cells(i, 3) = DESC_COMPLETE) Then
                Data(current, 1) = Data(current, 1) + .Cells(i, 4)
              End If
              Data(current, 0) = Data(current, 0) + .Cells(i, 4)
            End With
          Next i
        End If
      Next
    
      'This puts the data in the SUMMARY sheet
      'For reference:
      '  i represents the row in our Data array.
      '  j represents the row in output, starting with row 2.
      j = 2
      For i = 0 To MAX_EMPLOYEE_ID
        If (Data(i, 0) <> 0 Or Data(i, 1) <> 0) Then
          With Worksheets(SUMMARY_NAME)
            .Cells(j, 1) = i
            .Cells(j, 2) = Data(i, 0)
            .Cells(j, 3) = Data(i, 1)
          End With
          j = j + 1
        End If
      Next i
    End Sub
    Scott

  2. #2
    Registered User
    Join Date
    03-17-2006
    MS-Off Ver
    2007
    Posts
    93
    your assumptions are all correct - could you possibly attach the workbook with the macro in it? I'm much better at tearing them down and figuring it out than I am at building from scratch...
    Better to be roughly right than exactly wrong, unless you are using Excel.

  3. #3
    Forum Contributor
    Join Date
    07-05-2006
    Location
    Canada
    MS-Off Ver
    2003, 2007
    Posts
    581
    Ok, here you go.

    Just to ramble on, to use the code above, you're probably better to just copy it into your spreadsheet with all your data as opposed to copying all the data, although that is just a personal opinion. (I would also make a backup, in case things get screwed up. Full of confidence, aren't I?)

    All you have to do is go to VBA (Alt-F11), on the side, select the Workbook you want to put the macro in. Then choose Insert -> Module (Alt I - M), then paste the code in that module.

    Then you can run it with Alt-F8 and choose the procedure from the list.

    Scott
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    03-17-2006
    MS-Off Ver
    2007
    Posts
    93
    Quote Originally Posted by Maistrye
    (I would also make a backup, in case things get screwed up. Full of confidence, aren't I?)
    preachin' to the choir...
    My work book is actual fairly large with lots of other stuff. I want to put the macro in and start plyaing with the code to see how it works. Kinda like tearing apart a motor and putting back together (hopefully) correctly.


    All you have to do is go to VBA (Alt-F11), on the side, select the Workbook you want to put the macro in. Then choose Insert -> Module (Alt I - M), then paste the code in that module.

    Then you can run it with Alt-F8 and choose the procedure from the list.
    Went over my head pretty quick.

    got steps #1, #2, and part of #3 but when I get to the M part, the option is greyed out. Do I need to create a clean module somehow in the actual workbook I'm trying to paste in? If I can get to that point, I'll try the alt F8 part...

  5. #5
    Valued Forum Contributor
    Join Date
    07-11-2004
    Posts
    851
    another approach to gather all the tech #'s - not required to be numeric

    Sub macro1()
    Dim sht As Worksheet
    Dim tech(100) As String
    i = 0
    For Each sht In ActiveWorkbook.Worksheets
    sht.Activate
    If sht.Name = "summary" Then GoTo done
    'If sht = "Summary" Then GoTo done
    firstrow = sht.Cells(1, 1).End(xlDown).Row
    lastrow = sht.Cells(65000, 1).End(xlUp).Row
    If lastrow = firstrow Then firstrow = 1
    For j = firstrow To lastrow
    If i = 0 Then GoTo firstone
    For k = 1 To i
    If sht.Cells(j, 1) = tech(k) Then GoTo nextj
    Next k
    firstone:
    i = i + 1
    tech(i) = sht.Cells(j, 1).Value
    nextj:
    Next j
    Next sht
    done:
    Sheets("Summary").Select
    For m = 1 To i
    Cells(m + 1, 1) = tech(m)
    Next m
    End Sub
    not a professional, just trying to assist.....

  6. #6
    Forum Contributor
    Join Date
    07-05-2006
    Location
    Canada
    MS-Off Ver
    2003, 2007
    Posts
    581
    Playing with it is very understandable.

    Here's one site, not much, but a little bit on what I was trying to show:

    http://www.contextures.com/xlvba01.html

    Here's another with a couple more screenshots. (Only the first bit applies)

    http://www.fontstuff.com/vba/vbatut03.htm

    I guess the key point is that when you hit Alt-F11, you have to select the name of the spreadsheet on the left side listing. (like they show in the screenshots on these sites)

    (It's so much easier to show someone in person... and once you see it, it's fairly easy. The explanation in words is the hard part.)

    With the copy I attached, you should be able to run it, provided your Macro Security is set to Medium. (Low is baddddd.... High won't allow you to run macros in general.) (Tools -> Options ... choose the Security tab, click the Macro Security button, change it to Medium.)

    Scott

  7. #7
    Registered User
    Join Date
    03-17-2006
    MS-Off Ver
    2007
    Posts
    93

    cool....

    I'll check these out over the next couple days. Keep an eye out for htis thread, I'm sure I'll have questions.

    Macro setting is medium already.

+ 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