+ Reply to Thread
Results 1 to 5 of 5

Userform Run-time error 1004

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-21-2015
    Location
    nederland
    MS-Off Ver
    2021
    Posts
    204

    Userform Run-time error 1004

    Dear mr / mrs,

    I get run-time error 1004 when I try to delete data with cmdDelete see below the VBA

    Rowsource listbox1 = information

    please help me to resolve this issue
    thank you in advance for your support

    Kirana

    Private Sub ListBox1_Click()
    Dim say As Long, A As Byte, r As Long
    Dinfo1 = ListBox1.List(ListBox1.ListIndex, 0)
    
    For A = 1 To 6
    Controls("Info" & A) = ListBox1.List(ListBox1.ListIndex, A - 1)
    Next
    
    Sheets("Data").Range("A:A").Find(ListBox1.Text).Activate
    say = ActiveCell.Row
    Sheets("Data").Range("A" & say & ":I" & say).Select
    TextBox1 = ListBox1.ListIndex + 1
    End Sub
    
    
    Private Sub cmdDelete_Click()
    Dim findvalue As Range
        Dim cDelete As VbMsgBoxResult
        Dim cNum As Integer
        'check for values
        If Info1.Value = "" Or Info2.Value = "" Then
            MsgBox "There is not data to delete"
            Exit Sub
        End If
        'give the user a chance to change their mind
        cDelete = MsgBox("Are you sure that you want to delete Select Data", vbYesNo + vbDefaultButton2, "Are you sure????")
        If cDelete = vbYes Then
            'delete the row
            Set findvalue = Sheet1.Range("B:B").Find(What:=Info2, LookIn:=xlValues)
            findvalue.EntireRow.Delete
        End If
        'clear the controls
        cNum = 6
        For X = 1 To cNum
            Me.Controls("Info" & X).Value = ""
        Next
        'refresh the listbox
        Unload Me
        frmaddress.Show
    End Sub
    Attached Files Attached Files
    Last edited by kirana2014; 11-28-2017 at 01:44 AM.

  2. #2
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Userform Run-time error 1004

    It has to do with this line
    Sheets("Data").Range("A:A").Find(ListBox1.Text).Activate
    in the "ListBox1_Click" macro. If I comment out this line, it works fine.
    The error states that an application or object hasn't been defined

    More info: I stepped through the code and noticed that when the delete code runs, after it does this line....
    Set findvalue = Sheets("Data").Range("A:A").Find(What:=Info2, LookIn:=xlValues)
            findvalue.EntireRow.Delete
    it runs back through the ListBox1_Click code. At this point the range you want to activate has already been activated so it errors??
    Last edited by gmr4evr1; 11-26-2017 at 03:31 PM. Reason: Added more info
    1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG
    You don't have to add Rep if I have helped you out (but it would be nice), but please mark the thread as SOLVED if your issue is resolved.

    Tom

  3. #3
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,157

    Re: Userform Run-time error 1004

    As gmr4evr1 above, and and additionally:
    1. After deleting a row, 'ListBox1_Click' procedure runs 2x/works twice and fills the text boxes
    2. Why, since after returning, to the 'cmdDelete_Click' procedure, text boxes are clearing ?
    3. You haven't 'UserForm_Initialize' procedure
    According to me, you should add 'UserForm_Initialize' and improve the triggering of procedures (to prevent multiple runs).
    You could add e.g.:
    'New procedure
    Private Sub UserForm_Initialize()
        With frmaddress
            .Caption = "DATABASE"
            With .ListBox1
                .RowSource = Sheets("Data").Range("information").Address(0, 0)
                .ListIndex = -1
            End With
        End With
    End Sub
    and modify 'cmdDelete_Click', e.g.:
    Private Sub cmdDelete_Click()
    Dim findvalue As Range
    Dim cDelete As VbMsgBoxResult
    Dim cNum As Integer
    
        'check for values
        If Info1.Value = "" Or Info2.Value = "" Then
            MsgBox "There is not data to delete"
            Exit Sub
        End If
        
        'give the user a chance to change their mind
        cDelete = MsgBox("Are you sure that you want to delete Select Data", vbYesNo + vbDefaultButton2, "Are you sure????")
        If cDelete = vbYes Then
            'delete the row
            Set findvalue = Sheet1.Range("B:B").Find(What:=Info2, LookIn:=xlValues)
            
            If Not findvalue Is Nothing Then                            'new line
                findvalue.EntireRow.Delete
                Set findvalue = Nothing                                 'new line
            Else                                                        'new line
                MsgBox "There is nothing to remove", vbOKOnly, "Info"   'new line
                Exit Sub                                                'new line
            End If                                                      'new line
        End If
        
        'clear the controls
        cNum = 6
        For X = 1 To cNum
            Me.Controls("Info" & X).Value = ""
        Next
        
        'refresh the listbox
        'Unload Me
        'frmaddress.Show
        
        frmaddress.ListBox1.RowSource = Sheets("Data").Range("information").Address(0, 0) 'new line
    End Sub
    Last edited by mjr veverka; 11-26-2017 at 04:03 PM.

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Userform Run-time error 1004

    gmr4evr1 is right.

    In this type of scenario

    I Declare a public ChangeFlag as Boolean

    I then set ChangeFlag as True before I perform a function that could create an error

    I then set this back to False when I know that the risk of error has passed.

    This code works:-

    
    '**************************************
    Public Changeflag As Boolean
    '**************************************
    
    Private Sub cmdAdd_Click()
    
    Dim X As Integer
    Dim nextrow As Range
    
    On Error GoTo cmdAdd_Click_Error
    
    Set nextrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
    For X = 1 To 4
    If Me.Controls("Info" & X).Value = "" Then
    MsgBox "You must add Name ,Address ,State and Zipe"
    Exit Sub
    End If
    Next
    
    If WorksheetFunction.CountIf(Sheet1.Range("A:A"), Me.Info1.Value) > 0 Then
    MsgBox "This Name already exists"
    Exit Sub
    End If
    
    For X = 1 To 6
    nextrow = Me.Controls("Info" & X).Value
    Set nextrow = nextrow.Offset(0, 1)
    Next
    'clear
    For X = 1 To 6
    Me.Controls("Info" & X).Value = ""
    Next
    
    On Error GoTo 0
    Exit Sub
    
    cmdAdd_Click_Error:
    
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdAdd_Click of Form frmCustomer"
    
    End Sub
    
    Private Sub cmdClear_Click()
    Unload Me
    frmaddress.Show
    End Sub
    
    Private Sub cmdClose_Click()
    Unload Me
    End Sub
    
    Private Sub cmdDelete_Click()
    Dim findvalue As Range
        Dim cDelete As VbMsgBoxResult
        Dim cNum As Integer
        'check for values
        If Info1.Value = "" Or Info2.Value = "" Then
            MsgBox "There is not data to delete"
            Exit Sub
        End If
        'give the user a chance to change their mind
        cDelete = MsgBox("Are you sure that you want to delete Select Data", vbYesNo + vbDefaultButton2, "Are you sure????")
        If cDelete = vbYes Then
            'delete the row
    
    '**************************************
    Changeflag = True
            Set findvalue = Sheet1.Range("B:B").Find(What:=Info2, LookIn:=xlValues)
    
    
            findvalue.EntireRow.Delete
        End If
        'clear the controls
        cNum = 6
        For X = 1 To cNum
            Me.Controls("Info" & X).Value = ""
        Next
        'refresh the listbox
        Unload Me
        frmaddress.Show
    
    '**************************************
    Changeflag = False
    
    End Sub
    
    Private Sub ListBox1_Click()
    
    '**************************************
    If Changeflag = True Then Exit Sub
    
    Dim say As Long, A As Byte, r As Long
    Dinfo1 = ListBox1.List(ListBox1.ListIndex, 0)
    
    For A = 1 To 6
    Controls("Info" & A) = ListBox1.List(ListBox1.ListIndex, A - 1)
    Next
    
    Sheets("Data").Range("A:A").Find(ListBox1.Text).Activate
    say = ActiveCell.Row
    Sheets("Data").Range("A" & say & ":I" & say).Select
    TextBox1 = ListBox1.ListIndex + 1
    End Sub
    
    
    Private Sub TextBox1_Change()
    TextBox1 = ListBox1.ListIndex + 1
    End Sub
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  5. #5
    Forum Contributor
    Join Date
    09-21-2015
    Location
    nederland
    MS-Off Ver
    2021
    Posts
    204

    Re: Userform Run-time error 1004

    Dear all,

    Thank you for you help

    Kirana

+ 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. [SOLVED] I am getting error as "Run Time error '1004' Microsoft excel cannot paste the data
    By Amittapre in forum Excel Programming / VBA / Macros
    Replies: 43
    Last Post: 03-24-2017, 04:40 PM
  2. [SOLVED] Sort Macro Run-time error '1004': Application/Object-defined error.
    By sam1212 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-22-2014, 10:05 AM
  3. Run time error 1004 on userform initialize
    By Nitefox in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-09-2014, 12:44 PM
  4. 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
  5. Fix Userform IF Statement - Run Time Error "1004"
    By sawan202 in forum Excel General
    Replies: 2
    Last Post: 03-07-2012, 06:32 AM
  6. Run time error 1004-created a userform
    By scrupo in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-23-2010, 04:11 AM
  7. Run-time error 1004 when trying to load UserForm
    By jasoncw in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-22-2008, 04:28 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