pmich
06-04-2014, 04:03 PM
The dates in date column are entered like this.
01/03/2011
01/04/2011
01/06/2011
01/12/2011
13/1/2011
14/1/2011
19/1/2011
29/1/2011
31/1/2011
02/01/2011
02/03/2011
02/04/2011
02/08/2011
09/02/2011
02/10/2011
02/11/2011
14/2/2011
28/2/2011
03/01/2011
In the above example I have given as though one date occurs only one. But, one date may be repeated more than once.
From 01/03/2011 to 01/12/2011 refers to Jan. (mm/dd/yyyy)
From 13/1/2011 to 31/1/2011 refers to Jan. (dd/mm/yyyy)
Then,
From 02/01/2011 to 02/08/2011 refers to Feb. (mm/dd/yyyy)
Then 09/02/2011 refers to Feb. (dd/mm/yyyy)
From 02/10/2011 to 02/11/2011 refers to Feb. (mm/dd/yyyy)
From 14/2/2011 to 28/2/2011 refers to Feb. (dd/mm/yyyy)
Then,
From 03/01/2011 refers to Mar. (mm/dd/yyyy)
After this, dates of April are mentioned.
Likewise, until December dates are keyed in.
After December, the next year begins.
Again the date format is not uniform in that year.
Thus it goes on upto 2014.
There is no uniformity in the format.
The date format does not change after say, a particular row.
Months are noted down as 01 and also 1.
I have written the code given below. it works fine for 5 months.
But is there a simpler method or how to simplify this code?
Please suggest.
Private Sub UserForm_Click()
Dim rRng As Range
Dim rCell As Range
Dim sDest As String
Dim sYear, sMonth, sDay, aDate
Dim LastRecNum As Long
Sheets("MemberJoinDate").Select
On Error Resume Next
Sheets("MemberJoinDate").AutoFilterMode = False
On Error GoTo 0
Dim Bgnrow As Long
Dim Endrow As Long
Dim Mnth As Integer
Endrow = 0
ChkAgain:
If Endrow = 0 Then
Bgnrow = 2
Endrow = 73
Mnth = 1
ElseIf Endrow = 73 Then
Bgnrow = 74
Endrow = 150
Mnth = 2
ElseIf Endrow = 150 Then
Bgnrow = 151
Endrow = 219
Mnth = 3
ElseIf Endrow = 219 Then
Bgnrow = 220
Endrow = 301
Mnth = 4
ElseIf Endrow = 301 Then
Bgnrow = 302
Endrow = 405
Mnth = 5
End If
Set rRng = ActiveSheet.Range("B" & Trim(Str(Bgnrow)) & ":B" & Trim(Str(Endrow)))
sDest = "D"
For Each rCell In rRng.Cells
sYear = 99999
If InStr(rCell.Value, "/") > 0 Then
aDate = Split(rCell.Value, "/")
If UBound(aDate) = 2 Then
If aDate(0) <= 12 Then
If Val(aDate(0)) = Mnth Then
sDay = aDate(1)
sMonth = aDate(0)
ElseIf Val(aDate(0)) > Mnth And Val(aDate(1)) = Mnth Then
sDay = aDate(0)
sMonth = aDate(1)
ElseIf Val(aDate(0)) < Mnth And Val(aDate(1)) = Mnth Then
sDay = aDate(0)
sMonth = aDate(1)
End If
ElseIf aDate(0) >= 12 Then
If Val(aDate(0)) > Mnth And Val(aDate(1)) = Mnth Then
sDay = aDate(0)
sMonth = aDate(1)
End If
End If
sYear = aDate(2)
End If
End If
With rCell.Range(sDest & "1")
If sYear <> 99999 Then
On Error Resume Next
.Value = "'" & Format(CDate(sDay & "/" & sMonth & "/" & sYear), "DD-MM-YYYY")
If Err.Number <> 0 Then .Value = rCell.Value
On Error GoTo 0
Else
.Value = rCell.Value
End If
End With
Next
If Endrow = 876 Then
Exit Sub
End If
GoTo ChkAgain
End Sub
Sample of the result I get is given below:
03-01-2011
04-01-2011
07-01-2011
10-01-2011
11-01-2011
29-01-2011
31-01-2011
01-02-2011
09-02-2011
15-02-2011
28-02-2011
01-03-2011
09-03-2011
14-03-2011
31-03-2011
01-04-2011
09-04-2011
11-04-2011
19-04-2011
20/4/20 11
29-04-2011
30-04-2011
02-05-2011
02-05-2011
05-05-2011
06-05-2011
09-05-2011
10-05-2011
28-05-2011
30-05-2011
31-05-2011
Is it possible to get the result as : 03/01/2011 instead of 03-01-2011? Please suggest.
01/03/2011
01/04/2011
01/06/2011
01/12/2011
13/1/2011
14/1/2011
19/1/2011
29/1/2011
31/1/2011
02/01/2011
02/03/2011
02/04/2011
02/08/2011
09/02/2011
02/10/2011
02/11/2011
14/2/2011
28/2/2011
03/01/2011
In the above example I have given as though one date occurs only one. But, one date may be repeated more than once.
From 01/03/2011 to 01/12/2011 refers to Jan. (mm/dd/yyyy)
From 13/1/2011 to 31/1/2011 refers to Jan. (dd/mm/yyyy)
Then,
From 02/01/2011 to 02/08/2011 refers to Feb. (mm/dd/yyyy)
Then 09/02/2011 refers to Feb. (dd/mm/yyyy)
From 02/10/2011 to 02/11/2011 refers to Feb. (mm/dd/yyyy)
From 14/2/2011 to 28/2/2011 refers to Feb. (dd/mm/yyyy)
Then,
From 03/01/2011 refers to Mar. (mm/dd/yyyy)
After this, dates of April are mentioned.
Likewise, until December dates are keyed in.
After December, the next year begins.
Again the date format is not uniform in that year.
Thus it goes on upto 2014.
There is no uniformity in the format.
The date format does not change after say, a particular row.
Months are noted down as 01 and also 1.
I have written the code given below. it works fine for 5 months.
But is there a simpler method or how to simplify this code?
Please suggest.
Private Sub UserForm_Click()
Dim rRng As Range
Dim rCell As Range
Dim sDest As String
Dim sYear, sMonth, sDay, aDate
Dim LastRecNum As Long
Sheets("MemberJoinDate").Select
On Error Resume Next
Sheets("MemberJoinDate").AutoFilterMode = False
On Error GoTo 0
Dim Bgnrow As Long
Dim Endrow As Long
Dim Mnth As Integer
Endrow = 0
ChkAgain:
If Endrow = 0 Then
Bgnrow = 2
Endrow = 73
Mnth = 1
ElseIf Endrow = 73 Then
Bgnrow = 74
Endrow = 150
Mnth = 2
ElseIf Endrow = 150 Then
Bgnrow = 151
Endrow = 219
Mnth = 3
ElseIf Endrow = 219 Then
Bgnrow = 220
Endrow = 301
Mnth = 4
ElseIf Endrow = 301 Then
Bgnrow = 302
Endrow = 405
Mnth = 5
End If
Set rRng = ActiveSheet.Range("B" & Trim(Str(Bgnrow)) & ":B" & Trim(Str(Endrow)))
sDest = "D"
For Each rCell In rRng.Cells
sYear = 99999
If InStr(rCell.Value, "/") > 0 Then
aDate = Split(rCell.Value, "/")
If UBound(aDate) = 2 Then
If aDate(0) <= 12 Then
If Val(aDate(0)) = Mnth Then
sDay = aDate(1)
sMonth = aDate(0)
ElseIf Val(aDate(0)) > Mnth And Val(aDate(1)) = Mnth Then
sDay = aDate(0)
sMonth = aDate(1)
ElseIf Val(aDate(0)) < Mnth And Val(aDate(1)) = Mnth Then
sDay = aDate(0)
sMonth = aDate(1)
End If
ElseIf aDate(0) >= 12 Then
If Val(aDate(0)) > Mnth And Val(aDate(1)) = Mnth Then
sDay = aDate(0)
sMonth = aDate(1)
End If
End If
sYear = aDate(2)
End If
End If
With rCell.Range(sDest & "1")
If sYear <> 99999 Then
On Error Resume Next
.Value = "'" & Format(CDate(sDay & "/" & sMonth & "/" & sYear), "DD-MM-YYYY")
If Err.Number <> 0 Then .Value = rCell.Value
On Error GoTo 0
Else
.Value = rCell.Value
End If
End With
Next
If Endrow = 876 Then
Exit Sub
End If
GoTo ChkAgain
End Sub
Sample of the result I get is given below:
03-01-2011
04-01-2011
07-01-2011
10-01-2011
11-01-2011
29-01-2011
31-01-2011
01-02-2011
09-02-2011
15-02-2011
28-02-2011
01-03-2011
09-03-2011
14-03-2011
31-03-2011
01-04-2011
09-04-2011
11-04-2011
19-04-2011
20/4/20 11
29-04-2011
30-04-2011
02-05-2011
02-05-2011
05-05-2011
06-05-2011
09-05-2011
10-05-2011
28-05-2011
30-05-2011
31-05-2011
Is it possible to get the result as : 03/01/2011 instead of 03-01-2011? Please suggest.