【发布时间】:2018-09-13 11:44:03
【问题描述】:
我正在尝试提供输入数据并在按下按钮后自动移动到表格的用户表单。
我的用户表单由两个选项按钮(帮助区分输入数据的放置位置)和两个文本框组成:一个用于输入条件,另一个用于输入日期,必须将它们放入符合条件的单元格中。到目前为止,我想出了这个代码:
Private Sub UzdarytiCB_Click()
Dim i As Long
'setting conditions for first option button selection
If OptionButton1 = True And Condition_TextBox <> "" _
'with other subs I have made default text in texbox which is shown until clicked
'this line should stop macro from starting if no text is entered and default text left
And Condition_TB <> "Enter Application ID" Then
' I want macro to loop throught all the table until it finds a match
For i = 3 To Range("Table1").Rows.Count
'Conditions to finding a match. Cell in column D should say "Remarks" and entered value should match cell in column A
If Condition_TB = Worksheets("sheet1").Cells(i, "A").Value _
And Worksheets("sheet1").Cells(i, "D").Value = "Remarks" Then
'If conditions are met I want macro to change column D cell value to "done" and write date from second texbox to cell in column N
Worksheets("sheet1").Cells(i, "N").Value = Condition_TB
Worksheets("sheet1").Cells(i, "D").Value = "Done"
'Since there will be no more mach I want to stop the loop
Exit For
End If
Next i
' Second option button similar to first, but it should not stop after first match
ElseIf OptionButton2 = True And Condition_TextBox <> "" _
And Condition_TB <> "Enter Application ID" _
And Condition_TB <> "Enter item number" Then
For i = 3 To Range("Table1").Rows.Count
If Condition_TB = Worksheets("sheet1").Cells(i, "P").Value _
And Worksheets("sheet1").Cells(i, "D").Value = "Make a call" Then
Worksheets("sheet1").Cells(i, "L").Value = DataTB
Worksheets("sheet1").Cells(i, "D").Value = "Call made"
End If
Next i
End If
End Sub
到目前为止,整个代码没有显示任何错误,但随后单击并输入了数据,没有任何反应。 由于我是初学者,整个代码可能本质上是错误的,请帮助我找出错误或进入正确的轨道。
【问题讨论】:
-
第一个 If 语句中的 Then 在哪里?
-
对不起,我不小心把它弄丢了,然后编辑了代码以在帖子中看起来清晰,在原始文件中 然后 已经到位。我编辑了我的帖子。
-
那么 Condition_TB 可以是“输入应用程序 ID”或“输入项目编号”?
-
是的,是在 Condition_TB 中显示的默认文本,直到单击它并输入不同的文本。如果是“输入应用程序 ID”或“输入项目编号”取决于选项按钮选择。它们都是由另一个宏生成的,但我认为它不会干扰这个。
标签: vba excel error-handling