I have Created a main Userform for data to be enter into a main sheet called "MasterData" which works very well. Now I am trying to create a second userform that will search through this data for a specific job number that is enter into one of the text boxs and then create a new sheet to view the data in. I have a CalendarPicker on this user form with one text box for the job number.
On the second user form I am trying to use a textbox that would filter the data searching for "Job Number" that they will scan in from travelre. I have the userform creating a new worksheet for this data but can't get it to pull in any data. I am very new at this so I have pieces this together from searching on the net.
So for the text box being a filter I am not sure what I should put here and do I also include the range of the data I want they will be sent to the new sheet that will be created. Here is my code for everything:
Private Sub CalendarPicker_Click()
' If TAG = 1, send the value back to dateFrom
' If TAG = 2, send the value back to dateTo
If Me.CalendarPicker.Tag = 1 Then
Me.dateFrom.Value = Me.CalendarPicker.Value
Else
If Me.CalendarPicker.Tag = 2 Then
Me.dateTo.Value = Me.CalendarPicker.Value
End If
End If
Me.CalendarPicker.Visible = False
End Sub
Private Sub cmdCal1_Click()
If Me.CalendarPicker.Visible = False Then
Me.CalendarPicker.Visible = True
If IsDate(Me.dateFrom.Value) = True Then
Me.CalendarPicker.Value = Me.dateFrom.Value
Else
Me.CalendarPicker.Value = Now()
End If
' The calendar control's CLICK event will push the calendar value back to one of the two text boxes
' Therefore, the calendar control needs to know which text box (From or To) is calling it.
' There might be a better way, but simple method: when FROM calls it, it will set the calendar's tag property to 1
' When the TO textbox calls it, it will set the tag to 2
' Based on the this tag property, the calendar will update the appropriate textbox
Me.CalendarPicker.Tag = 1
Else
' the calendar is already showing. that means if they're clicking this button, they want it closed
Me.CalendarPicker.Visible = False
End If
End Sub
Private Sub cmdCal2_Click()
If Me.CalendarPicker.Visible = False Then
Me.CalendarPicker.Visible = True
If IsDate(Me.dateTo.Value) = True Then
Me.CalendarPicker.Value = Me.dateTo.Value
Else
Me.CalendarPicker.Value = Now()
End If
' The calendar control's CLICK event will push the calendar value back to one of the two text boxes
' Therefore, the calendar control needs to know which text box (From or To) is calling it.
' There might be a better way, but simple method: when FROM calls it, it will set the calendar's tag property to 1
' When the TO textbox calls it, it will set the tag to 2
' Based on the this tag property, the calendar will update the appropriate textbox
Me.CalendarPicker.Tag = 2
Else
' the calendar is already showing. that means if they're clicking this button, they want it closed
Me.CalendarPicker.Visible = False
End If
End Sub
Private Sub cmdRun_Click()
Application.DisplayAlerts = False
RunReports
Application.DisplayAlerts = True
End Sub
Private Sub dateTo_Change()
End Sub
Private Sub Frame2_Click()
End Sub
Private Sub TextBox1_Change()
End Sub
Private Sub Scanjobnumber_Change()
'Trying to get this to be a Filter of the data your trying to look for and have copy to new tab that gets created
Dim Source As Worksheet
' Sheet containing data
Dim Crit As String
' Defines what sheet has the master data
Set Source = Worksheets("MasterData")
Application.ScreenUpdating = False
' Sheet name is data required
Crit = ActiveSheet.Name
With Source.Range("F2:U" & Source.Range("F2").End(xlDown).Row)
' Filter for column 6 = Job Number being scanned in
.AutoFilter Field:=6, Criteria1:=Crit
' Copy data to active sheet, range decides where it goes on the active sheet
.Copy ActiveSheet.Range("A1")
' Trn off Autofilter
.AutoFilter
End With
Application.ScreenUpdating = True
End Sub
Exit Sub
End Sub
Private Sub UserForm_Initialize()
' Just to be on the safe side, make sure the calendar picker is invisible
Me.CalendarPicker.Visible = True
Me.dateFrom.Value = Date - 30
Me.dateTo.Value = Date
End Sub
Function SheetExists(strSheetName As String) As Boolean
On Error GoTo SheetDoesNotExist
If Len(Sheets(strSheetName).Name) > 0 Then
SheetExists = True
Exit Function
End If
SheetDoesNotExist:
SheetExists = False
End Function
Private Sub RunReports()
If Me.dateFrom.Text = "" And Me.dateTo.Text = "" Then
lnDummy = MsgBox("You must select at least one date", vbOKOnly)
Exit Sub
Else
' at least one is filled with a date
If Me.dateTo.Text = "" Then
' just use dateFrom
lcDate = " where date >= cast('" + Me.dateFrom.Text + "' as datetime) "
Else
If Me.dateFrom.Text = "" Then
' use only dateTo
lcDate = " where date < (cast('" + Me.dateTo.Text + "' as datetime) + 1) "
Else
' use both dates
lcDate = " where date >= cast('" + Me.dateFrom.Text + "' as datetime) and date < (cast('" + Me.dateTo.Text + "' as datetime) + 1) "
End If
End If
End If
If SheetExists("OverInSpecLog") Then
Sheets("OverInSpecLog").Select
' Clear out the data sheet
Cells.Select
Selection.ClearContents
' Make sure the formatting for all cells is set to "General"
Cells.Select
Selection.NumberFormat = "General"
Range("A1").Select
Else
ActiveWorkbook.Worksheets.Add
ActiveSheet.Name = "OverInSpecLog"
End If
If Me.Scanjobnumber.Value = True Then
'Now grab the data from your data worksheet
Dim ws As Worksheet
Set ws = Worksheets("MasterData")
Range("A1").EntireRow.Insert
'Find the LastRow
Dim LastLine As Long
LastLine = Columns(1).Find("*", , , , xlByColumns, xlPrevious).Row
'Copy the filtered data.
Range("A:A" & LastLine).SpecialCells(xlCellTypeVisible).Copy
Range("C:C" & LastLine).SpecialCells(xlCellTypeVisible).Copy
Range("F:F" & LastLine).SpecialCells(xlCellTypeVisible).Copy
Range("G:G" & LastLine).SpecialCells(xlCellTypeVisible).Copy
Range("H:H" & LastLine).SpecialCells(xlCellTypeVisible).Copy
Range("K:K" & LastLine).SpecialCells(xlCellTypeVisible).Copy
Range("L:L" & LastLine).SpecialCells(xlCellTypeVisible).Copy
Range("L1").Activate
Selection.Copy
Sheets("OverInSpecLog").Select
Cells.Select
ActiveSheet.Paste
End If
End Sub
Bookmarks