Datumsanzeige
17.03.2016 08:12:35
Michael
eine Frage an alle VBA-Anwender.
Ich habe ein Tabellenblatt, indem in Spalte B ein Anfangsdatum und in Spalte I ein
Enddatum eingetrage wird. Nun habe ich auf einer Userform zwei TextBoxen, indem man diese beiden Datumsangaben eintragen kann. Mit einem CommandButton wird die Suche ausgelöst. Findet er das Datum nicht, zeigt er ein Hinweis "Anfangs,- bzw Enddatum nicht gefunden". Löscht die entsprechende TextBox und man kann ein anderes Datum angeben.
Nun hätte ich gerne, dass er anstatt des löschen des Textfeldes das nächst, liegende Datum einträgt, dass als Suchanfrage eingegeben worden ist.
Beispiel: Anfangsdatum: 01.01.2015 = gibt`s nicht. Makro sucht und findet das Datum
02.01.2015. Er löscht das Datum 01.01.2015 und trägt dafür
das gefundene Datum 02.01.2015 in die TextBox ein.
Ich krieg das nicht hin. Kann mir jemand helfen? Hier mein bisheriges Makro
Private Sub CommandButton3_Click()
Dim objWs As Worksheet
Dim varRet As Variant, lngStart As Long, lngEnd As Long
Dim temp
Dim i As Long
Dim zeile As Long
Dim ende As Long
Application.ActivePrinter = "PDFCreator auf Ne00:"
With Sheets("Rechnungen")
.Visible = xlSheetVisible
.Copy After:=Sheets(Sheets.Count)
Set objWs = Sheets(Sheets.Count)
Range("B6").Select
With objWs
.Unprotect
Range("A6:I65535").Select
Selection.Sort Key1:=Range("B6"), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortTextAsNumbers
Range("B6").Select
If IsDate(TextBox1) Then
If IsDate(TextBox2) Then
temp = Application.WorksheetFunction.CountIf(.Columns(2), CLng(CDate(TextBox1)))
If temp = 0 Then
MsgBox "Das Anfangsdatum wurde nicht gefunden!", vbOKOnly, ""
TextBox1 = ""
ActiveSheet.Unprotect
Application.DisplayAlerts = False
.Delete
UserForm8.Hide
UserForm8.Show
End
Else
varRet = Application.Match(CLng(CDate(TextBox1)), .Columns(2), 0)
If IsNumeric(varRet) Then
lngStart = varRet
temp = Application.WorksheetFunction.CountIf(.Columns(2), CLng(CDate(TextBox2)))
If temp = 0 Then
MsgBox "Das Enddatum wurde nicht gefunden!", vbOKOnly, ""
TextBox2 = ""
ActiveSheet.Unprotect
Application.DisplayAlerts = False
.Delete
UserForm8.Hide
UserForm8.Show
End
Else
varRet = Application.Match(CLng(CDate(TextBox2)), .Columns(2), 1)
If IsNumeric(varRet) Then
lngEnd = varRet
Me.Hide
.PageSetup.PrintArea = CStr("A" & lngStart & ":I" & lngEnd)
.PrintPreview
Unload Me
Else
MsgBox "Anfangsdatum nicht gefunden!", vbOKOnly, ""
End If
End If
Else
MsgBox "Enddatum nicht gefunden!", vbOKOnly, ""
End If
End If
Else
MsgBox "Anfangsdatum ungültig!", vbOKOnly, ""
End If
Else
MsgBox "Enddatum ungültig!", vbOKOnly, ""
End If
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
End With
With Sheets("Startseite")
.Visible = True
.Activate
End With
For Each objWs In ThisWorkbook.Worksheets
If Not objWs.Name = "Startseite" Then
objWs.Visible = xlVeryHidden
End If
Next
End With
End Sub