Visual Basic For Applications:通过文本框编辑表格时出现问题



我已经用这个问题运行了一段时间,我留下了下面写的示例文件代码,问题是当我在我工作的计算机上运行它时,在Sub CommandButton1_Click((中,当它开始运行I=1的注释行后的3行时,每行都启动ListBox1_Click?,通过使用条件,使其在处理其他函数时不会覆盖任何内容。

我想知道以前是否有人遇到过这个问题,并知道如何解决它。从我的个人电脑上运行它不是一种选择,但if条件使事情发生,但我认为这不是解决问题的最佳方式。

Private Sub CommandButton1_Click()
Dim Ultima_Fila As Integer
Ultima_Fila = Sheets("Sheet1").Range("E2") + 1
Sheets("Sheet1").Range("A" & Ultima_Fila).EntireRow.Insert
Sheets("Sheet1").Range("A" & Ultima_Fila) = Sheets("Sheet1").Range("E2")
'i=1 'Required i to be 1 in order to avoid the code to jump and read the textbox from the ListBox1_Click
Sheets("Sheet1").Range("B" & Ultima_Fila) = TextBox1.Value
Sheets("Sheet1").Range("C" & Ultima_Fila) = TextBox2.Value
Sheets("Sheet1").Range("D" & Ultima_Fila) = TextBox3.Value
'i=0 Restarts i value
End Sub
Private Sub ListBox1_Click()
'If i = 0 Then 'When the sub is initialized directly from the listbox click it stores the values displayed on the textboxes
TextBox1 = ListBox1.List(ListBox1.ListIndex, 1)
TextBox2 = ListBox1.List(ListBox1.ListIndex, 2)
TextBox3 = ListBox1.List(ListBox1.ListIndex, 3)
'End If 'This conditional jumps the following instruction when it is reached from the CommandButton1_Click
End Sub
Private Sub UserForm_Activate()
With ListBox1
.ColumnCount = 4
.RowSource = "Table1"
.ColumnHeads = True
End With
End Sub

请不要对我对您的代码所做的更改感到沮丧。本质上它保持不变。我只是想确定我告诉你的是100%正确的:-

ListBox1_Click事件由ListBox1.ListIndex的更改触发。我下面的代码包含这样一个更改,并以您已经找到的方式禁用它引起的事件。对在另一台计算机上激发的过程的解释是ListIndex已更改。也许在那里运行的代码是不同的,也许是Mac或谷歌的工作表,简而言之,是一个解释相同代码的非微软引擎。

但是,您可能仍然喜欢我的代码,因为它有一些您没有的功能。请尝试一下。它应该在两个系统上都运行。

Option Explicit
Private DisableListBoxEvents    As Boolean
Private Sub UserForm_Activate()
' 263

With ListBox1
.ColumnCount = 4
.RowSource = "Table1"
.ColumnHeads = True
End With
End Sub
Private Sub CommandButton1_Click()
' 263
Dim Ultima_Fila As Integer
Dim Tbx         As Control
Dim i           As Integer

With Worksheets("Sheet1")
Ultima_Fila = .Range("E2").Value + 1
With .ListObjects(1)
' inserts a table row before table row Ultima_Fila
' Omit the number to append a row.
' To convert sheet row to table row enable this line:-

'Ultima_Fila=Ultima_Filla-.Range.Row-1带.ListRows.Add(Ultima_Fila(.Range对于i=1到3设置Tbx=Me.Controls("TextBox"&i(.Cells(i+1(.Value=Tbx.ValueTbx.值="下一个i结束于结束于以结束

With ListBox1
DisableListBoxEvents = True
.RowSource = "Table1"
.ListIndex = -1
DisableListBoxEvents = False
End With
End Sub
Private Sub ListBox1_Click()
' 263

Dim i       As Integer

If DisableListBoxEvents Then Exit Sub
With ListBox1
For i = 1 To 3
Me.Controls("TextBox" & i).Value = .List(.ListIndex, i)
Next i
End With
End Sub

相关内容

  • 没有找到相关文章

最新更新