เมื่อปีค.ศ.2000 ผมเคยเขียนรหัส VBA ใช้แก้ไขเซลล์วันที่ซึ่งบันทึกผิดเป็นปีพ.ศ. ให้แก้ไขกลับเป็นปีค.ศ. เลยนำมาให้ทดลองใช้กันครับ ถ้าใครพบว่าทำงานผิดพลาด ขอให้แจ้งให้ผมทราบด้วย (ดูรหัสในแฟ้มแนบดีกว่าที่ผมยกมาแสดงครับ)
ถ้าเลือกเซลล์เดียว จะถือว่าให้แก้ไขทั้งชีท แต่ถ้าเลือก 2 เซลล์ขึ้นไป จะแก้ไขให้เฉพาะพื้นที่ตารางที่เลือกไว้ครับ
Code:
Sub BE2AD()
On Error Resume Next
Dim WorkRange As Range
Dim DateCell As Range
Application.ScreenUpdating = False
If TypeName(Selection) <> "Range" Then Exit Sub
If Selection.Count = 1 Then
Set WorkRange = Cells
Else
Set WorkRange = Selection
End If
UserChoice = MsgBox("Correct in this Sheet?",
vbYesNo + vbDefaultButton1,
"Excel Expert Utility from www.ExcelExpertTraining.com")
If UserChoice = vbYes Then
UserComment = MsgBox("Need Comment?",
vbYesNo + vbDefaultButton1,
"Excel Expert Utility from www.ExcelExpertTraining.com")
PivotYear = InputBox("Possible maximum AD Year" & Chr(10) & Chr(10) & "",
"Excel Expert Utility from www.ExcelExpertTraining.com", 2442)
If Val(PivotYear) < 2442 Then
MsgBox "Below 2442 is not possible", ,
"Excel Expert Utility from www.ExcelExpertTraining.com"
PivotYear = 2442
End If
End If
Set DateCell = WorkRange.SpecialCells(xlCellTypeConstants, xlNumbers)
For Each cell In DateCell
If IsDate(cell) Then
If UserChoice = vbYes Then
usedyear = Application.WorksheetFunction.Text(cell, "yyyy")
If (Val(usedyear) > Val(PivotYear)) And (Val(usedyear) < 2810) Then
newyear = Val(usedyear) - 543
If UserComment = vbYes Then
Application.DisplayCommentIndicator = xlCommentIndicatorOnly
cell.ClearComments
cell.AddComment
cell.Comment.Text Text:="" & Chr(10) &
"Corrected date input from year " &
usedyear & " to " & newyear
End If
UsedMonth = Month(cell)
UsedDay = Day(cell)
newdate = DateSerial(newyear, UsedMonth, UsedDay)
cell.Value = newdate
End If
End If
End If
Next cell
End Sub
Bookmarks