使用Excel 2013 VBA
我有一些代码,我正在寻找一个新的数据行添加到工作表的底部。需要填充的工作表是Sheet3(“数据表”)或选项卡上的数据表。
在工作表上有两个表(Table3和Table4),根据从列表框中选择的类别(称为StatusListBox),数据行将添加到两个表中任何一个的底部。
如果用户选择“Live”,“Secured”或“Completed”,则应填充Table3,但如果用户选择“Tender(Pipeline”),Tender(Negotiated)或“Tender(Favorable)”,则表4应填充。
使用当前代码我将新的数据行添加到BOTH Table3和Table4的底部。
Private Sub AddNewButton_Click()
Dim the_sheet As Worksheet
Dim table_list_object As ListObject
Dim table_object_row As ListRow
Set the_sheet = Sheets("Data Sheet")
Set table_list_objectA = the_sheet.ListObjects("Table3")
Set table_list_objectB = the_sheet.ListObjects("Table4")
Set table_object_rowA = table_list_objectA.ListRows.Add
Set table_object_rowB = table_list_objectB.ListRows.Add
If Me.StatusListBox.ListIndex = "Secured" Then
table_list_objectA.ListRows.Add
ElseIf Me.StatusListBox.ListIndex = "Live" Then
table_list_objectA.ListRows.Add
ElseIf Me.StatusListBox.ListIndex = "Completed" Then
table_list_objectA.ListRows.Add
ElseIf Me.StatusListBox.ListIndex = "Tender (Pipeline)" Then
table_list_objectB.ListRows.Add
ElseIf Me.StatusListBox.ListIndex = "Tender (Negotiated)" Then
table_list_objectB.ListRows.Add
ElseIf Me.StatusListBox.ListIndex = "Tender (Favourable)" Then
table_list_objectB.ListRows.Add
End If
table_object_rowA.Range(1, 1).Value = ProjectNameTextBox.Value
table_object_rowB.Range(1, 1).Value = ProjectNameTextBox.Value
last_row_with_data = the_sheet.Range("A65536").End(xlUp).Row
last_row_with_data = last_row_with_data
the_sheet.Range("B" & last_row_with_data) = ClientTextBox.Value
the_sheet.Range("C" & last_row_with_data) = SectorListBox.Value
the_sheet.Range("D" & last_row_with_data) = StatusListBox.Value
the_sheet.Range("E" & last_row_with_data) = ContractValueTextBox.Value
the_sheet.Range("F" & last_row_with_data) = AFATextBox.Value
the_sheet.Range("G" & last_row_with_data) = RTPTextBox.Value
the_sheet.Range("H" & last_row_with_data) = TwentyFifteenTextBox.Value
the_sheet.Range("I" & last_row_with_data) = TwentySixteenTextBox.Value
the_sheet.Range("J" & last_row_with_data) = TwentySeventeenTextBox.Value
the_sheet.Range("K" & last_row_with_data) = TwentyEighteenTextBox.Value
the_sheet.Range("L" & last_row_with_data) = TwentyNineteenTextBox.Value
the_sheet.Range("M" & last_row_with_data) = DisciplineListBox.Value
the_sheet.Range("N" & last_row_with_data) = BoardDirectorListBox.Value
the_sheet.Range("O" & last_row_with_data) = AssociateDirectorTextBox.Value
the_sheet.Range("P" & last_row_with_data) = CommercialManagerTextBox.Value
the_sheet.Range("Q" & last_row_with_data) = ProjectManagerTextBox.Value
the_sheet.Range("R" & last_row_with_data) = QuantitySurveyorTextBox.Value
the_sheet.Range("S" & last_row_with_data) = PreConTextBox.Value
the_sheet.Range("T" & last_row_with_data) = ActualTextBox.Value
the_sheet.Range("U" & last_row_with_data) = DPStartTextBox.Value
the_sheet.Range("V" & last_row_with_data) = DPEndTextBox.Value
If Me.ProjectNameTextBox.Value = "" Then
MsgBox "Please enter Project Name.", vbExclamation, "Project Tracker Template"
Me.ProjectNameTextBox.SetFocus
End If
End Sub
感谢任何帮助。
答案 0 :(得分:0)
ListIndex
属性返回列表框列表中的项目位置,而.Value
属性返回其选定值(如果有)或错误(如果没有选择项目)
所以你可能需要以下内容:
Option Explicit
Private Sub AddNewButton_Click()
Dim the_sheet As Worksheet
Dim table_list_object As ListObject
Dim table_object_row As ListRow
Dim last_row_with_data As Long
Dim tableName As String, errMsg As String
If Me.ProjectNameTextBox.Value = "" Then
MsgBox "Please enter Project Name.", vbExclamation, "Project Tracker Template"
Me.ProjectNameTextBox.SetFocus
End If
With Me.StatusListBox
If .ListIndex <> -1 Then
Select Case .Value
Case "Secured", "Live", "Completed"
tableName = "Table3"
Case "Tender (Pipeline)", "Tender (Negotiated)", "Tender (Favourable)"
tableName = "Table4"
Case Else
errMsg = "No valid item selected!"
End Select
Else
errMsg = "No item selected!"
End If
End With
If tableName = "" Then
MsgBox errMsg, vbCritical
Exit Sub
End If
Set the_sheet = Sheets("Data Sheet")
With the_sheet
Set table_list_object = .ListObjects("Table3")
Set table_object_row = table_list_object.ListRows.Add
table_object_row.Range(1, 1).Value = ProjectNameTextBox.Value
last_row_with_data = .Range(.Rows.Count, 1).End(xlUp).row + 1
.Range("B" & last_row_with_data) = ClientTextBox.Value
.Range("C" & last_row_with_data) = SectorListBox.Value
.Range("D" & last_row_with_data) = StatusListBox.Value
.Range("E" & last_row_with_data) = ContractValueTextBox.Value
.Range("F" & last_row_with_data) = AFATextBox.Value
.Range("G" & last_row_with_data) = RTPTextBox.Value
.Range("H" & last_row_with_data) = TwentyFifteenTextBox.Value
.Range("I" & last_row_with_data) = TwentySixteenTextBox.Value
.Range("J" & last_row_with_data) = TwentySeventeenTextBox.Value
.Range("K" & last_row_with_data) = TwentyEighteenTextBox.Value
.Range("L" & last_row_with_data) = TwentyNineteenTextBox.Value
.Range("M" & last_row_with_data) = DisciplineListBox.Value
.Range("N" & last_row_with_data) = BoardDirectorListBox.Value
.Range("O" & last_row_with_data) = AssociateDirectorTextBox.Value
.Range("P" & last_row_with_data) = CommercialManagerTextBox.Value
.Range("Q" & last_row_with_data) = ProjectManagerTextBox.Value
.Range("R" & last_row_with_data) = QuantitySurveyorTextBox.Value
.Range("S" & last_row_with_data) = PreConTextBox.Value
.Range("T" & last_row_with_data) = ActualTextBox.Value
.Range("U" & last_row_with_data) = DPStartTextBox.Value
.Range("V" & last_row_with_data) = DPEndTextBox.Value
End With
End Sub
答案 1 :(得分:0)
我已经设法解决了这个问题,感谢那些贡献的人。
我用.ListObjects(tableName)
替换了.ListObjects(“Table3”)这是现在可以使用的代码:
Option Explicit
Private Sub AddNewButton_Click()
Dim the_sheet As Worksheet
Dim table_list_object As ListObject
Dim table_object_row As ListRow
Dim last_row_with_data As Long
Dim tableName As String, errMsg As String
If Me.ProjectNameTextBox.Value = "" Then
MsgBox "Please enter Project Name.", vbExclamation, "Project Tracker Template"
Me.ProjectNameTextBox.SetFocus
End If
With Me.StatusListBox
If .ListIndex <> -1 Then
Select Case .Value
Case "Secured", "Live", "Completed"
tableName = "Table3"
Case "Tender (Pipeline)", "Tender (Negotiated)", "Tender (Favourable)"
tableName = "Table4"
Case Else
errMsg = "No valid item selected!"
End Select
Else
errMsg = "No item selected!"
End If
End With
If tableName = "" Then
MsgBox errMsg, vbCritical
Exit Sub
End If
Set the_sheet = Sheets("Data Sheet")
With the_sheet
Set table_list_object = .ListObjects(tableName)
Set table_object_row = table_list_object.ListRows.Add
last_row_with_data = the_sheet.Range("A65536").End(xlUp).Row
table_object_row.Range(1, 1).Value = ProjectNameTextBox.Value
table_object_row.Range(1, 2).Value = ClientTextBox.Value
table_object_row.Range(1, 3).Value = SectorListBox.Value
table_object_row.Range(1, 4).Value = StatusListBox.Value
table_object_row.Range(1, 5).Value = ContractValueTextBox.Value
table_object_row.Range(1, 6).Value = AFATextBox.Value
table_object_row.Range(1, 7).Value = RTPTextBox.Value
table_object_row.Range(1, 8).Value = TwentyFifteenTextBox.Value
table_object_row.Range(1, 9).Value = TwentySixteenTextBox.Value
table_object_row.Range(1, 10).Value = TwentySeventeenTextBox.Value
table_object_row.Range(1, 11).Value = TwentyEighteenTextBox.Value
table_object_row.Range(1, 12).Value = TwentyNineteenTextBox.Value
table_object_row.Range(1, 13).Value = DisciplineListBox.Value
table_object_row.Range(1, 14).Value = BoardDirectorListBox.Value
table_object_row.Range(1, 15).Value = AssociateDirectorTextBox.Value
table_object_row.Range(1, 16).Value = CommercialManagerTextBox.Value
table_object_row.Range(1, 17).Value = ProjectManagerTextBox.Value
table_object_row.Range(1, 18).Value = QuantitySurveyorTextBox.Value
table_object_row.Range(1, 19).Value = PreConTextBox.Value
table_object_row.Range(1, 20).Value = ActualTextBox.Value
table_object_row.Range(1, 21).Value = DPStartTextBox.Value
table_object_row.Range(1, 22).Value = DPEndTextBox.Value
End With
End Sub
非常感谢。