0
votes

I have a worksheet called "Data" which stores 9 columns of address fields. The sheet is locked to prevent accidental deletion of cells. All amendments have to be carried out using a Userform

This sub defines the data range:

Private Sub UserForm_Initialize()
Dim LastRow as Long
LastRow = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Data").Range("A1:I" & LastRow).Name = "ListName"
ComboBox1.RowSource = "ListName"
ComboBox1.ListIndex = 0
End Sub

The next sub changes the form contents when Combobox 1 is changed:

Private Sub ComboBox1_Change()
With ComboBox1
TextBox30.Value = Range(.RowSource).Cells(.ListIndex + 1, 1)
TextBox31.Value = Range(.RowSource).Cells(.ListIndex + 1, 2)
TextBox32.Value = Range(.RowSource).Cells(.ListIndex + 1, 3)
TextBox33.Value = Range(.RowSource).Cells(.ListIndex + 1, 4)
TextBox34.Value = Range(.RowSource).Cells(.ListIndex + 1, 5)
TextBox35.Value = Range(.RowSource).Cells(.ListIndex + 1, 6)
TextBox36.Value = Range(.RowSource).Cells(.ListIndex + 1, 7)
TextBox37.Value = Range(.RowSource).Cells(.ListIndex + 1, 8)
TextBox38.Value = Range(.RowSource).Cells(.ListIndex + 1, 9)
End With
End Sub

The last sub should replace the worksheet contents with the value of the textboxes on the form

Sub CommandButton4_Click()
With ComboBox1
Range(.RowSource).Cells(.ListIndex + 1, 1).Value = TextBox30.Value
Range(.RowSource).Cells(.ListIndex + 1, 2).Value = TextBox31.Value '       
Range(.RowSource).Cells(.ListIndex + 1, 3).Value = TextBox32.Value
Range(.RowSource).Cells(.ListIndex + 1, 3).Value = TextBox32.Value
Range(.RowSource).Cells(.ListIndex + 1, 4).Value = TextBox33.Value
Range(.RowSource).Cells(.ListIndex + 1, 5).Value = TextBox34.Value
Range(.RowSource).Cells(.ListIndex + 1, 6).Value = TextBox35.Value
Range(.RowSource).Cells(.ListIndex + 1, 7).Value = TextBox36.Value
Range(.RowSource).Cells(.ListIndex + 1, 8).Value = TextBox37.Value
Range(.RowSource).Cells(.ListIndex + 1, 9).Value = TextBox38.Value
End With
Unload UserForm5
End Sub

The first line (Range(.RowSource).Cells(.ListIndex + 1, 1).Value = TextBox30.Value) is executed in the sub above and the amended value of Textbox30 is pasted on the Data sheet in column A, overwriting the previous value. None of the lines after this are executed. I've even tried moving lines around and each time only the first line is processed.

Can anyone enlighten me as to where I've gone wrong please.

1
Have you tried debugging to see what it's actually doing line by line?rory.ap
Is their any sheet code to protect the sheet? could test by using application.enableevents=false before the first line and then application.enableevents=true after last line99moorem
Application::EnableEvents does not remove worksheet protection. For that you would want Worksheet::ProtectCBRF23
Sorry, I should have mentioned; I haven't protected the worksheet yet. for testing purposesLitestream

1 Answers

1
votes

Your control is bound to the range. When you change the range, your control changes, which will trigger its change event, overwriting your textbox values. I suggest you don't use Rowsource at all, but use List to populate the control and then write back to the range using its name:

Private Sub UserForm_Initialize()
Dim LastRow as Long
LastRow = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Data").Range("A1:I" & LastRow).Name = "ListName"
ComboBox1.List= Sheets("Data").Range("ListName").Value
ComboBox1.ListIndex = 0
End Sub

Private Sub ComboBox1_Change()
Dim lIndex as Long
lIndex = ComboBox1.ListIndex + 1
With Sheets("Data").Range("ListName")
TextBox30.Value = .Cells(lIndex, 1).Value
TextBox31.Value = .Cells(lIndex, 2).Value
TextBox32.Value = .Cells(lIndex, 3).Value
TextBox33.Value = .Cells(lIndex, 4).Value
TextBox34.Value = .Cells(lIndex, 5).Value
TextBox35.Value = .Cells(lIndex, 6).Value
TextBox36.Value = .Cells(lIndex, 7).Value
TextBox37.Value = .Cells(lIndex, 8).Value
TextBox38.Value = .Cells(lIndex, 9).Value
End With
End Sub

Sub CommandButton4_Click()
Dim lIndex as Long
lIndex = ComboBox1.ListIndex + 1
Sheets("Data").Range("ListName").Cells(lIndex, 1).Resize(, 9).Value = _
Array(TextBox30.Value, TextBox31.Value, TextBox32.Value, TextBox32.Value, TextBox33.Value, _
TextBox34.Value, TextBox35.Value, TextBox36.Value, TextBox37.Value, TextBox38.Value)
Unload Me
End Sub