Hi, SANTHOSH!which is the key field for sorting and grouping rows under the yellow rows dates? There's no field that evidently appears to be the argument required.
Thanks for responding to my query and it is my warm pleasure to meet such a nice person.
Let us call it grouping by date values in several columns; keep sorting aside. And about being a database let us just call it a group of records with variable no. of attributes; keep database aside of Edgar F. Codd will try to turn in the grave and run away.I mean to say sorting is “sorting of entire database with different expiry dates. Dates are most important and crucial. If it is a single column, it can be sorted easily in ascending or discending order. If it is more than one column, then result becomes little bit difficult. In the master data, if the data is sorted on a particular column say “Insurance Expiry” other expiry dates will not be in order. Each time sorting different columns one by one in the master data and get relevant data in the desired output will require lot of hectic practice and lot of headache.
Rows not containing valid dates in the indicated columns will be grouped all together at the end (or at the beginning as I don't know how will they come out).Sorting for the month of Feb 2014 is done except the data provided here. This data like 25/2, 28/2 is also expiry dates, which, comes from a different file. Surely, this can also be clubbed and analysed later on. So leave it for the time being.
I was asking that because of this:the complete row is represented by the File No. like 102, 105, 147, etc. just like unique ID of an employee or CID No of a customer database in order to locate it easily.
I'll assume the last row used, in sample file row 103.For Insurance expiry : Column L5 to L 102 of Data worksheet to be sorted
For Road Tax Expiry : Column P5 to P 102 of Data worksheet to be sorted
for Goods tax Expiry : Column Q5 to Q 105 of Data worksheet to be sorted
For Fitness Expiry : Column T5 to Q 105 of Data worksheet to be sorted
I didn't not understand anything, should I do in order to analyze your requirement? I think I don't as I only need to know which columns are to be considered for the date grouping. Confirm that are the following, as you didn't specified them in your answer to a):For example, if the file no is named 102 and its name is TATA LPT 4135” and having 3 different expiry dates, in the output worksheet, the row containing File No., Name of equipment, RC No and expiry dates (having File No 102) will repeat 3 different times. Here, in the datewise list worksheet, total number of columns are 6, the file 102 will repeat maximum upto 6 times with difference in expiry dates. The expiry dates may be in same month or different month. If the data is summed up mothwise, it has to be shown separately
Option Explicit
Sub DontDoThatAgainFaseehOrIWillBillItToYou()
' constants
Const ksInputWS = "Data"
Const ksOutputWS = "Datewise List"
Const kiRowBlank = 10
Const kiRowTitle = 4
Const kiColumnLast = 25
Const kiColumnWitness = 2
Const ksColumnWitnessText = "Equipment"
Const ksColumnSource = "0 1 2 15 16 17 18 19 20 12 22"
Const ksColumnDate = "0 12 16 17 18 19 20"
Const ksFormat1 = "dd/mm/yyyy;@"
Const ksFormat2 = "mmm/yyyy;@"
Const kiFont = 18
Const kiColumnDate = 4
Const ksKey = "Key"
Const ksSlash = "/"
Const ksDot = "."
Const ksDash = "-"
' declarations
Dim wsI As Worksheet, wsO As Worksheet
Dim vSource As Variant, vDate As Variant
Dim dWork() As Date, bDate() As Boolean
Dim iSource As Integer, iDate As Integer, bOk As Boolean
Dim I As Long, J As Integer, K As Long, L As Integer, M As Integer
Dim A As String, V As Variant
' start
Set wsI = Worksheets(ksInputWS)
Set wsO = Worksheets(ksOutputWS)
vSource = Split(ksColumnSource)
vDate = Split(ksColumnDate)
iSource = UBound(vSource)
iDate = UBound(vDate)
ReDim dWork(iDate), bDate(iSource)
wsO.Cells.Clear
For I = 1 To iSource
For J = 1 To iDate
If vSource(I) = vDate(J) Then Exit For
Next J
bDate(I) = (J <= iDate)
Next I
' process
'
With wsI
' titles
K = 1
For I = 1 To iSource
wsO.Cells(K, I).Value = .Cells(kiRowTitle, Val(vSource(I))).Value
Next I
wsO.Cells(K, iSource + 1).Value = ksKey
' data
I = kiRowTitle + 1
J = 0
Do Until J > kiRowBlank Or I > .Rows.Count
A = .Cells(I, kiColumnWitness).Value
If A = "" Then
' count blanks
J = J + 1
Else
If A <> ksColumnWitnessText Then
' read input
J = 0
' dates
' load
For L = 1 To iDate
V = .Cells(I, Val(vDate(L))).Value
V = Replace(Replace(V, ksDot, ksSlash), ksDash, ksSlash)
On Error Resume Next
V = CDate(V)
On Error GoTo 0
If IsDate(V) Then
dWork(L) = V
Else
dWork(L) = CDate(0)
End If
Next L
' organize
For L = 1 To iDate
If dWork(L) <> CDate(0) Then
dWork(L) = dWork(L) - Day(dWork(L)) + 1
For M = L + 1 To iDate
If Year(dWork(L)) = Year(dWork(M)) And _
Month(dWork(L)) = Month(dWork(M)) Then _
dWork(M) = CDate(0)
Next M
End If
Next L
' write output
For L = 1 To iDate
If dWork(L) <> CDate(0) Then
K = K + 1
For M = 1 To iSource
V = Trim(.Cells(I, Val(vSource(M))).Value)
bOk = False
If Not bDate(M) Then
bOk = True
Else
If IsDate(V) Then
If Year(dWork(L)) = Year(V) And _
Month(dWork(L)) = Month(V) Then
bOk = True
V = CDate(V)
End If
End If
End If
If bOk Then wsO.Cells(K, M).Value = V
Next M
wsO.Cells(K, iSource + 1).Value = dWork(L)
End If
Next L
End If
End If
I = I + 1
Loop
End With
'
With wsO
' format
.Columns.AutoFit
With .Cells
.ClearFormats
.HorizontalAlignment = xlGeneral
End With
.Rows(1).Font.Bold = True
For I = 1 To iSource
If bDate(I) Then .Columns(I).NumberFormat = ksFormat1
Next I
.Columns(iSource + 1).NumberFormat = ksFormat1
' sort
With .Sort
With .SortFields
.Clear
.Add Key:=.Parent.Parent.Columns(iSource + 1), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
.Add Key:=.Parent.Parent.Columns(1), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
End With
.SetRange .Parent.Cells
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' group
I = 2
Do While .Cells(I, iSource + 1).Value <> ""
If .Cells(I, iSource + 1).Value <> .Cells(I - 1, iSource + 1).Value Then
.Rows(I).Insert xlShiftDown
With .Cells(I, kiColumnDate)
.Value = .Parent.Cells(I + 1, iSource + 1).Value
.NumberFormat = ksFormat2
With .Font
.Size = kiFont
.Bold = True
End With
.HorizontalAlignment = xlCenter
End With
Range(.Cells(I, 1), .Cells(I, iSource + 1)).Interior.Color = vbYellow
I = I + 1
End If
I = I + 1
Loop
End With
'
' end
Set wsO = Nothing
Set wsI = Nothing
Beep
End Sub
Let us call a bunch of data with almost no format at all.Let us call it grouping by date values in several columns; keep sorting aside. And about being a database let us just call it a group of records with variable no. of attributes; keep database aside of Edgar F. Codd will try to turn in the grave and run away.
When you take your time to read this and described in detail but concisely how is the data updated in 1st worksheet, then may be you convince me to trigger the procedure execution at the worksheet change event code: it's a process that lasts a few seconds and it's not intended to be executed at any change. There are other possibilities but they'll depend on your answer.Data is entered in one worksheet and output is required as per the 2nd worksheet. The dates are updated daily /periodically in the worksheet "All data". However, this has to be done in the worksheet "Monthwise" also. The total number of data will be around 200.
The requirement is that, in the worksheet "All data", the data to be extracted in such a way that date and month of each year to be sorted chronologically whenever the dates are updated in the worksheet "All Data"
Hi, next missing guy!
V = Trim(.Cells(I, Val(vSource(M))).Value)
V = Trim(.Cells(I, Val(vSource(M))).Value)
V = Replace(Replace(V, ksDot, ksSlash), ksDash, ksSlash)