+ Reply to Thread
Results 1 to 7 of 7

Macro code to show userform bringing runtime error 1004

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-06-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    352

    Macro code to show userform bringing runtime error 1004

    Hello,

    I have the following code in a standard module:

    Sub OpenCName()
    ChooseCName.Show
    End Sub
    The userform code is as follows:

    Private Sub CommandButton1_Click()
        Selection.Value = ListBox1.Value
        Unload Me
    End Sub
    
    Private Sub CommandButton2_Click()
    Unload Me
    End Sub
    
    Private Sub UserForm_Initialize()
    Dim rRng As Range, r As Range, rSortCr As String
    
    Set rRng = Sheets("LiveCustomers").Range("Table1[Name]")
    rSortCr = UCase(ActiveCell.Value)
    
    For Each r In rRng
        If InStr(UCase(r), rSortCr) Then Me.ListBox1.AddItem r.Value
    Next r
    
    If Me.ListBox1.ListCount = 1 Then
        Me.ListBox1.ListIndex = 0
        Selection.Value = Me.ListBox1.Value
        Unload Me
    End If
    
    End Sub
    However I am getting a runtime error 1004 which says application-defined or object-defined error.

    Can anyone help please?

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    8,031

    Re: Macro code to show userform bringing runtime error 1004

    Open the VBE (Alt + F11) and click on the line of code "Private Sub UserForm_Initialize"
    Then by tapping the F8 button to step through the code line by line, tell us which one it errors out on.

    Or, attach the workbook so we can see.

    BSB

  3. #3
    Forum Contributor
    Join Date
    03-06-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    352

    Re: Macro code to show userform bringing runtime error 1004

    Thank you - it errors when I try to move on from the Set rRng line

    Any ideas?

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,531

    Re: Macro code to show userform bringing runtime error 1004

    Have you checked (and double checked) all names used ?(Sheetname, tablename, headername)
    Because I just setup a table and it works just fine as you wrote it.

  5. #5
    Forum Contributor
    Join Date
    03-06-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    352

    Re: Macro code to show userform bringing runtime error 1004

    Sorry, it was a very stupid mistake on my part. Thanks for pointing me in the right direction

  6. #6
    Forum Contributor
    Join Date
    03-06-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    352

    Re: Macro code to show userform bringing runtime error 1004

    Just further to this, my code is now:

    Private Sub UserForm_Initialize()
    TextBox1.SetFocus
    End Sub
    
    Private Sub CommandButton3_Click()
    
    Dim rRng As Range, r As Range, rSortCr As String
    
    Set rRng = Sheets("LiveCustomers").Range("Table7[name]")
    rSortCr = TextBox1.Value
    
    For Each r In rRng
        If InStr(UCase(r), rSortCr) Then Me.ListBox1.AddItem r.Value
    Next r
    
    If Me.ListBox1.ListCount = 1 Then
        Me.ListBox1.ListIndex = 0
        Selection.Value = Me.ListBox1.Value
        Unload Me
    End If
    
    End Sub
    When I press the CommandButton3 it doesn't search in the table for the value of textbox1 and put the results in the listbox as it should. If I press the button with nothing in the textbox it comes up with all the entries in the table. If I put any text string in the textbox it comes up with nothing in the listbox?

    Can anyone help?

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,531

    Re: Macro code to show userform bringing runtime error 1004

    Please post sample workbook which represents actual situation so we have some testing material.

+ 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. Problem with VBA code - runtime error 1004
    By ExcelFailure in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-08-2015, 02:22 AM
  2. [SOLVED] Runtime error 1004 with my saveas code
    By Anto_BT in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-26-2015, 11:49 AM
  3. VBA code that adds comment to cell by userform; runtime error 1004 when left blank
    By dmbroekhuysen in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-26-2014, 01:02 PM
  4. [SOLVED] 'Runtime Error 1004' when trying to Rename Worksheet using vba code
    By Raporter65 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-28-2013, 07:43 PM
  5. [SOLVED] Range error in code, runs alone but not inside my full program, giving runtime error 1004
    By charizzardd in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-23-2012, 03:34 PM
  6. RunTime error with UserForm!.Show
    By j_Southern in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-03-2011, 09:42 AM
  7. Runtime Error Code 1004
    By laks in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-19-2007, 07:08 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