+ Reply to Thread
Results 1 to 5 of 5

Run Time Error when Deleting last Item in ListBox

  1. #1
    Registered User
    Join Date
    12-02-2014
    Location
    Circleville, Ohio
    MS-Off Ver
    Office 360
    Posts
    10

    Run Time Error when Deleting last Item in ListBox

    When deleting the last item in the list box an error message appears giving me a "Type Mismatch - Could not set the value property" Error. Deleting any other item in the listbox there is no problem.

    The code is:

    me.txtAccountNumber.Value = Application.VLOOKUP(me.lstAccountName, Sheets("AcctNames").Range ("A:B"), 2, False)

    Any help would be appreciated and thank you in advance

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Run Time Error when Deleting last Item in ListBox

    Is one of the list items selected when you delete the last entry?
    You can check the .ListIndex property to ensure the default property of lstAccountName has a valid value. It should be a value between zero and listcount-1
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    12-02-2014
    Location
    Circleville, Ohio
    MS-Off Ver
    Office 360
    Posts
    10

    Re: Run Time Error when Deleting last Item in ListBox

    I check the last record isn't deled the coding, by replacing the "+" with a "-", the last record isn't deleted

    Private Sub cmdDeleteAccount_Click()
    Dim strDeleteResponse As String
    Dim lngIndex As Long

    Application.ScreenUpdating = False
    Sheets("AcctNames").Visible = True
    Sheets("AcctNames").Activate

    strDeleteResponse = MsgBox("This will delete the selected row. Are you sure you want to remove this transaction?", vbCritical + vbYesNo, "Delete Entry")

    Select Case strDeleteResponse
    Case vbYes
    lngIndex = Me.lstAccountName.ListIndex + 1
    If lngIndex <> -1 Then
    AcctNames.Rows(lngIndex).EntireRow.Delete
    Me.lstAccountName.RowSource = "a:a"
    End If
    Case vbNo
    Exit Sub
    End Select

    Application.ScreenUpdating = False
    Sheets("AcctNames").Visible = True
    Sheets("AcctNames").Activate

    End Sub


    From start to finnish here is the code for my delete record.

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Run Time Error when Deleting last Item in ListBox

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

    Are you really listing all of column A?

    Perhaps it would make more sense if you posted workbook example.

  5. #5
    Registered User
    Join Date
    12-02-2014
    Location
    Circleville, Ohio
    MS-Off Ver
    Office 360
    Posts
    10

    Re: Run Time Error when Deleting last Item in ListBox

    CODE=
    Private Sub cmdDeleteAccount_Click()
    Dim strDeleteResponse As String
    Dim lngIndex As Long

    Application.ScreenUpdating = False
    Sheets("AcctNames").Visible = True
    Sheets("AcctNames").Activate

    strDeleteResponse = MsgBox("This will delete the selected row. Are you sure you want to remove this transaction?", vbCritical + vbYesNo, "Delete Entry")

    Select Case strDeleteResponse
    Case vbYes
    lngIndex = Me.lstAccountName.ListIndex + 1
    If lngIndex <> -1 Then
    AcctNames.Rows(lngIndex).EntireRow.Delete
    Me.lstAccountName.RowSource = "a:a"
    End If
    Case vbNo
    Exit Sub
    End Select

    Application.ScreenUpdating = False
    Sheets("AcctNames").Visible = True
    Sheets("AcctNames").Activate

    End Sub

    CODE/

+ 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] Deleting Duplicate item in listbox
    By zicktechnology in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-22-2013, 05:57 AM
  2. [SOLVED] Copy one listbox item to same index position in another listbox
    By Foreverlearning in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-19-2012, 06:05 AM
  3. Listbox fails to update after deleting an item
    By skysurfer in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-09-2010, 01:15 PM
  4. Populate ListBox Based On Item Chosen In Another ListBox
    By davemojo82 in forum Excel General
    Replies: 1
    Last Post: 08-04-2009, 08:39 AM
  5. deleting line item in import function causes REF# error
    By Jen in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-12-2006, 09:24 PM

Tags for this Thread

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