How to Query Excel data and Insert Results in Userform Listbox?

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.

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)
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,215,603
Messages
6,125,776
Members
449,259
Latest member
rehanahmadawan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top