Hi, I've been working on this at work for the past 3 days and I cannot figure this out.
I have multiple sheets each containg the portfolio of certain accounts. I want to be able to search the securities that they hold and have it sorted by the accounts.
I have a drop down list in sheet1.
The macro will look at what is in the drop down list and search through the data in all of the sheets and copy and paste the relevant information on to sheet1.
PROBLEM:
Depending on the position of the sheet within the workbook, results are duplicated. For example,
sheet2 has B. Only Sheet2 has B. Sheet2 is before Sheet3,4,5,6.
Then when I search B, it will show B 5 times.
I think it is a problem with the loop i have but I don't know how to edit it.
I will post my code.
My boss is breathing down my neck. Any help would be greatly appreciated.
------------------------------------------------------------
I have multiple sheets each containg the portfolio of certain accounts. I want to be able to search the securities that they hold and have it sorted by the accounts.
I have a drop down list in sheet1.
The macro will look at what is in the drop down list and search through the data in all of the sheets and copy and paste the relevant information on to sheet1.
PROBLEM:
Depending on the position of the sheet within the workbook, results are duplicated. For example,
sheet2 has B. Only Sheet2 has B. Sheet2 is before Sheet3,4,5,6.
Then when I search B, it will show B 5 times.
I think it is a problem with the loop i have but I don't know how to edit it.
I will post my code.
My boss is breathing down my neck. Any help would be greatly appreciated.
------------------------------------------------------------
Code:
Sub Findholders()
'this macro is supposed to find the "Holders" for the "issuers" that we want to search for
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'after i run the macro, i want to hide the sheets. So to run the macro, i need to make the sheets visible again
Dim wsSheet As Worksheet
For Each wsSheet In ActiveWorkbook.Worksheets
wsSheet.Visible = xlSheetVisible
Next wsSheet
Dim WS1 As Worksheet
Dim WS2 As Worksheet
Dim rng As Range
Dim rng2 As Range
Dim j&
Dim f&
Set WS1 = Sheets(1)
'the following clears the previous search's results to prepare for the new search
Range("Holders!A6").Activate
Rows("6:6").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.clearcontents
'this is the loop that is causing problems
For j = 3 To Sheets.count
Set WS2 = Sheets(j)
WS2.Select
Range("A15").Autofilter Field:=3, Criteria1:="=" & WS1.Range("c3").Value
With WS2.Autofilter.Range
On Error Resume Next
' Set rng2 to rng without the header row
Set rng2 = .Offset(1, 0).Resize(.Rows.count - 1, .Columns.count) _
.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not rng2 Is Nothing Then
'Copy and paste the cells into WS2 below the existing data
rng2.Copy
With Sheets(1).Range("A" & LastRow(WS1) + 1)
' Paste:=8 will copy the columnwidth in Excel 2000 and higher
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
End If
End With
WS2.AutoFilterMode = False
Next j
'this formats the columns the way I want them to. this is not part of the problem
WS1.Activate
Range("P6").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Cut
Range("B6").Select
ActiveSheet.Paste
Range("B3").Select
End Sub
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(what:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlValues, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function