For the sake of ease I'll get right to the thick of it!
What I have is;
2 sheets, start and data.
- On data i have my sourcedata (surprise!), the first five columns are "hierachical" levels, ranging from country, to region to area and all the way down to individual branch - the rest of the columns (6-33) are just data.
- On start I have 5 comboboxes, 28 checkboxes and one commandbutton
What I wanted to happen was;
In Combobox1 I wanted a listing of the countries (column A in data). Then combobox2 should show all off the regions (column A) that is in the country chosen with combobox1. And so forth with the rest of them.
Finally the user should check of which information from data (columns 6 - 33), he wants to include in his table, then press the commandbutton and the specific report should be generated (by copying the selection from the data sheet onto a new sheet).
My Problem is;
1 - I'm a complete rookie when it comes to comboboxes - I only know additem (which I will then need to state manually everytime it changes) and then I just found listfillrange - the problem with this is it doesnt filter the list shown - so I have Ireland showing up some 2000 times!
2 - I don't know how to fill the comboboxes 2-5, as stated previously. I guess what I need is something like autofilter - but I don't know if it can be controlled from VBA?
Here's a sub and a sample Workbook that will populate a ComboBox based on a Column number - adding only unique instances of each value.
The sub will, optionally, filter the values to add, based on a passed parameter.
There is one caveat with this. If you have the same child value under multiple parents, the filter for the children of that child value will not take the parent value into consideration. For example in my demo WB, if both Ireland and USA had a child called "South", the filtering of combobox3 by combobox2 would not work correctly. This could be corrected fairly easily,.
VB Code:
Option Explicit
Sub Populate_Box(ByRef MyCombo As ComboBox, ByVal ColNum As Long, Optional ByVal ParentVal As String)
Dim lRowCount As Long
Dim rngData As Range
Dim lRowNum As Long
Dim sEntry As String
Dim bLoaded As Boolean
Dim lIndex As Long
'Remove all existing values
'from the combobox
MyCombo.Clear
'Set the input range for the CB
With Worksheets("Data")
lRowCount = .UsedRange.Rows.Count
Set rngData = .Range(.Cells(2, ColNum), .Cells(lRowCount, ColNum))
End With
'Loop through all rows in the input range
For lRowNum = 1 To rngData.Rows.Count
'I have assumed that the Parent (Filtering Value)
'is in the next column to the left
'If there is no filter..
If ParentVal = "" Then
'..store the value
sEntry = rngData.Cells(lRowNum, 1).Value
'..Otherwise...
Else
'...Only store the value if the Filter Value
'...if the preceding column has the parent value
If rngData.Cells(lRowNum, 1).Offset(0, -1).Value = ParentVal Then
sEntry = rngData.Cells(lRowNum, 1).Value
End If
End If
'Assume that the value has not been loaded
'into the CB
bLoaded = False
'Only continue if we have a value to load
If sEntry <> "" Then
'Loop through the current values in the CB
For lIndex = 0 To UBound(MyCombo.List)
'If the value is already in the CB - record this fact
If MyCombo.List(lIndex) = sEntry Then
bLoaded = True
Exit For
End If
Next lIndex
'If the value is not in the CB, Add it
If Not bLoaded Then
MyCombo.AddItem sEntry
End If
End If
Next lRowNum
End Sub
Last edited by DKenny; Jun 7th, 2006 at 08:52 AM.
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful
Thanx for taking time to help me out here - I'm curently going with your idea Declan, but a few questions have arrived:
I tried runnning your worksheet and it works splendidly, however when I try to implement it into my own workbook, I'm getting an error (Unspecified error)at the following line;
VB Code:
'Remove all existing values
'from the combobox
MyCombo.Clear
I noticed that on your sheet, the comboboxes are on the data sheet, while at mine it is on the start sheet - is this causing the error? I can't find any sheet references though except for the setrange for the input?
Last edited by direktoren; Jun 8th, 2006 at 02:17 AM.
Hummm.
I 'm not sure what error you are getting or why. Here's a revised copy of the sample book with an extra sheet added and the CB's moved to that sheet and the code still works for me. Can you test this one?
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful
I also got yours to function - don't know what is wrong with mine, I can't find any differences, but then again, my eye probably isn't as trained as yours!
hmm... tried running it at my pc at home - now i got a new error!
I've uploaded my workbokk, hoping that you might want to take a look - I've had to use some random data in the data sheet, since the other data is categorized as classified - but it shouldn't matter what names ot numbers are in the data sheet i guess.
OK, I have found the problem. You have specified a ListFillRange for each of your Comboboxes. Removing these values will solve the problem.
Also I would suggest changing hte line where we set the lRowCount variable to the following. The UsedRange is unrelaible and I never should have included it.
VB Code:
lRowCount = .Range("A1").End(xlDown).Row
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful
Okay - thanx...It makes sense since I started out by using the listfillrange! I will have a look at tomorrow when I go to work - hopefully that should solve my problems!
One other problem however, which I was hoping that you could might help me with... Its in my final copy function:
VB Code:
Private Sub CommandButton1_Click()
Dim x As Long
Dim i As Long
Dim oControl As OLEObject
Dim dSheet As Worksheet
Dim sSheet As Worksheet
Set dSheet = ActiveWorkbook.Worksheets("Data")
Set sSheet = ActiveWorkbook.Worksheets("Start")
dSheet.Copy after:=Sheets(1)
If Not ComboBox1.Value = "" Then
For i = 2 To Sheet3.Cells(2, 1).End(xlDown).Row
If Not Sheet3.Cells(i, 1).Value = ComboBox1.Value Then
Sheet3.Rows(i).EntireRow.Delete
End If
Next i
If Not ComboBox2.Value = "" Then
For i = 2 To Sheet3.Cells(2, 1).End(xlDown).Row
If Not Sheet3.Cells(i, 2).Value = ComboBox2.Value Then
Sheet3.Rows(i).EntireRow.Delete
End If
Next i
End If
If Not ComboBox3.Value = "" Then
For i = 2 To Sheet3.Cells(2, 1).End(xlDown).Row
If Not Sheet3.Cells(i, 3).Value = ComboBox3.Value Then
Sheet3.Rows(i).EntireRow.Delete
End If
Next i
End If
If Not ComboBox4.Value = "" Then
For i = 2 To Sheet3.Cells(2, 1).End(xlDown).Row
If Not Sheet3.Cells(i, 4).Value = ComboBox4.Value Then
Sheet3.Rows(i).EntireRow.Delete
End If
Next i
End If
If Not ComboBox5.Value = "" Then
For i = 2 To Sheet3.Cells(2, 1).End(xlDown).Row
If Not Sheet3.Cells(i, 5).Value = ComboBox5.Value Then
Sheet3.Rows(i).EntireRow.Delete
End If
Next i
End If
For Each oControl In sSheet.OLEObjects
With oControl
If Left(.Name, 3) = "chk" Then
If Not .Object.Value = True Then
For i = 6 To Sheet3.Cells(1, 1).End(xlToRight).Column
If Sheet3.Cells(1, i).Value = .Object.Caption Then
Sheet3.Columns(i).EntireColumn.Delete
End If
Next i
End If
End If
End With
Next oControl
Else
Sheet3.Delete
End If
End Sub
So I'm starting by copying my data sheet and thereafter trying to delete the rows and columns which doesn't match my criterias. If nothing is selected then the copy should get deleted again.
I can't find the error -I get an "object required" - but I can't see what's wrong, to me it looks pretty straight forward!