Thread: [Solved] HLOOKUP, if,? OR MACRO
View Single Post
 
Old 04-12-2011, 07:25 AM
macropod's Avatar
macropod macropod is offline Windows 7 32bit Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi Bobby,

Here's a commented version of the sub. These should help you understand what's going on.

The code doesn't look at the Weld ID at all. If you were to name your various worksheets with the Weld IDs, then another loop could be set up to go through those worksheets and, based on their names, put the output data in the corresponding columns on the 'summary' sheet. Or you could stick with what you've got and have the code look at the Weld IDs in Column A of each sheet. Either way, additional logic and looping will be required for the code to work with multiple tabs.
Code:
Sub Summarize()
'Disable ScreenUpdating for speed
Application.ScreenUpdating = False
'Define variables
Dim i As Long, j As Long, k As Long
Dim StrName As String, iTally As Long
'Start with the list of welders on Sheet2
With ThisWorkbook.Sheets("Sheet2")
  'Get each used row on Sheet2
  '.Range("A" & .Rows.Count).End(xlUp).Row locates the last row
  For i = 2 To .Range("A" & .Rows.Count).End(xlUp).Row
    'Store the welders' name
    StrName = .Range("A" & i).Value
    'Set the weld count to 0
    iTally = 0
    'Look in Sheet1
    With ThisWorkbook.Sheets("Sheet1")
    'Get each used row on Sheet1
    '.Range("A" & .Rows.Count).End(xlUp).Row  locates the last row
      For j = 2 To .Range("A" & .Rows.Count).End(xlUp).Row
        'Get each used column on Sheet1
        '.Cells.SpecialCells(xlCellTypeLastCell).Column  locates the last column
        For k = 2 To .Cells.SpecialCells(xlCellTypeLastCell).Column
          'Test whether the cell for the current row/column matches the welder's name
          If .Cells(j, k).Value = StrName Then
            'If it is, increment the tally, then skip the rest of this row
            iTally = iTally + 1
            Exit For
          End If
        Next k 'Check the next column for this row
      Next j 'Check the next row
    End With
    'update the welder's tally count
    .Range("B" & i).Value = iTally
  Next i 'Get the next welder
End With
'Restore ScreenUpdating
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote