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.
Bookmarks