Results 1 to 10 of 10

Thread: Complex Excel Problem!!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Complex Excel Problem!!

    Hi

    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?

    Any help would be greatly appreciated!!

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Complex Excel Problem!!

    I thought the Excel combo's didn't use AddItem, perhaps I was thinking of another Office program.

    The important thing to realise is that AddItem doesn't need to be a "manual" value - you can read the values from the sheet.

    Assuming that the countries are in order, you can use code like this:
    VB Code:
    1. Dim strThisCountry as String, strLastCountry as String
    2. Dim lngRowNumber as Long
    3.  
    4.   strLastCountry = ""
    5.   [u]Combo1[/u].Clear  '(change to name of combo to fill)
    6.  
    7.   With WorkSheets("[U]Sheet1[/U]")  '(change to name of sheet with data)
    8.     For lngRowNumber = [u]2[/u] To .UsedRange.Rows.Count  '(change to number of first row)
    9.       strThisCountry = .Cells(lngRowNumber, [u]1[/u])  '(change to column number [A=1, B=2, ...])
    10.       If strThisCountry <> strLastCountry Then
    11.         [u]Combo1[/u].Additem strThisCountry
    12.         strLastCountry = strThisCountry
    13.       End If
    14.     Next lngRowNumber
    15.   End With

    See the Excel Tutorial link below for an explanation of the parts of the code I used.

  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Complex Excel Problem!!

    hmmm.. My first thought was along the same lines as what Si is saying..
    but now im thinking...

    Are you doing this all in excel? or are u making a VB front end for this?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Complex Excel Problem!!

    direktoren

    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:
    1. Option Explicit
    2.  
    3. Sub Populate_Box(ByRef MyCombo As ComboBox, ByVal ColNum As Long, Optional ByVal ParentVal As String)
    4. Dim lRowCount As Long
    5. Dim rngData As Range
    6. Dim lRowNum As Long
    7. Dim sEntry As String
    8. Dim bLoaded As Boolean
    9. Dim lIndex As Long
    10.    
    11.     'Remove all existing values
    12.     'from the combobox
    13.     MyCombo.Clear
    14.    
    15.     'Set the input range for the CB
    16.     With Worksheets("Data")
    17.         lRowCount = .UsedRange.Rows.Count
    18.         Set rngData = .Range(.Cells(2, ColNum), .Cells(lRowCount, ColNum))
    19.     End With
    20.    
    21.     'Loop through all rows in the input range
    22.     For lRowNum = 1 To rngData.Rows.Count
    23.        
    24.         'I have assumed that the Parent (Filtering Value)
    25.         'is in the next column to the left
    26.        
    27.         'If there is no filter..
    28.         If ParentVal = "" Then
    29.            
    30.             '..store the value
    31.             sEntry = rngData.Cells(lRowNum, 1).Value
    32.        
    33.         '..Otherwise...
    34.         Else
    35.             '...Only store the value if the Filter Value
    36.             '...if the preceding column has the parent value
    37.             If rngData.Cells(lRowNum, 1).Offset(0, -1).Value = ParentVal Then
    38.                 sEntry = rngData.Cells(lRowNum, 1).Value
    39.             End If
    40.         End If
    41.        
    42.         'Assume that the value has not been loaded
    43.         'into the CB
    44.         bLoaded = False
    45.        
    46.         'Only continue if we have a value to load
    47.         If sEntry <> "" Then
    48.            
    49.             'Loop through the current values in the CB
    50.             For lIndex = 0 To UBound(MyCombo.List)
    51.                
    52.                 'If the value is already in the CB - record this fact
    53.                 If MyCombo.List(lIndex) = sEntry Then
    54.                     bLoaded = True
    55.                     Exit For
    56.                 End If
    57.             Next lIndex
    58.            
    59.             'If the value is not in the CB, Add it
    60.             If Not bLoaded Then
    61.                 MyCombo.AddItem sEntry
    62.             End If
    63.            
    64.         End If
    65.     Next lRowNum
    66.  
    67. End Sub
    Attached Files Attached Files
    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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Re: Complex Excel Problem!!

    Hi guys

    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:
    1. 'Remove all existing values
    2.     'from the combobox
    3.     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.

  6. #6
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Complex Excel Problem!!

    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?
    Attached Files Attached Files
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Re: Complex Excel Problem!!

    Hi Declan

    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.

    regards Nicolaj
    Attached Files Attached Files

  8. #8
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Complex Excel Problem!!

    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:
    1. 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

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Re: Complex Excel Problem!!

    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!

    Thanks again
    Nicolaj

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Re: Complex Excel Problem!!

    Hey Declan

    just tried it - works as a peach! Thanx

    One other problem however, which I was hoping that you could might help me with... Its in my final copy function:

    VB Code:
    1. Private Sub CommandButton1_Click()
    2. Dim x As Long
    3. Dim i As Long
    4. Dim oControl As OLEObject
    5. Dim dSheet As Worksheet
    6. Dim sSheet As Worksheet
    7.  
    8. Set dSheet = ActiveWorkbook.Worksheets("Data")
    9. Set sSheet = ActiveWorkbook.Worksheets("Start")
    10.  
    11. dSheet.Copy after:=Sheets(1)
    12.  
    13. If Not ComboBox1.Value = "" Then
    14. For i = 2 To Sheet3.Cells(2, 1).End(xlDown).Row
    15.     If Not Sheet3.Cells(i, 1).Value = ComboBox1.Value Then
    16.         Sheet3.Rows(i).EntireRow.Delete
    17.     End If
    18. Next i
    19. If Not ComboBox2.Value = "" Then
    20. For i = 2 To Sheet3.Cells(2, 1).End(xlDown).Row
    21.     If Not Sheet3.Cells(i, 2).Value = ComboBox2.Value Then
    22.         Sheet3.Rows(i).EntireRow.Delete
    23.     End If
    24. Next i
    25. End If
    26. If Not ComboBox3.Value = "" Then
    27. For i = 2 To Sheet3.Cells(2, 1).End(xlDown).Row
    28.     If Not Sheet3.Cells(i, 3).Value = ComboBox3.Value Then
    29.         Sheet3.Rows(i).EntireRow.Delete
    30.     End If
    31. Next i
    32. End If
    33. If Not ComboBox4.Value = "" Then
    34. For i = 2 To Sheet3.Cells(2, 1).End(xlDown).Row
    35.     If Not Sheet3.Cells(i, 4).Value = ComboBox4.Value Then
    36.         Sheet3.Rows(i).EntireRow.Delete
    37.     End If
    38. Next i
    39. End If
    40. If Not ComboBox5.Value = "" Then
    41. For i = 2 To Sheet3.Cells(2, 1).End(xlDown).Row
    42.     If Not Sheet3.Cells(i, 5).Value = ComboBox5.Value Then
    43.         Sheet3.Rows(i).EntireRow.Delete
    44.     End If
    45. Next i
    46. End If
    47. For Each oControl In sSheet.OLEObjects
    48.         With oControl
    49.             If Left(.Name, 3) = "chk" Then
    50.             If Not .Object.Value = True Then
    51.                 For i = 6 To Sheet3.Cells(1, 1).End(xlToRight).Column
    52.                    If Sheet3.Cells(1, i).Value = .Object.Caption Then
    53.                        Sheet3.Columns(i).EntireColumn.Delete
    54.                    End If
    55.                 Next i
    56.             End If
    57.             End If
    58.         End With
    59.     Next oControl
    60. Else
    61.     Sheet3.Delete
    62. End If
    63.  
    64. 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!

    Nicolaj

    I

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width