K
Kevin
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.
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.