excel 用户表单中导航期间达到限制的警报消息
Alert message for reaching a limit during navigation in excel userform
我正在使用以下代码从 excel sheet 中的表格数据填充用户表单,并尝试使用下一个和上一个按钮循环浏览 table 中的上一个条目并具有成功完成。使用此代码,上一个按钮选择上一行并将其加载到用户窗体中。下一个按钮选择下一行并将其也加载到用户窗体中。
问题:我只想将循环限制在table(工作sheet中的第339行到第390行)。我想修复此代码以将导航限制为 table 并显示有关到达的第一条记录和最后一条记录的警报消息。我已成功修复并测试了 "next" 按钮的代码,但前一个按钮的代码不起作用。
共享整个代码供您审阅和建议,我们将不胜感激。
Private Sub cmdGetNext_Click()
Range("B339").Select
ActiveCell.End(xlDown).Select
lastRow = ActiveCell.Row
currentrow = currentrow + 1
If currentrow = lastRow + 1 Then
currentrow = lastRow
MsgBox "You have reached the last entered data!"
End If
txtmeas.Text = Cells(currentrow, 2).Value
txtsource.Text = Cells(currentrow, 4).Value
cmbmatric.Text = Cells(currentrow, 6).Value
End Sub
Private Sub cmdPreviousData_Click()
currentrow = currentrow - 1
If currentrow > 1 Then
txtmeas.Text = Cells(currentrow, 2).Value
txtsource.Text = Cells(currentrow, 4).Value
cmbmatric.Text = Cells(currentrow, 6).Value
ElseIf currentrow = 1 Then
MsgBox "This is your first record!"
currentrow = currentrow + 1
End If
End Sub
我会在模块级别定义一个常量,代表您要设置的table的第一行:Const TblFirstRow
。
那么下面,可以用Range("B" & TblFirstRow)
代替Range("B339")
,以后修改起来会更加动态
代码
Option Explicit
' define a variable at the module level, for first row of the table
Const TblFirstRow As Long = 339
Private Sub cmdGetNext_Click()
Dim LastRow As Long, CurrentRow As Long
' get last row (not skipping blank cells)
LastRow = Range("B" & TblFirstRow).End(xlDown).Row
If CurrentRow + 1 > LastRow Then
CurrentRow = LastRow
MsgBox "You have reached the last entered data!"
Else
CurrentRow = CurrentRow + 1
End If
txtmeas.Text = Cells(CurrentRow, 2).Value
txtsource.Text = Cells(CurrentRow, 4).Value
cmbmatric.Text = Cells(CurrentRow, 6).Value
End Sub
'================================================================
Private Sub cmdPreviousData_Click()
If CurrentRow - 1 < TblFirstRow Then
CurrentRow = TblFirstRow
MsgBox "This is your first record!"
Else
CurrentRow = CurrentRow - 1
End If
txtmeas.Text = Cells(CurrentRow, 2).Value
txtsource.Text = Cells(CurrentRow, 4).Value
cmbmatric.Text = Cells(CurrentRow, 6).Value
End Sub
我会将 "elseif currentrow = 1 then" 更改为 "else"。
如果您从 currentrow = 1 和 运行 cmdPreviousData_Click() 开始,那么您在第一行中将 currentrow 的值从 1 减少到 0 (currentrow = currentrow - 1)。那么你的 if 语句不是 运行 因为 0 不是 >1 而你的 elseif 也不是 运行 因为 0 <>1.
我正在使用以下代码从 excel sheet 中的表格数据填充用户表单,并尝试使用下一个和上一个按钮循环浏览 table 中的上一个条目并具有成功完成。使用此代码,上一个按钮选择上一行并将其加载到用户窗体中。下一个按钮选择下一行并将其也加载到用户窗体中。
问题:我只想将循环限制在table(工作sheet中的第339行到第390行)。我想修复此代码以将导航限制为 table 并显示有关到达的第一条记录和最后一条记录的警报消息。我已成功修复并测试了 "next" 按钮的代码,但前一个按钮的代码不起作用。
共享整个代码供您审阅和建议,我们将不胜感激。
Private Sub cmdGetNext_Click()
Range("B339").Select
ActiveCell.End(xlDown).Select
lastRow = ActiveCell.Row
currentrow = currentrow + 1
If currentrow = lastRow + 1 Then
currentrow = lastRow
MsgBox "You have reached the last entered data!"
End If
txtmeas.Text = Cells(currentrow, 2).Value
txtsource.Text = Cells(currentrow, 4).Value
cmbmatric.Text = Cells(currentrow, 6).Value
End Sub
Private Sub cmdPreviousData_Click()
currentrow = currentrow - 1
If currentrow > 1 Then
txtmeas.Text = Cells(currentrow, 2).Value
txtsource.Text = Cells(currentrow, 4).Value
cmbmatric.Text = Cells(currentrow, 6).Value
ElseIf currentrow = 1 Then
MsgBox "This is your first record!"
currentrow = currentrow + 1
End If
End Sub
我会在模块级别定义一个常量,代表您要设置的table的第一行:Const TblFirstRow
。
那么下面,可以用Range("B" & TblFirstRow)
代替Range("B339")
,以后修改起来会更加动态
代码
Option Explicit
' define a variable at the module level, for first row of the table
Const TblFirstRow As Long = 339
Private Sub cmdGetNext_Click()
Dim LastRow As Long, CurrentRow As Long
' get last row (not skipping blank cells)
LastRow = Range("B" & TblFirstRow).End(xlDown).Row
If CurrentRow + 1 > LastRow Then
CurrentRow = LastRow
MsgBox "You have reached the last entered data!"
Else
CurrentRow = CurrentRow + 1
End If
txtmeas.Text = Cells(CurrentRow, 2).Value
txtsource.Text = Cells(CurrentRow, 4).Value
cmbmatric.Text = Cells(CurrentRow, 6).Value
End Sub
'================================================================
Private Sub cmdPreviousData_Click()
If CurrentRow - 1 < TblFirstRow Then
CurrentRow = TblFirstRow
MsgBox "This is your first record!"
Else
CurrentRow = CurrentRow - 1
End If
txtmeas.Text = Cells(CurrentRow, 2).Value
txtsource.Text = Cells(CurrentRow, 4).Value
cmbmatric.Text = Cells(CurrentRow, 6).Value
End Sub
我会将 "elseif currentrow = 1 then" 更改为 "else"。
如果您从 currentrow = 1 和 运行 cmdPreviousData_Click() 开始,那么您在第一行中将 currentrow 的值从 1 减少到 0 (currentrow = currentrow - 1)。那么你的 if 语句不是 运行 因为 0 不是 >1 而你的 elseif 也不是 运行 因为 0 <>1.