As rylo correctly pointed out, the advanced filter is preferable in this instance and is the route I took. Since you asked for code, I developed code. With that said, it's not entirely necessary.
**Disclaimer - I could not get the 'criteria' component of the advanced filter to work. Should someone point out the correct syntax, the for each loop I used would not be needed and the resulting code would be much simpler. (I take these on as puzzles, like others do suduko or crosswords).**
The following applies the advanced filter and sends the results to a temp worksheet. The resulting range is then analyzed for instances where the date entered in a named range in your workbook (which you will need to create) equals the date in the range. The corresponding Bill No. is then passed to the next available row in the original worksheet.
Always make a copy of your workbook and run the code on that copy. VBA can be difficult to undue and taking a few precautionary steps can save you a headache down the road. Please see my notes throughout for adjustment as needed.
Option Explicit
Sub BeUnique()
'===============================================
Dim ws As Worksheet
Dim wsTemp As Worksheet
Dim rngCell As Range
Dim rngSource As Range
Dim rngSource2 As Range
Dim rngDate As Range
Dim strTempName As String
Dim lngThisRow As Long
Dim lngDest As Long
'===============================================
Set ws = ActiveWorkbook.Sheets("Sheet1") ' will need to change sheet name
Set rngDate = Range("DateCriteria") '/// You must create a named range of "DateCriteria" \\\
'/// in the cell in which the user will specify the \\\
'/// date \\\
strTempName = "MyTemp"
'/// Efficiency \\\
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
End With
With ws
'/// The following assumes data WITH HEADERS beginning in "A4:B4" \\\
'/// (i.e. A4 = "Bill No., B4 = "Date". You will need to change \\\
'/// the locations of your actual data since you did not specify \\\
'/// it in your original post. \\\
Set rngSource = .Range("A4", .Range("B4").End(xlDown))
End With
'/// Create a temp worksheet to hold filtered data \\\
Sheets.Add.Name = strTempName
Set wsTemp = Sheets(strTempName)
'/// Use advanced filter to get unique values from list and pass to temp ws \\\
rngSource.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wsTemp.Range("A1"), _
Unique:=True
'/// Analyze and set the size of the filtered range \\\
With wsTemp
Set rngSource2 = .Range("B1", .Range("B1").End(xlDown))
End With
'/// Loop through the range and pass instances where Date Criteria = True \\\
'/// to Destination <You will need to determine where you want your \\\
'/// results to show up. I have used "G5", with "G4" being the location \\\
'/// of the users date entry cell. \\\
For Each rngCell In rngSource2
If rngCell.Value = rngDate.Value Then
lngThisRow = rngCell.Row
wsTemp.Range("A" & lngThisRow).Copy
With ws
lngDest = .Cells(.Rows.Count, "G").End(xlUp).Row + 1
End With
ws.Range("G" & lngDest).PasteSpecial xlPasteValues
End If
Next rngCell
'/// Delete the temp worksheet \\\
wsTemp.Delete
'/// Clean up \\\
With Application
.Goto ws.Range("A1"), True ' Change to different cell if you wish to "land" somewhere else
.EnableEvents = True
.DisplayAlerts = True
.ScreenUpdating = True
End With
End Sub
Bookmarks