【发布时间】:2017-03-17 18:41:22
【问题描述】:
我正在尝试创建一个可编辑的用户表单,它将在“数据”工作表中搜索特定值,选择偏移单元格并将它们替换为文本框中的信息。
通过双击前一个用户表单列表框填充用户表单。
Initialize 事件正在工作,将正确的信息添加到所需的文本/cbo 框中,但是我想要一个“更新”cmd 按钮来搜索 txtup1 中的数字并更改偏移单元格中的值。
我收到运行时错误 424 no option 消息,但我不知道为什么。
Private Sub UserForm_Initialize()
'dim the variables
Dim i As Integer
On Error Resume Next
'find the selected list item
i = frmenqnew.lstenq.ListIndex
'add the values to the text boxes
Me.txtup1.Value = frmenqnew.lstenq.Column(0, i)
Me.txtup2.Value = frmenqnew.lstenq.Column(1, i)
Me.cboup3.Value = frmenqnew.lstenq.Column(4, i)
Me.cboup4.Value = frmenqnew.lstenq.Column(5, i)
Me.cboup5.Value = frmenqnew.lstenq.Column(6, i)
Me.cboup6.Value = frmenqnew.lstenq.Column(7, i)
Me.txtrev.Value = frmenqnew.lstenq.Column(9, i)
With cboup5
.AddItem "Active"
.AddItem "Dormant"
.AddItem "Lost"
.AddItem "Sold"
End With
With cboup6
.AddItem "Drawing"
.AddItem "Appraisal"
.AddItem "Verification"
.AddItem "Presenting"
End With
On Error GoTo 0
End Sub
'====================================================================
Private Sub cmdUpdate_Click()
'declare the variables
Dim findvalue As Range
Dim cNum As Integer
Dim DataSH As Worksheet
'error handling
On Error GoTo errHandler:
'hold in memory and stop screen flicker
Application.ScreenUpdating = False
Set DataSH = Sheet1
'check for values
If txtup1.Value = "" Or txtup2.Value = "" Then
MsgBox "There is no data to edit"
Exit Sub
End If
'clear the listbox
lstenq.RowSource = ""
'find the row to edit
Set findvalue = DataSH.Range("A:A"). _
Find(What:=txtup1.Value, LookIn:=xlValues, LookAt:=xlWhole)
'update the values
findvalue = txtup1.Value
findvalue.Offset(0, 5) = cboup3.Value
findvalue.Offset(0, 6) = cboup4.Value
findvalue.Offset(0, 7) = cboup5.Value
findvalue.Offset(0, 8) = cboup6.Value
'unprotect the worksheets for the advanced filter
'Unprotect_All
'filter the data
DataSH.Range("A8").CurrentRegion.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Range("Data!$P$8:$P$9"), CopyToRange:=Range("Data!$R$8:$AE$8"), _
Unique:=False
'if no data exists then clear the rowsource
If DataSH.Range("P9").Value = "" Then
lstenq.RowSource = ""
Else
'add the filtered data to the rowsource
lstenq.RowSource = DataSH.Range("outdata").Address(external:=True)
End If
'return to sheet
Sheet2.Select
'Protect all sheets
'Protect_All
'error block
On Error GoTo 0
Exit Sub
errHandler:
'Protect all sheets
'Protect_All
'show error information in a messagebox
MsgBox "An Error has Occurred " & vbCrLf & _
"The error number is: " & Err.Number & vbCrLf & _
Err.Description & vbCrLf & "Please notify the administrator"
End Sub
任何帮助将不胜感激
【问题讨论】:
-
你在哪一行得到错误?
-
我猜是 txtup1 是表单上文本框的名称吗?
-
大家好,textup1 是用户表单中的第一个文本框。
-
我发布该代码是为了说明从列表框中填充文本框的用户表单初始化。
-
嗨@Shai Rado 给我错误的行是 findvalue = txtup1.Value 但是如果我将鼠标移到 findvalue 部分上,它会显示正确的搜索行。我的 A 列已设置为特殊,因此我可以有 4 个以 0001 开头的数字字符