使用VBA Userform更新活动行

时间:2017-05-31 13:41:13

标签: excel vba excel-vba

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

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

End Sub

任何有关此代码的帮助都会非常适合

2 个答案:

答案 0 :(得分:0)

尝试此操作以获取当前表的活动行。找到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”

答案 1 :(得分:0)

在玩完代码之后,这个解决方案为我工作了

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