+ Reply to Thread
Results 1 to 11 of 11

Combobox not loading the items if the source sheet is not activated

Hybrid View

  1. #1
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Combobox not loading the items if the source sheet is not activated

    Hi

    In the attached workbook, I have a userform
    the combobox ComboMainHead is being populated from the chart of accounts sheet. the problem is if the said sheet is not activated the combox is not populated.

    What am I am doing wrong

    Here is the code behind the userform
    Private Sub UserForm_Initialize()
    Me.lblHeader.Caption = Sheet8.Range("A7").Value & vbNewLine & Sheet8.Range("A8").Value & vbNewLine & Sheet8.Range("A9").Value
        Dim myCollection As Collection, cell As Range
    
        On Error Resume Next
        Set myCollection = New Collection
        With Me.ComboMainHead
            .Clear
            For Each cell In Sheet4.Range("A3:A" & Cells(Rows.Count, 1).End(xlUp).Row)
                If Len(cell) <> 0 Then
                    Err.Clear
                    myCollection.Add cell.Value, cell.Value
                    If Err.Number = 0 Then .AddItem cell.Value
                End If
            Next cell
        End With
        Me.ComboMainHead.ListIndex = 0
    
    End Sub
    Attached Files Attached Files
    Last edited by ImranBhatti; 12-25-2017 at 11:24 AM.
    Teach me Excel VBA

  2. #2
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Combobox not loading the items if the source sheet is not activated

    Replace existing UserForm_Initialize code with the following one...

    Private Sub UserForm_Initialize()
    Me.lblHeader.Caption = Sheet8.Range("A7").Value & vbNewLine & Sheet8.Range("A8").Value & vbNewLine & Sheet8.Range("A9").Value
        Dim myCollection As Collection, cell As Range
        Dim lr As Long
        On Error Resume Next
        Set myCollection = New Collection
        lr = Sheet4.Cells(Rows.Count, 1).End(xlUp).Row
        With Me.ComboMainHead
            .Clear
            For Each cell In Sheet4.Range("A3:A" & lr)
                If Len(cell) <> 0 Then
                    Err.Clear
                    myCollection.Add cell.Value, cell.Value
                    If Err.Number = 0 Then .AddItem cell.Value
                End If
            Next cell
        End With
        Me.ComboMainHead.ListIndex = 0
    
    End Sub
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  3. #3
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: Combobox not loading the items if the source sheet is not activated

    Guru G

    The problem is if I call the userform from the Database then the said combobox is not getting populated even with the amended code. This is what I just came to know. It should be populated if I call the userform from Database.
    Last edited by ImranBhatti; 12-25-2017 at 11:25 AM.

  4. #4
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Combobox not loading the items if the source sheet is not activated

    After making the changes, if I click the button called "Show Voucher Entry Form" on DataBase sheet, the ComboBox gets populated as desired.

    Please refer to the demo video.
    Attached Files Attached Files

  5. #5
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,112

    Re: Combobox not loading the items if the source sheet is not activated

    Whilst the code supplied by sktneer worked for me, how about
    Private Sub UserForm_Initialize()
    Me.lblHeader.Caption = Sheet8.Range("A7").Value & vbNewLine & Sheet8.Range("A8").Value & vbNewLine & Sheet8.Range("A9").Value
        Dim myCollection As Collection, cell As Range
    
        On Error Resume Next
        Set myCollection = New Collection
        With Me.ComboMainHead
            .Clear
            For Each cell In Sheet4.Range("A3", Sheet4.Range("A" & Rows.Count).End(xlUp))
                If Len(cell) <> 0 Then
                    Err.Clear
                    myCollection.Add cell.Value, cell.Value
                    If Err.Number = 0 Then .AddItem cell.Value
                End If
            Next cell
        End With
        Me.ComboMainHead.ListIndex = 0
    End Sub

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: Combobox not loading the items if the source sheet is not activated

    Works fine for me.

    Private Sub UserForm_Initialize()
        With Sheet8
            lblHeader.Caption = .Range("A7").Value & vbNewLine & .Range("A8").Value & vbNewLine & .Range("A9").Value
        End With
        sn = Sheet4.Cells(1).CurrentRegion.Value
        With CreateObject("scripting.dictionary")
            For i = 3 To UBound(sn)
                If sn(i, 1) <> vbNullString Then x0 = .Item(sn(i, 1))
            Next
            ComboMainHead.List = .keys
        End With
        ComboMainHead.ListIndex = 0
    End Sub
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  7. #7
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: Combobox not loading the items if the source sheet is not activated

    All you Gurus are correct at your place. If by means of the amended codes the ComboMainHead is populated then the second depended combobox is not populated.Please select the second item from the Mian Head and then click on sub Head combo.

  8. #8
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Combobox not loading the items if the source sheet is not activated

    The issue with both of your codes is because of the way you are trying to find the last used row in column A. Look at the bold part in the For Loop...
    For Each cell In Sheet4.Range("A3:A" & Cells(Rows.Count, 1).End(xlUp).Row)
    This part is not qualified with the Sheet reference so it would always find the last row used in column A on the ActiveSheet.
    So all you need is to make the same change in the ComboMainHead_Change code as well.

    Try it like this...

    Private Sub ComboMainHead_Change()
      Me.ComboSubHead.Clear
        Dim cell2 As Range
        Dim lr As Long
        lr = Sheet4.Cells(Rows.Count, 1).End(xlUp).Row
        For Each cell2 In Sheet4.Range("A3:A" & lr)
            If cell2.Value = Me.ComboMainHead.Value Then
              Me.ComboSubHead.AddItem cell2.End(xlToRight).Value
            End If
        Next
    End Sub

  9. #9
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: Combobox not loading the items if the source sheet is not activated

    Quote Originally Posted by sktneer View Post
    The issue with both of your codes is because of the way you are trying to find the last used row in column A. Look at the bold part in the For Loop...
    For Each cell In Sheet4.Range("A3:A" & Cells(Rows.Count, 1).End(xlUp).Row)
    This part is not qualified with the Sheet reference so it would always find the last row used in column A on the ActiveSheet.
    So all you need is to make the same change in the ComboMainHead_Change code as well.

    Try it like this...

    Private Sub ComboMainHead_Change()
      Me.ComboSubHead.Clear
        Dim cell2 As Range
        Dim lr As Long
        lr = Sheet4.Cells(Rows.Count, 1).End(xlUp).Row
        For Each cell2 In Sheet4.Range("A3:A" & lr)
            If cell2.Value = Me.ComboMainHead.Value Then
              Me.ComboSubHead.AddItem cell2.End(xlToRight).Value
            End If
        Next
    End Sub
    Dhanayawaad Guru Ji!
    That worked like a charm.much much appreciated.

    Thanks to bakerman and others who tried their best to help out.

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: Combobox not loading the items if the source sheet is not activated

    Is this what you mean ?
    Private Sub ComboMainHead_Change()
      Me.ComboSubHead.Clear
        Dim cell2 As Range
        Dim lr As Long, x0
        lr = Sheet4.Cells(Rows.Count, 1).End(xlUp).Row
        With CreateObject("scripting.dictionary")
            For Each cell2 In Sheet4.Range("A3:A" & lr)
                If cell2.Value = ComboMainHead.Value Then x0 = .Item(cell2.Offset(, 1).Value)
            Next
            ComboSubHead.List = .keys
        End With
    End Sub

  11. #11
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Combobox not loading the items if the source sheet is not activated

    You're welcome Imran! Glad we could help.
    Thanks for the feedback.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. ComboBox values source being changed based on another combobox selection in a UserForm
    By stephenanderson in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-08-2016, 11:12 PM
  2. Add items in combobox in a sheet
    By siobeh in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-03-2013, 03:27 AM
  3. Loading from external source and looking cells
    By johnph in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-02-2013, 04:23 PM
  4. Loading Images on Sheet Based On ComboBox
    By Muhnamana in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-25-2010, 12:25 PM
  5. Combobox not loading
    By sentinela in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-14-2009, 06:13 PM
  6. Items From Sheet In Combobox
    By vroemans in forum Excel General
    Replies: 1
    Last Post: 01-12-2007, 03:56 AM
  7. [SOLVED] Loading sheet names in Combobox
    By Sige in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-16-2005, 01:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1