+ Reply to Thread
Results 1 to 8 of 8

getting a run time error 1004, trying to create a combobox

Hybrid View

  1. #1
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    getting a run time error 1004, trying to create a combobox

    here is my code to initialize the form:

    Private Sub userform_initialize()
    OptionButton3 = True
    OptionButton15 = True
    OptionButton1 = True
    TextBox1.Value = Format(Now(), "h:mm AM/PM")
    Dim cLoc As Range
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")
        For Each cLoc In ws.Range("Names")
            With Me.ComboBox1
               .AddItem cLoc.Value
            End With
        Next cLoc
    Me.ComboBox1.SetFocus
    End Sub
    what am i doing wrong?

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: getting a run time error 1004, trying to create a combobox

    Is scope of named range set to workbook or worksheet?

    If former, try just using Rane("Names") instead of ws.Range("Names")
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  3. #3
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: getting a run time error 1004, trying to create a combobox

    one quick question, im trying to create a combobox that lists two ranges, the first one is named Hours and the second is offset to that. so why isn't this working?

    Private Sub userform_initialize()
    OptionButton3 = True
    OptionButton15 = True
    OptionButton1 = True
    'TextBox1.Value = hours & minutes & segment
    TextBox1.Value = Format(Now(), "h:mm AM/PM")
    Dim cLoc As Range
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")
        For Each cLoc In ws.Range("Hours")
            With Me.ComboBox1
               .AddItem cLoc.Value
               .List(.ListCount - 1, 1) = cLoc.Offset(0, 1).Value
            End With
            With Me.ComboBox1
            MsgBox .ListCount
            End With
        Next cLoc
    Me.ComboBox1.SetFocus
    End Sub

  4. #4
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: getting a run time error 1004, trying to create a combobox

    thanks CK, but i changed this one line and it's working now.

    Set ws = ThisWorkbook.Sheets("Sheet1")
    i added the ThisWorkbook to the set statement.

  5. #5
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: getting a run time error 1004, trying to create a combobox

    Glad you got it solved. I actually ran some tests and scope had no impact when it's set to workbook.
    I guess, you had another workbook active when the code ran.

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: getting a run time error 1004, trying to create a combobox

    What's the end result you are trying to achieve?

    Should both be loaded to same combobox and selected as one?

    If so do something like.
    Private Sub userform_initialize()
    OptionButton3 = True
    OptionButton15 = True
    OptionButton1 = True
    TextBox1.Value = Format(Now(), "h:mm AM/PM")
    Dim cLoc As Range
    Dim ws As Worksheet
    Dim listArr
    Set ws = ThisWorkbook.Sheets("Sheet1")
    listArr = ws.Range("Hours").Resize(, 2)
        With Me.ComboBox1
            .ColumnCount = 2
            .List = listArr
        End With
        With Me.ComboBox1
            MsgBox .ListCount
        End With
    Me.ComboBox1.SetFocus
    End Sub
    Or are you trying to do something different?

  7. #7
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: getting a run time error 1004, trying to create a combobox

    ok so that shows the combobox with both columns of text, but when i pick something off the list, then it only returns the first column in the combobox. see snapshot. how do i fix that?

    combobox1.png

  8. #8
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: getting a run time error 1004, trying to create a combobox

    That's expected behavior of Combobox. Combobox can only display value of first visible column.

    I've experimented with it before. But there really isn't direct way to show both columns.

    One method is to use caption or text box above combo box to show both.

    Another is to have 3rd column added to range concatenating 1 & 2.
    Then setting column width of 1st and 2nd to 0. Hence, showing 3rd column only.
    Private Sub UserForm_Initialize()
    Dim cLoc As Range
    Dim ws As Worksheet
    Dim listArr
    Set ws = ThisWorkbook.Sheets("Sheet1")
    listArr = ws.Range("Hours").Resize(, 3)
        With Me.ComboBox1
            .ColumnCount = 3
            .ColumnWidths = "0;0;50"
            .List = listArr
        End With
    End Sub
    Or use _Change event to change ColumnWidth.
    Private Sub ComboBox1_Change()
    With Me.ComboBox1
        .ColumnWidths = "0;0;50"
    End With
    End Sub
    
    Private Sub UserForm_Initialize()
    Dim cLoc As Range
    Dim ws As Worksheet
    Dim listArr
    Set ws = ThisWorkbook.Sheets("Sheet1")
    listArr = ws.Range("Hours").Resize(, 3)
        With Me.ComboBox1
            .ColumnCount = 3
            .ColumnWidths = "50;50;0"
            .List = listArr
        End With
    End Sub
    Or just use listbox instead of Combobox.

+ 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. error 1004 when hide/unhide rows using activex multiple combobox
    By wnd.le in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-02-2017, 06:55 PM
  2. Run-time error '1004' when trying to create PDF
    By suetyun in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-25-2015, 05:38 AM
  3. Combobox selection / Delete row after saving in another sheet / Run-time error '1004'
    By Arvin.Amaro in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-15-2015, 07:27 PM
  4. Run Time Error 1004 when trying to create a Query Table
    By scottbass in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-23-2014, 08:23 PM
  5. Need to Create Error Message Box during Runtime error 1004
    By ExcelHelp2013 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-14-2013, 06:24 AM
  6. Error "run-time Error '1004': General Odbc Error
    By D4WNO77 in forum Access Tables & Databases
    Replies: 2
    Last Post: 07-16-2012, 09:55 AM
  7. 1004 Error:VB code to restrict the use of a combobox
    By bernie_bolt_sa in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-03-2008, 09:55 AM

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