2017-05-31 36 views
0

我加载用户表单和编辑数据,当我点击更新按钮表的最后一行被更新,而不是我选择的行使用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 

末次

对此代码的任何帮助都会有很大的帮助

+0

我的代码玩耍后怀疑当你调用表单时,你的“活动行”失去焦点 –

+0

发布'ModifyTableRow'的代码或其相关部分。 –

回答

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 
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”

+0

我添加了上面的代码并得到了编译错误:变量未定义 – Andy

+0

该子例程中的所有变量均由dim语句定义,因此您没有从该代码中获得该错误。是否有任何工作表子例程在后台工作?别的东西正在导致这个错误。测试此代码几次,有和没有“Option Explicit” –