T
tercerojista
I've written some code and added error handling, but the error
handling isn't kicking in when it should, and I get the usual runtime
error message. Can anybody point out what I'm doing wrong here? The
code is supposed to modify dates that are in the worksheet as text
into proper Excel dates (in dd/mm/yyyy format). The error handling
has been added to handle any cases where there is not a valid date to
convert. The error handling works in some cases, but not in others.
Here is the full code:
- - - - -
Option Explicit
Sub ConvertBlackDates()
'*** Converts the dates in the "Black" worksheet that are stored as
text
'*** into proper Excel dates.
Dim lngFinalRow As Long, i As Integer
Dim vaColArray(3) As Variant, j As Integer
Dim strThisText As String, dtmThisDate As Date
On Error GoTo ErrorHandler
lngFinalRow = Cells(Rows.Count, 1).End(xlUp).Row
'*** Set the columns that need to be modified
vaColArray(0) = 9
vaColArray(1) = 33
vaColArray(2) = 34
'*** Loop through the data and make the necessary modifications
For j = LBound(vaColArray) To UBound(vaColArray) - 1
For i = 2 To lngFinalRow
strThisText = Cells(i, vaColArray(j)).Value
'*** Switch on error handling here in case the value
cannot be converted
'*** to a date (e.g. the cell may contain a number that is
not a date)
On Error GoTo NextRow
'*** Skip any blanks or non-numeric values
If IsNumeric(Left(strThisText, 1)) Then
dtmThisDate = CDate(Mid(strThisText, 4, 2) & "/" &
Left(strThisText, 2) & "/" & Mid(strThisText, 7, 4))
Cells(i, vaColArray(j)).Value = dtmThisDate
End If
On Error GoTo ErrorHandler
NextRow:
Next i
Next j
Exit Sub
ErrorHandler:
MsgBox ("An error arose while trying to modify" & vbCr & _
"the date formats. Please contact your" & vbCr & _
"Excel support." & vbCr & vbCr & _
"Error description: " & Err.Description & vbCr & _
"Error source: Row " & i & " Column " & vaColArray(j))
End Sub
- - - - -
As an example, the error handling works fine when "strThisText" has a
value of "999999", but not when it is "560307".
handling isn't kicking in when it should, and I get the usual runtime
error message. Can anybody point out what I'm doing wrong here? The
code is supposed to modify dates that are in the worksheet as text
into proper Excel dates (in dd/mm/yyyy format). The error handling
has been added to handle any cases where there is not a valid date to
convert. The error handling works in some cases, but not in others.
Here is the full code:
- - - - -
Option Explicit
Sub ConvertBlackDates()
'*** Converts the dates in the "Black" worksheet that are stored as
text
'*** into proper Excel dates.
Dim lngFinalRow As Long, i As Integer
Dim vaColArray(3) As Variant, j As Integer
Dim strThisText As String, dtmThisDate As Date
On Error GoTo ErrorHandler
lngFinalRow = Cells(Rows.Count, 1).End(xlUp).Row
'*** Set the columns that need to be modified
vaColArray(0) = 9
vaColArray(1) = 33
vaColArray(2) = 34
'*** Loop through the data and make the necessary modifications
For j = LBound(vaColArray) To UBound(vaColArray) - 1
For i = 2 To lngFinalRow
strThisText = Cells(i, vaColArray(j)).Value
'*** Switch on error handling here in case the value
cannot be converted
'*** to a date (e.g. the cell may contain a number that is
not a date)
On Error GoTo NextRow
'*** Skip any blanks or non-numeric values
If IsNumeric(Left(strThisText, 1)) Then
dtmThisDate = CDate(Mid(strThisText, 4, 2) & "/" &
Left(strThisText, 2) & "/" & Mid(strThisText, 7, 4))
Cells(i, vaColArray(j)).Value = dtmThisDate
End If
On Error GoTo ErrorHandler
NextRow:
Next i
Next j
Exit Sub
ErrorHandler:
MsgBox ("An error arose while trying to modify" & vbCr & _
"the date formats. Please contact your" & vbCr & _
"Excel support." & vbCr & vbCr & _
"Error description: " & Err.Description & vbCr & _
"Error source: Row " & i & " Column " & vaColArray(j))
End Sub
- - - - -
As an example, the error handling works fine when "strThisText" has a
value of "999999", but not when it is "560307".