VBA OpitonsButton
29.07.2015 12:40:16
Nicolai
ich stehe vor einem Problem. Ich habe eine Userform mit mehreren OptionButtons.
Angehängtes Makro funktioniert auch, allerdings möchte ich den unteren Code auslagern.
Dim TextKommentar As String
Dim AktZeile As Integer
Dim AktSpalte As Integer
AktZeile = ActiveCell.Row
AktSpalte = ActiveCell.Column
Dim wert1 As Double 'Typendeklaration wichtig für dezimale Stundenangaben, z.B. 1,5!
Dim wert2 As Double
wert1 = ActiveCell.Value
wert2 = UserForm2.TextBox2.Value
Kommentartext = UserForm2.TextBox3.Value
akennz = -1
For i = 1 To 32
If i = 19 Then
'ignorieren
ElseIf Controls("OptionButton" & CStr(i)).Value Then
akennz = i
TextKommentar = Controls("OptionButton" & CStr(i)).Caption
Exit For
End If
Next
Diesen Teil möchte ich auslagern:
If akennz = 1 Then
If ActiveSheet.Cells(AktZeile + 1, AktSpalte).Value = "" Then
Cells(AktZeile + 1, AktSpalte).Value = " | " & Format(Date, "DD.MM.") & " - _
_
_
" & Application.UserName & ": " & wert2 & " Stunde(n) " & TextKommentar & Chr(34) & _
Kommentartext & Chr(34) & " " & "|" & " "
Else
vorhandener_Kommentar = Cells(AktZeile + 1, AktSpalte).Value
Cells(AktZeile + 1, AktSpalte).Value = ""
Cells(AktZeile + 1, AktSpalte).Value = vorhandener_Kommentar & " | " & Format( _
_
_
Date, "DD.MM.") & " - " & Application.UserName & ": " & wert2 & " Stunde(n) " & TextKommentar & _
_
Chr(34) & Kommentartext & Chr(34) & " " & "|" & " "
End If
'Fallunterscheidung für negative Stundenanzahl / Schichtunterscheidung, z.B. -7,5
If wert1
Neuer Code:
If akennz = 1 Then
Call Auswahl
Cells(331, ActiveCell.Column) = Cells(331, ActiveCell.Column) + wert2
End If
In dieser Zeile TextKommentar = Controls("OptionButton" & CStr(i)).Caption bringt er nun bei Controls immer eine Fehlermeldung. "Sub oder Function nicht definiert" Lasse ich die Zeile ganz weg funktioniert es nur im unteren Teil wo ich "TextKommentar" aufrufe schreibt er mir den die Caption des Optionsbutton.
Sub Auswahl()
Dim TextKommentar As String
Dim AktZeile As Integer
Dim AktSpalte As Integer
AktZeile = ActiveCell.Row
AktSpalte = ActiveCell.Column
Dim wert1 As Double
Dim wert2 As Double
Dim com As Comment
Dim akennz As Integer
TextKommentar = Controls("OptionButton" & CStr(i)).Caption
wert1 = ActiveCell.Value
wert2 = UserForm2.TextBox2.Value
Kommentartext = UserForm2.TextBox3.Value
'Kommentar hinzufügen
If ActiveSheet.Cells(AktZeile + 1, AktSpalte).Value = "" Then
Cells(AktZeile + 1, AktSpalte).Value = " | " & Format(Date, "DD.MM.") & " - _
_
_
" & Application.UserName & ": " & wert2 & " Stunde(n) " & TextKommentar & Chr(34) & _
Kommentartext & Chr(34) & " " & "|" & " "
Else
vorhandener_Kommentar = Cells(AktZeile + 1, AktSpalte).Value
Cells(AktZeile + 1, AktSpalte).Value = ""
Cells(AktZeile + 1, AktSpalte).Value = vorhandener_Kommentar & " | " & Format( _
_
_
Date, "DD.MM.") & " - " & Application.UserName & ": " & wert2 & " Stunde(n) " & TextKommentar & _
_
Chr(34) & Kommentartext & Chr(34) & " " & "|" & " "
End If
'Fallunterscheidung für negative Stundenanzahl / Schichtunterscheidung, z.B. -7,5
If wert1
Ich hoffe, ich habe es einigermaßen verständlich geschrieben und mir kann jemand weiterhelfen.
Vielen Dank & Grüße,
Nicolai