First, I must apologize for the code below, I am just starting out with VBA so I'm sure it will look very rudimentary to you all.
I'm a financial analyst making a user-friendly dialog box that will help navigate through my database, which basically includes 130 companies. I would like my user to be able to select certain criteria (e.g. 15 < PE multiple > 5, Market Cap>$100m) on a combobox and have my workbook pop out a new worksheet with the companies that fit those criteria.
Here are the challenges/questions I am facing:
1a) Criteria for AdvancedFilter: Currently, the AdvancedFilter is using a Range("Criteria"
where Criteria is comprised of arbitrarily assigned variables. I would like to be able to adjust the criteria depending on what the user chooses in a dialog box (e.g. 5 variables all with different ranges). I'm sure there's a million ways to do this, but perhaps just being able to change the Range("Criteria"
would be a simple solution (although I do not know how to do this). Is there a general way to name a range that can vary in size?
1b) In general, what is the best way to select cells whose range varies? Is it possible to count the number of filled cells (e.g. NumberofCompanies = 27), and then substitute that number into a Range("4,NumberofCompanies"
?
2) When sorting the data, must I use a bubble-sort or the like? Is the sorting built-in function limiting in terms of changing the variables? If I know the total number of variables, why can I not plug that number into a Cell(C,1), select the whole thing and sort it?
Any kind of feedback would be appreciated. Please let me know if you need to see the whole file.
Regards,
Richard
Sub Search()
Worksheets("Sheet2"
.Select
Cells.Clear
Worksheets("Rank"
.Select
Call UsingAdvancedFilter ' Uses Advanced Filter function to search through the data
Range("B2
Q140"
.Copy ' Selecting results of search and pasting into new worksheet
Worksheets("Sheet2"
.Select
Range("B4"
.Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Call FindNumCompanies
Call SortAfterSearch
End Sub
Sub UsingAdvancedFilter()
'Copied this sub from somewhere but I cannot remember where, I apologize for not citing source
Dim l_wkbTest As Workbook
Dim l_wksSheetToFilter As Worksheet
Set l_wkbTest = ThisWorkbook
Set l_wksSheetToFilter = l_wkbTest.Sheets("Rank"
l_wksSheetToFilter.Range("DBList"
.AdvancedFilter xlFilterInPlace, Range("Criteria"
, , False
Set l_wksSheetToFilter = Nothing
Set l_wkbTest = Nothing
End Sub
Sub FindNumCompanies()
' Determining number of companies found in search
NumCompanies = 0
Do While Not IsEmpty(ActiveCell)
ActiveCell.Offset(1, 0).Select
NumCompanies = NumCompanies + 1
Loop
MsgBox "Number of companies " & NumCompanies
Worksheets("SearchOut"
.Range("a1"
.Value = NumCompanies
End Sub
Sub SortAfterSearch()
Rows("4:150"
.Select
Selection.Sort Key1:=Range("AS4"
, Order1:=xlDescending, Header:=xlNo _
, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Range("B4"
.Select
End Sub
I'm a financial analyst making a user-friendly dialog box that will help navigate through my database, which basically includes 130 companies. I would like my user to be able to select certain criteria (e.g. 15 < PE multiple > 5, Market Cap>$100m) on a combobox and have my workbook pop out a new worksheet with the companies that fit those criteria.
Here are the challenges/questions I am facing:
1a) Criteria for AdvancedFilter: Currently, the AdvancedFilter is using a Range("Criteria"
1b) In general, what is the best way to select cells whose range varies? Is it possible to count the number of filled cells (e.g. NumberofCompanies = 27), and then substitute that number into a Range("4,NumberofCompanies"
2) When sorting the data, must I use a bubble-sort or the like? Is the sorting built-in function limiting in terms of changing the variables? If I know the total number of variables, why can I not plug that number into a Cell(C,1), select the whole thing and sort it?
Any kind of feedback would be appreciated. Please let me know if you need to see the whole file.
Regards,
Richard
Sub Search()
Worksheets("Sheet2"
Cells.Clear
Worksheets("Rank"
Call UsingAdvancedFilter ' Uses Advanced Filter function to search through the data
Range("B2
Worksheets("Sheet2"
Range("B4"
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Call FindNumCompanies
Call SortAfterSearch
End Sub
Sub UsingAdvancedFilter()
'Copied this sub from somewhere but I cannot remember where, I apologize for not citing source
Dim l_wkbTest As Workbook
Dim l_wksSheetToFilter As Worksheet
Set l_wkbTest = ThisWorkbook
Set l_wksSheetToFilter = l_wkbTest.Sheets("Rank"
l_wksSheetToFilter.Range("DBList"
Set l_wksSheetToFilter = Nothing
Set l_wkbTest = Nothing
End Sub
Sub FindNumCompanies()
' Determining number of companies found in search
NumCompanies = 0
Do While Not IsEmpty(ActiveCell)
ActiveCell.Offset(1, 0).Select
NumCompanies = NumCompanies + 1
Loop
MsgBox "Number of companies " & NumCompanies
Worksheets("SearchOut"
End Sub
Sub SortAfterSearch()
Rows("4:150"
Selection.Sort Key1:=Range("AS4"
, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Range("B4"
End Sub