VBA ComboBox Code Help

ForumMember

Board Regular
Joined
Sep 10, 2011
Messages
57
Hi all,

I have the code for my project which does almost what I want it to (thanks to Norie), but I have one small problem.

Code:
Option Explicit


Private Sub cboCause_Change()
Dim rngData As Range
Dim LastRow As Long

    If Me.cboCause.ListIndex <> -1 Then

        With Worksheets("Sheet1")

            LastRow = .Cells(Rows.Count, Me.cboCause.ListIndex + 2).End(xlUp).Row - 1

            Set rngData = .Cells(2, Me.cboCause.ListIndex + 2).Resize(LastRow - 1)

        End With

        Me.cboResponsible.List = rngData.Value
        Me.cboResponsible.ListIndex = -1

    End If

End Sub

Private Sub CommandButton1_Click()
    Unload Me
End Sub

Private Sub UserForm_Initialize()
Dim NoHeaders As Long
Dim LastRow As Long
Dim arrHeaders
Dim arrIssue

    With Worksheets("Sheet1")
        NoHeaders = .Cells(1, Columns.Count).End(xlToLeft).Column

        arrHeaders = .Range("B1").Resize(, NoHeaders - 1).Value
        LastRow = .Range("A" & Rows.Count).End(xlUp).Row

        arrIssue = .Range("A2:A" & LastRow).Value
    End With
    
    arrIssue = Application.Transpose(arrIssue)
    
    Me.cboIssue.List = arrIssue
    
    cboIssue.Value = ""
    
    Me.cboCause.List = Application.Transpose(arrHeaders)
    
    
End Sub
If I select an option in cboBox1 that only has one suboption, I get an error:

"Run-time '1004' Application-defined or Object-defined error"

When clicking on debug option, it highlights the following line of code:

Code:
Set rngData = .Cells(2, Me.cboCause.ListIndex + 2).Resize(LastRow - 1)
Could anyone explain why this might happen and how best to amend the code, or work around it to ensure it doesn't keep happening?

Many thanks in advance!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,224,594
Messages
6,179,794
Members
452,943
Latest member
Newbie4296

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