Place the following In a module :
Option Explicit
Sub in_between_or_not()
'run this to hide the unwanted data
Dim startrow As Integer 'Start of the data
Dim endrow As Integer 'End of data
Dim actualrow As Integer 'rownumber during loop from start to end
Dim counting As Integer
startrow = 11 'always 11
endrow = LastCell(Dates_for_bids).Row 'lastcell is a function
actualrow = 11
For counting = startrow To endrow 'as long as the endrow isn't reached, go on
'check the dates if current date is equal or in between
If Date >= Range("B" & actualrow) And Date <= Range("C" & actualrow) Then
'move on to the next row
actualrow = actualrow + 1
Else
'if current date isn't in between or equal than hide
Rows(actualrow).EntireRow.Hidden = True 'hide row if not in between dates
actualrow = actualrow + 1
End If
Next counting
End Sub
Sub show_everything()
'run this to show back all your data
Dim startrow As Integer
Dim endrow As Integer
Dim actualrow As Integer
Dim counting As Integer
startrow = 11
endrow = LastCell(Dates_for_bids).Row
actualrow = 11
For counting = startrow To endrow
If Rows(actualrow).Hidden = True Then
Rows(actualrow).EntireRow.Hidden = False
actualrow = actualrow + 1
Else
actualrow = actualrow + 1
End If
Next counting
End Sub
Function LastCell(ws As Worksheet) As Range
Dim LastRow&, LastCol%
' This is a function from someone else that I found on his or her site.
' Use it to know the last used cell in a sheet.
' Error-handling is here in case there is not any
' data in the worksheet
On Error Resume Next
With ws
' Find the last real row
LastRow& = .Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row
' Find the last real column
LastCol% = .Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column
End With
' Finally, initialize a Range object variable for
' the last populated row.
Set LastCell = ws.Cells(LastRow&, LastCol%)
End Function
--- End For module
This Is the code For the form With one listbox on it
Option Explicit
Private Sub UserForm_activate()
Dim R As Integer 'loop going through rows
Dim rijteller As Integer 'the rownumber
Dim pos As Integer 'place in the array
Dim MyList() As String 'name of the array
Dim i As Integer 'no of rows in the array that you don't know
Dim echte_laatste_rij As Integer 'no of the last row without filtering
echte_laatste_rij = LastCell(Dates_for_bids).Row 'determine the last row
'last row must be known before you are going to filter
'so first you count the no of rows and then you hide the rows based
'on the criteria.
in_between_or_not 'this is the code that filters the rows to be seen.
rijteller = 11 'start of the data
For R = 11 To echte_laatste_rij 'Loop trough all the rows to determine how much rows the array must have
If Rows(rijteller).Hidden = True Then 'if row is filtered, go to next row
rijteller = rijteller + 1 'row is row+1
Else
i = i + 1 'if not hided, array is one row bigger
rijteller = rijteller + 1 'go to the next row
End If
Next R
ReDim Preserve MyList(i, 3) 'Here you must have the number of rows that aren't hidden.
'the list contains 3 columns
'you can adjust the number, width and height
Application.ShowToolTips = True
With ListBox1
.ColumnCount = 3
.ColumnWidths = "2 cm ;2 cm;5 cm"
.ControlTipText = "Dates for bids ..."
.ListStyle = fmListStylePlain
.SpecialEffect = fmSpecialEffectFlat
End With
'Define the list and where it's obtained from (Columns B, C, D in this example)
rijteller = 11
pos = 0 'place in redefined array - MyList(i,3) so many i rows and 3 columns
With ActiveSheet
For R = 0 To echte_laatste_rij - 10 '-10 because data starts at row 11
If Rows(rijteller).Hidden = True Then
rijteller = rijteller + 1
Else
MyList(pos, 0) = .Range("B" & rijteller) 'value of cell B in array
MyList(pos, 1) = .Range("C" & rijteller)
MyList(pos, 2) = .Range("D" & rijteller)
pos = pos + 1 'arrayrow is 1 higher
rijteller = rijteller + 1 'row is one higher
End If
Next R
End With
'fill the listbox with the array
ListBox1.List = MyList
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
show_everything 'restore worksheet to show all the rows
Unload Me 'remove form from memory
End Sub
|