Halo,anyone.
I would like to ask how to assign the correct item code for each record in template2?
It is because when I search 1/7/2013-15/7/2013, it output 4 records,
but the item code is not 1,2,3,4. How can I assign it correctly?
Thanks, jason.
Option Explicit
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws1 = Sheets("Template2")
Set ws2 = Sheets("Database2")
Set ws3 = Sheets("By Period")
Dim i As Integer
Dim j As Integer
For i = 2 To ws2.Cells(Rows.Count, 1).End(xlUp).row
If ws3.Cells(7, 3).Value >= ws2.Cells(i, 2).Value And ws3.Cells(4, 3).Value <= ws2.Cells(i, 2).Value And ws2.Cells(i, 9).Value = 0 Then
LR = ws1.Range("B" & Rows.Count).End(xlUp).row
ws1.Range("A" & LR + 1) = i - 1
ws2.Cells(i, 2).Copy
ws1.Range("B" & LR + 1).PasteSpecial Paste:=xlPasteValues
ws1.Range("B" & LR + 1).NumberFormat = "dd/mm/yyyy"
ws2.Cells(i, 3).Copy
ws1.Range("C" & LR + 1).PasteSpecial Paste:=xlPasteValues
ws2.Cells(i, 4).Copy
ws1.Range("D" & LR + 1).PasteSpecial Paste:=xlPasteValues
ws2.Cells(i, 8).Copy
ws1.Range("E" & LR + 1).PasteSpecial Paste:=xlPasteValues
ws2.Cells(i, 5).Copy
ws1.Range("F" & LR + 1).PasteSpecial Paste:=xlPasteValues
ws2.Cells(i, 14).Copy
ws1.Range("G" & LR + 1).PasteSpecial Paste:=xlPasteValues
ws2.Cells(i, 6).Copy
ws1.Range("H" & LR + 1).PasteSpecial Paste:=xlPasteValues
ws2.Cells(i, 12).Copy
ws1.Range("I" & LR + 1).PasteSpecial Paste:=xlPasteValues
ws2.Cells(i, 13).Copy
ws1.Range("J" & LR + 1).PasteSpecial Paste:=xlPasteValues
ws1.Range("J" & LR + 1).NumberFormat = "dd/mm/yyyy"
ws2.Cells(i, 11).Copy
ws1.Range("K" & LR + 1).PasteSpecial Paste:=xlPasteValues
End If
Next i
Dim xlSort As XlSortOrder
With ws1
LR = ws1.Range("B" & Rows.Count).End(xlUp).row
If (.Range("C6").Value > .Range("C" & CStr(LR))) Then
xlSort = xlDescending
Else
xlSort = xlAscending
End If
.Range("B6:K" & LR).Sort Key1:=.Range("C6"), Order1:=xlSort, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End With
ActiveWorkbook.Save
End Sub
Same post by hei,
http://www.mrexcel.com/forum/excel-q...code-date.html
Bookmarks