LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Error Handler only works once

Sorry, should be

if SheetExists(myVendor) then 'New if

--
HTH...

Jim Thomlinson


"Jim Thomlinson" wrote:

Once an error has been generated you leave the normal thread of execution and
start processing under the error handler. To resume normal exectuion requires
the resume key word. Since you never resume normal exectuion after your first
error you are now processing under the error handler. Once in the error
handler you can no longer handle any subsequent errors. IMO this is an
inappropriate use of the error handler (catching an error that should never
be generated in the first place). You should verify that a sheet exists prior
to using that sheet. Something like this...

Public Function SheetExists(SName As String, _
Optional ByVal Wb As Workbook) As Boolean
'Chip Pearson
On Error Resume Next
If Wb Is Nothing Then Set Wb = ThisWorkbook
SheetExists = CBool(Len(Wb.Sheets(SName).Name))
End Function

Sub atryThisSix()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim myAddress, theAddress As Range, myPrice
Dim myVendor As String, myProduct
Dim m
j = 1
k = 1
l = 2
Application.ScreenUpdating = False
Windows("trial.xls").Activate
Do Until Cells(k, j) = ""

If Cells(k, j).Value = "f" Then
myVendor = Cells(k, j).Offset(0, 6).Value
myProduct = Cells(k, j).Offset(0, 7).Value
Cells(k, 2).Value = myVendor
Cells(k, 3).Value = myProduct
Windows("Code.xls").Activate

if sheetexists(Workbooks("Code.xls").Sheets(myVendor) ) then 'New if
Workbooks("Code.xls").Sheets(myVendor).Select
Columns("F:F").Select
Dim cell As Range

Set cell = Columns("f:f").Find(What:=myProduct, _
after:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
cell.Activate
Set cell = ActiveCell

' cell.Activate
myPrice = ActiveCell.Offset(0, 1).Value

If Not cell Is Nothing Then
Windows("trial.xls").Activate
Cells(k, 12).Value = myPrice
End If
' Windows("trial.xls").Activate
' Cells(k, 12).Value = myPrice

Windows("trial.xls").Activate



Else
Cells(k, 2).Value = Cells(k, j).Offset(0, 9).Value
End If
end if 'new end if
Windows("trial.xls").Activate
k = k + 1

Loop

Application.ScreenUpdating = True
End Sub

--
HTH...

Jim Thomlinson


"aagray" wrote:

Hello,

I'm trying to catch an error using "on error goto line 1", whenever a
tab in another file that I'm referencing does not exist.

It seems to work the first time around, but not the second. I've
attached the code.

Any help would be greatly appreciated.

Regard,
Anita

Sub atryThisSix()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim myAddress, theAddress As Range, myPrice
Dim myVendor As String, myProduct
Dim m
j = 1
k = 1
l = 2
Application.ScreenUpdating = False
Windows("trial.xls").Activate
Do Until Cells(k, j) = ""

If Cells(k, j).Value = "f" Then
myVendor = Cells(k, j).Offset(0, 6).Value
myProduct = Cells(k, j).Offset(0, 7).Value
Cells(k, 2).Value = myVendor
Cells(k, 3).Value = myProduct
Windows("Code.xls").Activate

On Error GoTo line1
Workbooks("Code.xls").Sheets(myVendor).Select
Columns("F:F").Select
Dim cell As Range

Set cell = Columns("f:f").Find(What:=myProduct, _
after:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
cell.Activate
Set cell = ActiveCell

' cell.Activate
myPrice = ActiveCell.Offset(0, 1).Value

If Not cell Is Nothing Then
Windows("trial.xls").Activate
Cells(k, 12).Value = myPrice
End If
' Windows("trial.xls").Activate
' Cells(k, 12).Value = myPrice

Windows("trial.xls").Activate



Else
Cells(k, 2).Value = Cells(k, j).Offset(0, 9).Value
End If
line1:

Windows("trial.xls").Activate
k = k + 1

Loop

Application.ScreenUpdating = True
End Sub


A

*** Sent via Developersdex http://www.developersdex.com ***

 
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 handler linto Excel Discussion (Misc queries) 1 February 11th 10 12:17 PM
VBA Error Handler MVM Excel Programming 1 June 12th 06 07:56 PM
Form Err.Raise error not trapped by entry procedure error handler [email protected] Excel Programming 1 February 8th 06 10:19 AM
Error Handler dht[_2_] Excel Programming 5 August 19th 04 08:51 AM
error handler Fleur Excel Programming 1 November 20th 03 06:06 AM


All times are GMT +1. The time now is 12:44 AM.

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

About Us

"It's about Microsoft Excel"