LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Why isn't my error handling working?!

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".

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
error handling James Cornthwaite Excel Programming 2 June 7th 06 11:06 PM
Error Handling - On Error GoTo doesn't trap error successfully David Excel Programming 9 February 16th 06 05:59 PM
Error handling with a handling routine ben Excel Programming 0 March 15th 05 03:01 PM
error handling not working Mike Macgowan Excel Programming 0 March 14th 05 09:29 PM
Error Handling James Agostinho Excel Programming 1 January 30th 04 06:40 AM


All times are GMT +1. The time now is 11:16 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"