VBA代码不按预期运行

我有一个名为“数据”的工作表,它存储了9列地址字段。 该表被locking,以防止意外删除细胞。 所有修改必须使用用户窗体进行

这个子部分定义了数据范围:

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 

当combobox1被改变时,下一个子部分改变表单的内容:

 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 

最后一个子应该用表单上的文本框的值replace工作表内容

 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 

第一行(Range(.RowSource).Cells(.ListIndex + 1,1).Value = TextBox30.Value)在上面的子程序中执行,Textbox30的修改值被粘贴在A列的数据表中,覆盖以前的价值。 这之后没有任何行被执行。 我甚至尝试过移动线条,每次只处理第一行。

任何人都可以启发我到哪里我错了请。

你的控制是绑定的范围。 当您更改范围时,控件会更改,这将触发其更改事件,从而覆盖文本框的值。 我build议你完全不使用Rowsource ,但使用List来填充控件,然后使用它的名称回写到范围:

 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