长时间的用户,这里的第一次问询者。全新的到VBA,有点挣扎。在每一篇文章中,我都对Excel中的Next record按钮功能进行了回顾和尝试,但似乎无法找到适合我的解决方案。
我创建了一个用户表单,旨在帮助用户查看Excel电子表格中非常广泛的数据(如果需要/帮助,可以提供工作表的示例)。由于用户的需求,当整个行被选择为时,表单需要启动(这就是问题所在)。除了下一个按钮和以前的按钮之外,我已经设法使大多数控件按照预期的方式运行。我的麻烦似乎在于从工作表>txt.rownum例程到按钮中获取‘Target.Row’(Target.Row)。下面是表格的图形:
在工作表例程中,表单将当前行捕获为UserForm1.txt_rownum = Target.Row
。下面是代码的第一部分,它确认已经选择了整行并填充了表单:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A2:CE1000")) Is Nothing Then
If Target.Address = Target.EntireRow.Address Then
'yes, full row is selected
UserForm1.txt_prop_name = Cells(Target.Row, 1)
UserForm1.txt_alt_prop_name = Cells(Target.Row, 2)
UserForm1.txt_client_prop_code = Cells(Target.Row, 3)
UserForm1.txt_rownum = Target.Row
UserForm1.txt_mailability_score = Cells(Target.Row, 4)
UserForm1.txt_ad_descr = Cells(Target.Row, 5)...
Next按钮后面的当前代码如下所示:
Dim currentrow As Long
'Next button
Private Sub CommandButton2_Click()
Dim lastrow As Long
lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
currentrow = currentrow + 1
UserForm1.txt_prop_name = Cells(currentrow, 1)
UserForm1.txt_alt_prop_name = Cells(currentrow, 2)
UserForm1.txt_client_prop_code = Cells(currentrow, 3)
End Sub
当单击Next按钮时,表单将正确地向下索引一行,但它从A1开始,而不是在窗体显示的当前行开始。如何将txt.rownum (或Target.Row)传递给Next按钮,以便它能够正确地从当前行移动到下一条记录,并显示电子表格中生成的下一行数据?
谢谢您的帮助和耐心-非常感谢!
发布于 2018-01-15 17:29:37
一些守则建议:
这样,您就不再在两个函数之间重复代码。
公共RowNumber作为整数
Sub PopulateForm()
UserForm1.txt_prop_name = Cells(Target.Row, 1)
UserForm1.txt_alt_prop_name = Cells(Target.Row, 2)
UserForm1.txt_client_prop_code = Cells(Target.Row, 3)
UserForm1.txt_rownum = Target.Row
UserForm1.txt_mailability_score = Cells(Target.Row, 4)
UserForm1.txt_ad_descr = Cells(Target.Row, 5)...
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A2:CE1000")) Is Nothing Then // Whatever This Is?
If Target.Address = Target.EntireRow.Address Then
RowNumber = Target.Row
Call PopulateForm
End If
End If
End Sub
Private Sub CommandButton2_Click()
RowNumber = RowNumber + 1
Call PopulateForm
End Sub
https://stackoverflow.com/questions/48272241
复制