+ Reply to Thread
Results 1 to 11 of 11

Using one listbox to edit data from multiple worksheets

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-04-2018
    Location
    San Dieg CA
    MS-Off Ver
    Microsoft Office Professional Plus 2016 (Excel 2016 MSO 32 bit)
    Posts
    167

    Using one listbox to edit data from multiple worksheets

    Hello All,

    Reaching out for some help once again. I have a listbox that is populated from a combobox when a worksheet is selected. When I click on a selection in the listbox it populates the corresponding textboxes below. I have the below code that’s supposed to edit whatever selection I make, but instead it edits the entire table. My second problem is that it only works (if you can call it that) on one worksheet (Re-Enlistments). I want to be able to edit all the worksheets in the combobox. Code is below and file is attached. As always, I’m grateful for any assistance.

     Private Sub CommandButton1_Click()
        Dim i As Integer
            For i = 2 To Range("B10000").End(xlUp).Row
            'If Cells(i, 1) >= 1 Then
            If Me.ListBox1.List(Me.ListBox1.ListIndex, 0) = "" Then
         
                Cells(i, 2) = TextBox1.Text
                Cells(i, 3) = TextBox2.Text
                Cells(i, 4) = TextBox3.Text
                Cells(i, 5) = TextBox4.Text
                Cells(i, 6) = TextBox5.Text
                Cells(i, 7) = TextBox6.Text
                Cells(i, 8) = TextBox7.Text
                Cells(i, 9) = TextBox9.Text
                Cells(i, 10) = TextBox10.Text
            End If
        Next i
        MsgBox "Data has been Saved!", vbInformation
        Unload Me
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Using one listbox to edit data from multiple worksheets

    but instead it edits the entire table.
    That is because you are looping the entire tables rows
    For i = 2 To Range("B10000").End(xlUp).Row
    My second problem is that it only works (if you can call it that) on one worksheet
    As you are not referencing a sheet, this code below works on active sheet only...
    Cells(i, 2) = TextBox1.Text

    Try this to see what I mean...
    Private Sub ListBox1_Click()
    Dim i As Long
    For i = 1 To 10
        Me.Controls("TextBox" & i) = Me.ListBox1.List(Me.ListBox1.ListIndex, i)
    Next i
    End Sub
    Private Sub CommandButton1_Click()
    Dim rw As Long
    rw = ListBox1.ListIndex - 3
    With Sheets(ComboBox1.Value).ListObjects(1)
       .DataBodyRange(rw, 1).Resize(, 10) = Array(TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6, TextBox7, TextBox8, TextBox9, TextBox10)
    End With
    End Sub

    I want to be able to edit all the worksheets in the combobox
    Not all worksheets have the same info as your listbox selections though?
    Are you wanting to search for criteria and update accordingly...
    Last edited by Sintek; 07-15-2020 at 05:17 AM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Forum Contributor
    Join Date
    08-04-2018
    Location
    San Dieg CA
    MS-Off Ver
    Microsoft Office Professional Plus 2016 (Excel 2016 MSO 32 bit)
    Posts
    167

    Re: Using one listbox to edit data from multiple worksheets

    first of all,

    thank you for the overwhelming replies. i love this forum!!!. both suggestions work beautifully, and this may have to be a different post, but since i have everyone's attention... how to do i go the sheet or active the sheet i'm editing and show the edit in the listbox as well?

    roro

  4. #4
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Using one listbox to edit data from multiple worksheets

    Not so sure if I get what you want exactly,
    anyway the code below is "cheating" by insert a number (for row number) to the corresponding sheet :


    Option Explicit
    
    
    
    
    Private Sub UserForm_Initialize()
        Dim i As Integer
        
        For i = 3 To Sheets.Count
        Me.ComboBox1.AddItem Sheets(i).Name
        Next i
        Me.ListBox1.ColumnWidths = "0;70;70;80;80;80;150;55;70;150;85" 'the first column is 0, so it's hidden - but it contains the row number information.
        
    End Sub
    
    Private Sub combobox1_change()
        Dim LR As Long, LC As Long
        
        Me.ListBox1.Clear
        If Me.ComboBox1.ListIndex = -1 Then Exit Sub
        
        With Sheets(Me.ComboBox1.Value)
            LR = .Range("B" & Rows.Count).End(xlUp).Row
            LC = .ListObjects(1).ListColumns.Count + 1
            
            .Range("A1").Value = 1 'this line is to fill corresponding sheet with row number .... then...
            .Range("A1").AutoFill Destination:=.Range("A1").Resize(LR), Type:=xlFillSeries 'then fill it to the last row of the data in column B
        
        
            With .Range("A2", .Cells(LR, LC))
            
                Me.ListBox1.ColumnCount = .Columns.Count
                Me.ListBox1.List = .Value
            End With
        End With
        
    End Sub
    
    Private Sub ListBox1_Click()
         
        Me.TextBox1.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 1)
        Me.TextBox2.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 2)
        Me.TextBox3.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 3)
        Me.TextBox4.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 4)
        Me.TextBox5.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 5)
        Me.TextBox6.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 6)
        Me.TextBox7.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 7)
        Me.TextBox8.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 8)
        Me.TextBox9.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 9)
        Me.TextBox10.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 10)
    
    End Sub
    
    
    Private Sub CommandButton1_Click()
        Dim i As Integer
        
        'For i = 2 To Range("B10000").End(xlUp).Row
            'If Cells(i, 1) >= 1 Then
        '    If Me.ListBox1.List(Me.ListBox1.ListIndex, 0) = "" Then
    
        Dim X As Long, bool As Boolean
    For X = 0 To ListBox1.ListCount 'the loop is to check if any item is selected in the ListBox1
      If ListBox1.Selected(X) Then
        bool = True
        Exit For
      End If
    Next
    
    If bool = False Then Exit Sub 'if nothing is selected in ListBox1 then exit the sub
        
    
         With Sheets(Me.ComboBox1.Value)
         i = ListBox1.Column(0)  'i is the first column (the hidden one) of the ListBox1 selected value (contains the row number).
                .Cells(i, 2) = TextBox1.Text
                .Cells(i, 3) = TextBox2.Text
                .Cells(i, 4) = TextBox3.Text
                .Cells(i, 5) = TextBox4.Text
                .Cells(i, 6) = TextBox5.Text
                .Cells(i, 7) = TextBox6.Text
                .Cells(i, 8) = TextBox7.Text
                .Cells(i, 9) = TextBox9.Text
                .Cells(i, 10) = TextBox10.Text
         '   End If
        'Next i
    End With
        MsgBox "Data has been Saved!", vbInformation
        Unload Me
    End Sub
    I want to be able to edit all the worksheets in the combobox
    It will edit the corresponding sheet (which is selected via the combobox).
    But if you mean :
    the user select Re-Enlistments sheet in the combobox,
    select one item in the ListBox1,
    edit a value in the text-box
    click the CommandButton1
    then the user expecting that the edited value in the text-box is updated in Re-Enlistments sheet AND ALSO to Retirements and C-Ways sheet.....

    The code don't do that, as it only updated the selected sheet via the combobox.
    If the user select Re-Enlistments in the combo-box, then the updated cell (according to what's in the text box) will be just only to Re-Enlistments sheet.
    If the user select Retirements in the combo-box, then the updated cell (according to what's in the text box) will be just only to Retirements sheet.
    If the user select C-Ways in the combo-box, then the updated cell (according to what's in the text box) will be just only to C-Ways sheet.
    Last edited by karmapala; 07-15-2020 at 06:21 AM.

  5. #5
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Using one listbox to edit data from multiple worksheets

    Just add this snippet
    Call ComboBox1_Change
    after you've updated...

  6. #6
    Forum Contributor
    Join Date
    08-04-2018
    Location
    San Dieg CA
    MS-Off Ver
    Microsoft Office Professional Plus 2016 (Excel 2016 MSO 32 bit)
    Posts
    167

    Re: Using one listbox to edit data from multiple worksheets

    brilliant!!! thank you kindly!!!

  7. #7
    Forum Contributor
    Join Date
    08-04-2018
    Location
    San Dieg CA
    MS-Off Ver
    Microsoft Office Professional Plus 2016 (Excel 2016 MSO 32 bit)
    Posts
    167

    Re: Using one listbox to edit data from multiple worksheets

    I hope i'm not wearing out my welcome, but is there a way to activate the selected sheet?

  8. #8
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Using one listbox to edit data from multiple worksheets

    Quote Originally Posted by RoroBear View Post
    I hope i'm not wearing out my welcome, but is there a way to activate the selected sheet?
    I don't exactly understand what you mean, but I guess that you want the sheet is activated once the user select the item (the name of the sheet) in the ComboBox1.

    change this :
    Private Sub CommandButton1_Click()
    Frm_Listbox.Show
    End Sub
    to this
    Private Sub CommandButton1_Click()
    Frm_Listbox.Show vbModeless
    End Sub


    in combobox1_change() sub, add this line :
    With Sheets(Me.ComboBox1.Value)
        .Activate '---> added line
            LR = .Range("B" & Rows.Count).End(xlUp).Row
    This line will activate the sheet once the item (sheet name) in the ComboBox1 is selected.

    then finally in CommandButton1_Click sub :
        Unload Me
        MsgBox "Data has been Saved!", vbInformation
        Sheets("To Do List").Activate
    This will unload the userform first, so you can see the updated text in the cell of the sheet,
    then after the msgbox OK button is clicked, it will activate the "To Do List" sheet.

  9. #9
    Forum Contributor
    Join Date
    08-04-2018
    Location
    San Dieg CA
    MS-Off Ver
    Microsoft Office Professional Plus 2016 (Excel 2016 MSO 32 bit)
    Posts
    167

    Re: Using one listbox to edit data from multiple worksheets

    i wanted the worksheet to active once it's selected from the dropdown, and the codes provided did just that. thank you everyone!!! as always, really appreciate the positive feedback.

  10. #10
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Using one listbox to edit data from multiple worksheets

    Not sure where you want the snippet of code...or why you want to activate it....
    Sheets(ComboBox1.Value).activate

  11. #11
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Using one listbox to edit data from multiple worksheets

    Glad we could assist...Tx for rep +

+ 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. Search and edit the data from multiple worksheets from userform
    By bujaber10 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-06-2019, 10:01 AM
  2. [SOLVED] Edit select data on Listbox
    By kirana2014 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-29-2017, 01:15 AM
  3. Replies: 0
    Last Post: 08-27-2015, 09:44 PM
  4. select multiple column listbox to edit userform
    By tbar05 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-12-2012, 12:13 AM
  5. edit multiple data on multiple worksheets all at once
    By semidevil in forum Excel General
    Replies: 1
    Last Post: 10-17-2010, 06:03 PM
  6. UserForm selected data; to edit multiple worksheets(& specific rows)
    By anonfish in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-17-2010, 11:43 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