+ Reply to Thread
Results 1 to 5 of 5

Select listbox items and edit in VBA

Hybrid View

terriertrip Select listbox items and edit... 06-20-2017, 04:12 PM
terriertrip Re: Select listbox items and... 06-28-2017, 01:12 PM
Arkadi Re: Select listbox items and... 06-28-2017, 01:17 PM
Norie Re: Select listbox items and... 06-28-2017, 01:21 PM
terriertrip Re: Select listbox items and... 06-28-2017, 02:44 PM
  1. #1
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Select listbox items and edit in VBA

    I have a problem with my listbox edit sub. Userform contains 3 textboxes to add/update/delete items (3 columns) to a list box. The items in the list are stored in a sheet. I want the user to select any item from the list and update the items from the textboxes. The textboxes populate the listbox selection. The code works to populate except that 1.) when an item is selected, the actual update is applied to the last item in the row and not the user selected item from the listbox.
    Option Explicit
    
    Const miROW_NO__HEADER  As Integer = 1
    Const miCOL_NO__FM    As Integer = 1
    Const miCOL_NO__TVD   As Integer = 2
    Const miCOL_NO__MD     As Integer = 3
    Const msTEST_COLUMN     As String = "A"
    Const msSHEET_NAME      As String = "Sheet1"
    
    Dim miRowNo_Current     As Integer
    Dim miRowNo_Last        As Integer
    
    Private Sub UserForm_Initialize()
    
        Const sTEST_COLUMN  As String = "A"
        Dim Rng    As Range
        Dim lastrow As Long
        Dim LastCol As Long
    
        With Sheets(msSHEET_NAME)
    
            lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
            Set Rng = .Range(Cells(2, 1), Cells(lastrow, 1))
    
        ' Populate controls only if the worksheet contains at least one data row
            If .Range(sTEST_COLUMN & miROW_NO__HEADER).Offset(1, 0).Value <> vbNullString Then
    
            miRowNo_Last = .Range(sTEST_COLUMN & .Rows.Count).End(xlUp).Row
            miRowNo_Current = miRowNo_Last
    
        Me.txtFm.Text = .Cells(miRowNo_Last, miCOL_NO__FM).Text
        Me.txtMD.Text = .Cells(miRowNo_Last, miCOL_NO__MD).Text
        Me.txtTVD.Text = .Cells(miRowNo_Last, miCOL_NO__TVD).Text
            End If
        End With
    
        Set Rng = Nothing
          Me.Repaint
    '    txtFm.SetFocus
       
        Dim lbtarget As MSForms.ListBox
        Dim rngSource As Range
        Dim cLastRow As Long
        Dim bLastRow As Long
        
        bLastRow = Sheets(msSHEET_NAME).Range("A" & Rows.Count).End(xlUp).Row
        
    ' populates listbox with data
        With Sheets(msSHEET_NAME)
            Set rngSource = .Range("A2:C" & bLastRow)
            Set lbtarget = Me.ListBox1
            
            With lbtarget
                .ColumnCount = 3
                .ColumnWidths = "185;50;40"
                .List = rngSource.Cells.Value
            End With
        End With
        
      txtFm.SetFocus
      
    End Sub
    
    Private Sub ListBox1_click()
    
        With ThisWorkbook.Sheets(msSHEET_NAME)
        
        txtFm.Text = Me.ListBox1.List(ListBox1.ListIndex, 0)
        txtMD.Text = Me.ListBox1.List(ListBox1.ListIndex, 1)
        txtTVD.Text = Me.ListBox1.List(ListBox1.ListIndex, 2)
        
        End With
    End Sub
    
    Private Sub cmdUpdate_Click()
    
      If MsgBox("You are about to update your formation information.", vbYesNo + vbDefaultButton2, "Update Formation Record") = vbNo Then
        Else
        With ThisWorkbook.Sheets(msSHEET_NAME)
    
            .Cells(miRowNo_Current, miCOL_NO__FM).Value = Me.txtFm.Text
            .Cells(miRowNo_Current, miCOL_NO__MD).Value = Me.txtMD.Text
            .Cells(miRowNo_Current, miCOL_NO__TVD).Value = Me.txtTVD.Text
    
        End With
        End If
        
        UserForm_Initialize
    
    End Sub
    Best regards.
    Last edited by terriertrip; 06-28-2017 at 02:45 PM. Reason: simplification

  2. #2
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Select listbox items and edit in VBA

    I've simplified the problem. Can anybody help with this, please??

  3. #3
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Select listbox items and edit in VBA

    Can you provide the workbook? With userforms especially, it is very hard for us to re-create the environment to see what is going on...
    Clean out any sensitive data, we just need a few rows of random data, and the userform in question.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Select listbox items and edit in VBA

    You need to set miRowNo_Current to the row the data has come from and you can probably do that by using the ListIndex property of the listbox.
    miRowNo_Current = Me.ListBox1.ListIndex + 2
    If posting code please use code tags, see here.

  5. #5
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Select listbox items and edit in VBA

    Thanks, Norie! That works perfectly.

+ 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] Add, Edit, Delete items in dynamic listbox
    By zvonacfirst in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 02-17-2017, 03:59 AM
  2. Edit ListBox Items
    By Boechat in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-19-2016, 10:53 AM
  3. is it possible to edit items of a listbox in excel VBA
    By maggie_xio in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-03-2015, 05:46 AM
  4. Select & edit items in a listbox in VBA
    By poppy in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 04-04-2014, 04:47 PM
  5. [SOLVED] Auto select items in a Listbox with items from another Listbox
    By perducci in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 05-22-2013, 04:45 PM
  6. VBA edit listbox items on userform
    By Hilton1982 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-11-2013, 03:51 AM
  7. Select, Edit, Add & Delete items in a list box
    By baisty182 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-10-2010, 07:25 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