jonsnowball
New Member
- Joined
- Jun 1, 2016
- Messages
- 1
I am trying to build a userform that contains a listbox in it. I want to populate this listbox with data from a sheet called "DataSheet". Columns 1 through 7 are Farm Name, Crop, Date, Bushels, Price, Revenue, and Contract Number. Right now I also have two comboboxes, that when updated, post their values to cells J2 & J3, and then I use an autofilter, and only grab the visible results. The code for this can be seen below.
The problem I am having with this is I need to include column number 10 in the listbox but cannot without hiding column 8 and 9. I am wondering if there is a way I can base a query off of the value in combobox A, which is farm selection, (eg: Old McDonalds Farm) and then the value in combobox B, crop selection, (eg: CORN), and then my listbox would just show rows of data where the farm name is "Old McDonalds Farm" and the crop is "CORN".....along with just specific columns (perhaps 1-7 and 10)
Code:
Sub FilterMacro()
Sheets("DataSheet").Select
Range("A1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$F$15").AutoFilter field:=1, Criteria1:=Sheet4.Range("J2").Value
ActiveSheet.Range("$A$1:$F$15").AutoFilter field:=2, Criteria1:=Sheet4.Range("J3").Value
Dim c As Integer
Dim R As Integer
Dim MyArray(50, 7)
UserForm2.lstPreviousSales.ColumnCount = 7
c = 0
R = 0
'Load values MyArray - 7 columns
For Each Cell In Sheets("DataSheet").Range("A1:G50").SpecialCells(xlCellTypeVisible)
MyArray(R, c) = Cell
c = c + 1
If c = 7 Then
c = 0
R = R + 1
End If
Next Cell
'Load ListBox1
UserForm2.lstPreviousSales.List() = MyArray
Sheets("DataSheet").Select
Selection.AutoFilter
End Sub
The problem I am having with this is I need to include column number 10 in the listbox but cannot without hiding column 8 and 9. I am wondering if there is a way I can base a query off of the value in combobox A, which is farm selection, (eg: Old McDonalds Farm) and then the value in combobox B, crop selection, (eg: CORN), and then my listbox would just show rows of data where the farm name is "Old McDonalds Farm" and the crop is "CORN".....along with just specific columns (perhaps 1-7 and 10)