Gruppe
Dialog
Bereich
InputBox
Thema
Beispiele zum Einsatz der InputBox
Problem
10 Beispiele zum Einsatz der eingebauten InputBox
Lösung
Geben Sie den Code in ein Standardmodul ein und weisen Sie ihn Schaltflächen zu.
StandardModule: Modul1
Sub CallVBA()
Dim sTxt As String
sTxt = InputBox("Bitte Eingabe tätigen:")
If sTxt = "" Then Exit Sub
MsgBox sTxt
End Sub
Sub CallXL()
Dim vTxt As Variant
vTxt = Application.InputBox("Bitte Eingabe tätigen:")
If vTxt = False Then Exit Sub
MsgBox vTxt
End Sub
Sub SetPrompt()
Dim sTxt As String, sPrompt As String
sPrompt = "Nutzen Sie bitte folgende Syntax:" & vbLf & _
" 'dd.mm.yyyy' oder" & vbLf & _
" 'dd/mm/yyyy' oder" & vbLf & _
" 'dd-mm-yyyy':"
sTxt = InputBox(prompt:=sPrompt)
On Error GoTo ERRORHANDLER
MsgBox CDate(sTxt)
Exit Sub
ERRORHANDLER:
MsgBox "Kein gültiges Datumsformat!"
End Sub
Sub SetTitle()
Dim sTxt As String, sTitle As String
sTitle = Application.UserName & " says:"
sTxt = InputBox(prompt:="Eingabe:", Title:=sTitle)
If sTxt = "" Then Exit Sub
MsgBox sTxt
End Sub
Sub SetDefault()
Dim sTxt As String, sPrompt As String, sDefault As String
sPrompt = "Eingangsdatum eingeben:"
sDefault = Format(Date - 3, "dd.mm.yyyy")
sTxt = InputBox(prompt:=sPrompt, Default:=sDefault)
On Error GoTo ERRORHANDLER
MsgBox CDate(sTxt)
Exit Sub
ERRORHANDLER:
MsgBox "Kein gültiges Datumsformat!"
End Sub
Sub SelDefault()
Dim sTxt As String, sPrompt As String, sDefault As String
sPrompt = "Eingangsdatum eingeben:"
sDefault = Format(Date - 3, "dd.mm.yyyy")
SendKeys "{home}{right 3}+{right 2}"
sTxt = InputBox(prompt:=sPrompt, Default:=sDefault)
On Error GoTo ERRORHANDLER
MsgBox CDate(sTxt)
Exit Sub
ERRORHANDLER:
MsgBox "Kein gültiges Datumsformat!"
End Sub
Application.InputBox
Sub InputBoxPositionA()
Dim sTxt As String, sPrompt As String
sPrompt = "Eingabe:"
sTxt = InputBox(prompt:="Eingabe:", xpos:=1000, ypos:=1000)
End Sub
Sub InputBoxPositionB()
Dim rng As Range
Dim iLeft As Integer, iTop As Integer
Dim sTxt As String, sPrompt As String
Set rng = Range("B3")
iLeft = rng.Left + 23
iTop = rng.Top + 16
sPrompt = "Eingabe:"
sTxt = Application.InputBox( _
prompt:="Eingabe:", Left:=iLeft, Top:=iTop)
End Sub
Sub CallHelpA()
Dim sTxt As String, sPrompt As String, sFile As String
sFile = "..\..\2001\Beispiele\Kontext.chm"
If Dir(sFile) = "" Then
Beep
MsgBox "Hilfedatei existiert nicht!"
Exit Sub
End If
sPrompt = "Eingabe:"
sTxt = InputBox( _
prompt:="Eingabe:", _
HelpFile:=sFile, _
Context:="1001")
End Sub
Sub CallHelpB()
Dim sTxt As String, sPrompt As String, sFile As String
sFile = "..\..\2001\Beispiele\Kontext.chm"
If Dir(sFile) = "" Then
Beep
MsgBox "Hilfedatei existiert nicht!"
Exit Sub
End If
sPrompt = "Eingabe:"
sTxt = Application.InputBox( _
prompt:="Eingabe:", _
HelpFile:=sFile, _
HelpContextID:="1001")
End Sub