Find Match in another WB and return detailed sheet
I might need more info from you to get this right, but I'll take a shot at it:
Workbooks:
WB2 - project summary workbook
WBwip - details of expenditures
Routines:
BuildReport - the main routine (loops through a range of projects)
GetExpenditure- finds the details and copies to WB2
Flow:
The BuildReport routine will loop through a range of cells (set to A1-A10)
and, using the GetExpenditure routine, look at the WBwip book for the
matching project and expenditure sheet and copy that sheet to WB2.
I had to assume some things about how you would want this to work (like
the WBwip book already being opened and that col J has the name of the detail
sheet), so I could be wrong about the approach.
Hopefully this will help a little though:
Sub BuildReport()
'Not quite sure what type of range your looking at
'named range? the whole column?, specific cell range?
'I used a specific cell range (only the ones with values)
For Each c In Range("A1", "A10").SpecialCells(xlCellTypeConstants)
GetExpenditure Left(c.Value, 6)
Next
Worksheets("WB2").Select
End Sub
Sub GetExpenditure(strProject As String)
Dim lngRow As Long
lngRow = -1
With Workbooks("WBwip.xls").Worksheets("WBwip").Columns (1)
On Error Resume Next
'Find the project row based on column A
lngRow = .Find( _
What:=strProject, _
After:=.Range("A1"), _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
SearchFormat:=False _
).Row - 1
On Error GoTo 0
'Process only if it found the project
If lngRow < -1 Then
Dim strExpenditure As String
strExpenditure = .Range("J1").Offset(lngRow, 0)
'Copy to WB2
With Workbooks("WBwip.xls").Worksheets(strExpenditure)
.Copy
After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksh eets.Count)
End With
End If
End With
End Sub
"GregR" wrote:
WB2 is a workbook that has projects listed in Column A. The
project identifier is actually the left(6) characters. WBwip is a pivot
table that has those same projects listed in column A with total
expenditure amount
listed in Column (J). What I want is to match the project in WB2 with
WBwip in Column A, then offset that found cell to Column (J), the
expenditure column, and display the detail of that expenditure, which
actually adds a sheet to WBwip. Then move that detail sheet to WB2. As
an example WB2 identifies A7 = 06-013, the result 06-013 is used to
match the project in WBwip. Once it finds the matching 06-013, it
offsets to the total expenditure column and displays the detailed
results of that expenditure and moves that detail sheet to WB2. Once it
does that, it loops through the rest of projects in WB2 and does that
until all projects have been added to WB2. The expected finished result
is WB2
has the initial project sheet with additional detailed expenditure
sheets for each project. Any help would be appreciated. TIA
Greg
|