2016-06-22 1 views
0

を選択した場合、私は私がの一番下に追加されるデータの新しい行を探していますコードのビットを持っているワークシート上の特定のテーブルに入力されましたワークシートの表移入する必要があるワークシートは、Sheet3(「データシート」)またはタブ上のデータシートだけです。は、データに新しい行を追加し、特定のリストボックスの項目がエクセル2013 VBAに</p> <p>を使用して

ワークシートには2つのテーブル(表3と表4)があり、リストボックス(StatusListBox)から選択されたカテゴリに応じて、2つのテーブルのいずれかの下部にデータ行が追加されます。

ユーザが「Live」、「Secured」または「Completed」を選択した場合、Table3を入力する必要がありますが、ユーザが「入札(Pipeline)」、「入札(交渉)」または「入札4移入されるべきである。

をIデータの新しい行を表3及び表4の両者の底部に添加される持って現在のコードである。

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

'set'ステートメント(例えば' Set table_object_rowA = table_list_objectA.ListRows.Add')は、彼らが言うように、テーブルに行を追加しますか?また、行の値を設定する前に、 'Me.ProjectNameTextBox.Value'をチェックしてコードをさらに上に移動することもできます。 – Dave

答えて

0

ListIndexプロパティ戻りますリストボックスリスト内の項目位置、whi (何の項目が選択されていない場合)ル.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 
+0

このメッセージは無視してください。私は間違ったスペースに入力を開始しました – Robertscoder

+0

@Nisaa Roberts:あなたが作業として投稿したコードは、私がこの回答に投稿したコードです。次に、この回答に合格とマークしてください。ありがとうございました – user3598756

0

貢献した。

私はこれが動作するようになりましたコードである.ListObjectsと.ListObjects( "表3")(tableNameの)

を置き換える:

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 

感謝を。

関連する問題