【问题标题】:Use VBA Userform to update active row使用 VBA Userform 更新活动行
【发布时间】:2017-05-31 13:41:12
【问题描述】:

当我点击更新按钮时,我加载用户表单并编辑数据,表格的最后一行被更新,而不是我选择的行

Private Sub UpdateExpenses_Click()

ModifyTableRow ExpensesTable.ListRows(CurrentRow).Range

UpdatePositionCaption

End Sub

Private Sub ModifyTableRow(TableRow As Range)

With TableRow

    .Cells(1, 1) = Calendar1.Value
    .Cells(1, 2) = StaffName.Value
    .Cells(1, 4) = SystemID.Value
    .Cells(1, 6) = SystemAEnd.Value
    .Cells(1, 7) = SystemBEnd.Value
    .Cells(1, 3) = CircuitDesc.Value
    .Cells(1, 9) = CircuitStatus.Value
    .Cells(1, 10) = Comments.Value
    .Cells(1, 8) = TypeofCircuit.Value
    .Cells(1, 5) = ChannelNum.Value



End With

ChangeRecord.Max = ExpensesTable.ListRows.Count

结束子

任何有关此代码的帮助都会非常感谢

【问题讨论】:

  • 我怀疑当您调用表单时,您的“活动行”失去了焦点
  • 贴出ModifyTableRow的代码,或者相关部分。

标签: vba excel


【解决方案1】:

试试这个来获取当前表的活动行。找到here 最初由@Anand 回答

Sub FindRowNoInTable()

Dim ObjSheet As Worksheet
Dim startRow, ActiveRow, ActiveCol
Dim ObjList As ListObject
Set ObjSheet = ActiveSheet
ActiveRow = ActiveCell.Row
ActiveCol = ActiveCell.Column
For Each ObjList In ObjSheet.ListObjects
        Application.Goto ObjList.Range
        startRow = ObjList.Range.Row
Next
Debug.Print (ActiveRow - startRow)

End Sub

然后将(ActiveRow - startRow)放入变量“CurrentRow”

【讨论】:

  • 我添加了上面的代码并得到了编译错误:变量未定义
  • 此子例程中的所有变量均由 dim 语句定义,因此您不会从此代码中收到该错误。是否有任何工作表子例程在后台工作?其他原因导致该错误。多次测试此代码,有无“选项显式”
【解决方案2】:

在玩弄了代码之后,这个解决方案对我有用

Option Explicit
Private ExpensesTable As ListObject
Private CurrentRow As Long
Private WithEvent`enter code here`s Calendar1 As cCalendar

Private Sub UpdateExpenses_Click()
CurrentRow = ActiveCell.Row
Cells(CurrentRow, 2) = Calendar1.Value
Cells(CurrentRow, 3) = Me.StaffName.Value
Cells(CurrentRow, 4) = Me.CircuitDesc.Value
Cells(CurrentRow, 5) = Me.SystemID.Value
Cells(CurrentRow, 6) = Me.ChannelNum.Value
Cells(CurrentRow, 7) = Me.SystemAEnd.Value
Cells(CurrentRow, 8) = Me.SystemBEnd.Value
Cells(CurrentRow, 9) = Me.TypeofCircuit.Value
Cells(CurrentRow, 10) = Me.CircuitStatus.Value
Cells(CurrentRow, 11) = Me.Comments.Value
Unload ExpensesForm

End Sub

【讨论】:

    猜你喜欢
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多