Hi,
erstelle ein UserForm und gib ihm den Namen frmRunden. Erstelle darauf 4 OptionsButton und benenne sie opbRunden, opbAufrunden, opbAbrunden, opbRunden sowie eine TextBox mit Namen tbFaktor und einen CommandButton mit Namen cmbUebernehmen.
Mache dann einen Doppelklick auf das UserForm und kopiere den folgenden Code in das rechte (obere) Codefenster:
Private Sub cmbUebernehmen_Click()
Dim strFormel As String
Dim coElement As Control
If Selection.Count > 1 Then
MsgBox "Bitte nur 1 Zelle auswählen"
Else
If ActiveCell.HasFormula Then
strFormel = ActiveCell.FormulaLocal
If opbLoeschen Then
If InStr(ActiveCell.FormulaLocal, "AUFRUNDEN(") > 0 Then
strFormel = Mid(strFormel, 12)
strFormel = Mid(strFormel, 1, InStrRev(strFormel, ";") - 1)
ActiveCell.FormulaLocal = "=" & strFormel
ElseIf InStr(ActiveCell.FormulaLocal, "ABRUNDEN(") > 0 Then
strFormel = Mid(strFormel, 11)
strFormel = Mid(strFormel, 1, InStrRev(strFormel, ";") - 1)
ActiveCell.FormulaLocal = "=" & strFormel
ElseIf InStr(ActiveCell.FormulaLocal, "RUNDEN(") > 0 Then
strFormel = Mid(strFormel, 9)
strFormel = Mid(strFormel, 1, InStrRev(strFormel, ";") - 1)
ActiveCell.FormulaLocal = "=" & strFormel
End If
Else
If IsNumeric(tbFaktor) Then
If InStr(ActiveCell.FormulaLocal, "AUFRUNDEN(") > 0 Then
strFormel = Mid(strFormel, 12)
strFormel = Mid(strFormel, 1, InStrRev(strFormel, ";") - 1)
ActiveCell.FormulaLocal = "=" & strFormel
ElseIf InStr(ActiveCell.FormulaLocal, "ABRUNDEN(") > 0 Then
strFormel = Mid(strFormel, 11)
strFormel = Mid(strFormel, 1, InStrRev(strFormel, ";") - 1)
ActiveCell.FormulaLocal = "=" & strFormel
ElseIf InStr(ActiveCell.FormulaLocal, "RUNDEN(") > 0 Then
strFormel = Mid(strFormel, 9)
strFormel = Mid(strFormel, 1, InStrRev(strFormel, ";") - 1)
ActiveCell.FormulaLocal = "=" & strFormel
End If
If opbAufrunden Then
ActiveCell.FormulaLocal = "=AUFRUNDEN(" & Application.Substitute(ActiveCell.FormulaLocal, "=", "") & ";" & CInt(tbFaktor) & ")"
ElseIf opbAbrunden Then
ActiveCell.FormulaLocal = "=ABRUNDEN(" & Application.Substitute(ActiveCell.FormulaLocal, "=", "") & ";" & CInt(tbFaktor) & ")"
Else
ActiveCell.FormulaLocal = "=RUNDEN(" & Application.Substitute(ActiveCell.FormulaLocal, "=", "") & ";" & CInt(tbFaktor) & ")"
End If
Else
MsgBox "Bitte eine Zahl eingeben"
End If
End If
End If
End If
End Sub
Erstelle außerdem ein allgemeines Modul und kopiere dort diesen Code:
Sub Starten()
If ActiveCell.HasFormula Then
frmRunden.Show
Else
MsgBox "Diese Zelle enthält keine Formel"
End If
End Sub
Dieses Makro kannst du einer Tastenkombination zuweisen oder du erstellst einen Schalter im Tabellenblatt und weist ihm den Code zu.
Wenn du das UserForm startest, kannst du einen der 4 OptionsButton auswählen und die in der Zelle vorhandene Formel wird entsprechend geändert. Der OptionsButton opbLoeschen bewirkt, dass alle Formeln zurückgesetzt werden, d.h. sie werden ohne Rundungsfunktion in die Zelle geschrieben. Das betrifft natürlich auch Formeln, die du von Hand mit einer der Rundungsfunktionen erstellt hast.
Bis später,
Karin