使用VBA用户窗体更新活动行

使用VBA用户窗体更新活动行,vba,excel,Vba,Excel,我加载用户表单并在单击“更新”按钮时编辑数据。表的最后一行将更新,而不是我选择的行 Private Sub UpdateExpenses_Click() ModifyTableRow ExpensesTable.ListRows(CurrentRow).Range UpdatePositionCaption End Sub Private Sub ModifyTableRow(TableRow As Range) With TableRow .Cells(1, 1) = Ca

我加载用户表单并在单击“更新”按钮时编辑数据。表的最后一行将更新,而不是我选择的行

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
端接头


任何有关此代码的帮助都将非常有用

请尝试获取当前表的活动行。建立 最初由@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”

中,在使用了这个解决方案对我有效的代码之后

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

我怀疑当您调用表单时,您的“活动行”丢失了焦点Post的代码
ModifyTableRow
,或者它的相关部分。我添加了上述代码并得到编译错误:变量未定义此子例程中的所有变量都是由dim语句定义的,因此您不会从该代码中得到该错误。是否有工作表子程序在后台工作?是其他原因导致了这个错误。多次测试此代码,无论是否使用“Option Explicit”