2017-05-31 55 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」 –