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: 504
Default How to trap an error with onError in a for i loop

I am pretty new to VBA in Excel and am working through some stuff that will
help me at work. I wrote some code that essentially looks at one file, finds
a value (inventory number in this case) in a column, goes over a few cells,
copies the cell (on hand value) and then finds that same inventory number in
another workbook and pastes it a few cells over in that workbook. It works
fine, but to get better at this, I want to be able to handle errors. I have
purposly sabotaged some of the cells so that it won't be able to find the
value it is looking forand will cause an error just to see what happend. I
use the "onError" event, but I am running a loop and it only works once, but
not if there are more than one error. I just want it to skip the
instructions between the OnError and the line it says to go to. Again, it
does actually work once, but the next time it just gives me the error again.
By the way, this repeats hundreds of times.

Here is some of the code, I know it could be more efficient, but you'll get
the point. What I want to know is how to make it skip a step everytime it
finds an error ,not just the first time.

'sets variable equal the total number of rows in the sheet
intRowCount = Range("A2").CurrentRegion.Rows.Count
'start loop to run through all the active cells
For i = 1 To intRowCount
'Sets the contents of the current cell as the variable
varItemNumber = ActiveCell.Value
'Moves 0 cells down and 18 cells right (where this data should be)
ActiveCell.Offset(0, 18).Select
'Copies the data from the cell
ActiveCell.Copy
'Switch to the other sheet where the data will be used
Windows("Daily Analysis for Bug Testing.xls").Activate
Sheets("Products").Select
'Selects the entire row of item numbers to "find" from
Columns("A:A").Select
'Finds the cell that contains the item number that was stored as
"varItemNumber"
On Error GoTo line1
Selection.Find(What:=varItemNumber).Activate
'Moves 6 spaces to the right
ActiveCell.Offset(0, 6).Select
'Pastes the data
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
'Goes back to the original sheet

line1:
Windows("Workbook.xls").Activate
Sheets("Data").Select
'moves one cell down and 2 cells to the left to pickup the next item
number
ActiveCell.Offset(1, -18).Select
'starts the loop all over again
Next i

Any help would be much appreciated.


 
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
Cleaner Error trap in Loop. plys Excel Discussion (Misc queries) 3 January 20th 07 03:31 AM
Error Handling - On Error GoTo doesn't trap error successfully David Excel Programming 9 February 16th 06 05:59 PM
Error Trap Kirk P. Excel Programming 2 September 8th 05 09:51 PM
error trap Rhonda[_3_] Excel Programming 2 October 22nd 03 07:07 PM


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

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"