2016-04-04 10 views
0

Vielen Dank für die Überprüfung meiner Frage :) Ich erstellen ein Textfeld auf einem Benutzerformular zur Laufzeit & möchte gefilterte Daten darauf angezeigt werden. Ich habe zu viele Laufzeitfehler für den folgenden Code ausgeführt. Für das erste Textfeld möchte ich einen fatalcount anzeigen, für das zweite Textfeld sollte es Majorcount & für das letzte Textfeld sein, es sollte minorcount sein. Kann mir jemand den richtigen Weg weisen? danke im vorausSo zeigen Sie einen gefilterten Wert in einem Textfeld des Benutzerformulars in VBA zur Laufzeit

Private Sub UserForm_Initialize() 

Set sh = ThisWorkbook.Sheets("Testing") 

sh.Range("F21").Activate 

With sh 

    fatalcount = WorksheetFunction.CountIf(Range("F:F"), "Fatal") 
    'MsgBox fatalcount 
    Majorcount = WorksheetFunction.CountIf(Range("F:F"), "Major") 
    'MsgBox Majorcount 
    Minorcount = WorksheetFunction.CountIf(Range("F:F"), "Minor") 

    'MsgBox Minorcount 

'Add a text box at run time 

Dim txtB1 As Control 

Dim i 

For i = 0 To 5 
    Set txtB1 = Me.Controls.Add("Forms.TextBox.1") 
    With txtB1 
     .Name = "chkDemo" & i 
     .Height = 20 
     .Width = 100 
     .Left = 12 
     .Top = 15 * i * 2 
     .Text.i = fatalcount ' problem lines 
     .Text.i 1 = Majorcount 
     .Text.i 2 = Minorcount 

    End With 
Next i 
End Sub 

Antwort

1

Hoffe das ist, was Sie suchen.

Private Sub UserForm_Initialize() 
    Set sh = ThisWorkbook.Sheets("Testing") 
    sh.Range("F21").Activate 
    With sh 
     fatalcount = WorksheetFunction.CountIf(Range("F:F"), "Fatal") 
     Majorcount = WorksheetFunction.CountIf(Range("F:F"), "Major") 
     Minorcount = WorksheetFunction.CountIf(Range("F:F"), "Minor") 
    End With 
    Dim txtB1 As Control 
    Dim i 
    For i = 0 To 2 
     Set txtB1 = UserForm1.Controls.Add("Forms.TextBox.1") 
     With txtB1 
      .Name = "chkDemo" & i 
      .Height = 20 
      .Width = 100 
      .Left = 12 
      .Top = 15 * i * 2 
     End With 
    Next i 
    Dim tbox As Control 
    For Each tbox In UserForm1.Controls 
     If tbox.Name = "chkDemo0" Then 
      tbox.Value = fatalcount 
     ElseIf tbox.Name = "chkDemo1" Then 
      tbox.Value = Majorcount 
     ElseIf tbox.Name = "chkDemo2" Then 
      tbox.Value = Minorcount 
     End If 
    Next 
End Sub 
+0

danke. Ich möchte, dass das Textfeld horizontal und nicht eins unter anderem angezeigt wird :) –

Verwandte Themen