Try this:
Option Explicit
Sub ConsolidateInfoFromSheets()
'JBeaucaire (5/7/2010)
'Search all sheets in a workbook for one account
'and merge rows into one summary sheet (stacked)
Dim cs As Worksheet, ws As Worksheet, wbData As Workbook
Dim LR As Long, NR As Long, AcctCol As Long, ShtNum As Long
Dim sName As Boolean, fClose As Boolean
Dim fName As String, AcctColStr As String, AcctNum As Double
Application.ScreenUpdating = False
'String that will identify the account numbers in workbook
'sheets without this text string will be skipped
AcctColStr = "Account Number"
'Prompt for an account number, activecell will be the default offered
AcctNum = Application.InputBox("Enter the Account Number to consolidate", _
"Account Number", IIf(IsNumeric(ActiveCell), ActiveCell, 0), Type:=1)
If AcctNum = 0 Then Exit Sub
'Prompt for the workbook to open
If MsgBox("Use the current activeworkbook?" & vbLf & vbLf & _
"YES - current workbook will be consolidated" & vbLf & _
"NO - you will be allowed to select a workbook", vbYesNo) = vbNo Then
fName = Application.GetOpenFilename("Microsoft Office Excel Files (.xls),.xls")
If fName = "False" Then Exit Sub
Set wbData = Workbooks.Open(fName)
fClose = True
End If
'Add consolidation sheet if needed
If Not Evaluate("ISREF(Consolidated!A1)") Then _
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Consolidated"
'Option to add sheet names to consolidation report
sName = MsgBox("Add sheet names to consolidation report?", vbYesNo + vbQuestion) = vbYes
'Setup
Set cs = Sheets("Consolidated")
cs.Cells.ClearContents
NR = 1
On Error GoTo Next1
'Process each data sheet in selected workbook
For ShtNum = Sheets.Count To 1 Step -1
Set ws = Sheets(ShtNum)
If ws.Name <> "Consolidated" And ws.Visible = True Then
AcctCol = ws.Cells.Find(AcctColStr, After:=ws.Range("A" & ws.Rows.Count), _
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False).Column
ws.Columns(AcctCol).AutoFilter Field:=1, Criteria1:=AcctNum
LR = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
If LR > 1 Then 'make sure there is at least one row to copy
'find range to copy
If NR = 1 Then 'copy with titles in row 1
ws.Range("A1:Z" & LR).Copy
Else 'copy without titles
ws.Range("A2:Z" & LR).Copy
End If
If sName Then 'paste and add sheet names if required
cs.Range("B" & NR).PasteSpecial xlPasteValues
cs.Range("A" & NR, cs.Range("B" & cs.Rows.Count).End(xlUp).Offset(0, -1)) = ws.Name
Else
cs.Range("A" & NR).PasteSpecial xlPasteValues
End If
NR = cs.Range("A" & cs.Rows.Count).End(xlUp).Row + 1
End If
ws.AutoFilterMode = False
End If
Next1:
Next ShtNum
'Cleanup and save
cs.Move
If fClose Then wbData.Close False
If sName Then [A1] = "Sheet"
Rows(1).Font.Bold = True
Cells.Columns.AutoFit
ActiveWorkbook.SaveAs AcctNum & Format(Date, " MM-DD-YY"), xlNormal
Application.ScreenUpdating = True
End Sub
Bookmarks