largeselection
Active Member
- Joined
- Aug 4, 2008
- Messages
- 358
So I have some code (which I'm sure can be written more elegantly, but we all have to start somewhere haha) and I my error statements don't seem to be doing what I was hoping they would do. Basically, my code copies info from bookA.SheetA to bookB.SheetA and I've repeated it so that it does this for every tab.
So I tried to put in some error buffers in case there is no bookA.SheetC so that it doesn't stop the code from running.
I ran an example and in my example I had two sheets bookA.SheetA, bookA.SheetB. The code successfully copied the info from Sheet A and Sheet B into bookB, and it successfully skipped the code to try and copy Sheet C, but then it fizzled and errored in the code to try and copy Sheet D.
I had initially envisioned that it would hit my on errors and then just go through to the end of the routine.
Here's some code:
Part1:
On Error GoTo Part2:
Windows(obk).Activate
Worksheets("C1").Select
Set oRange = a find statement formula
Range("A1:D10").Select
Selection.Copy
ThisWorkbook.Activate
Sheets("C1").Select
Range("A1").Select
ActiveSheet.Paste
Part 2:
On Error GoTo Part3:
Windows(obk).Activate
Worksheets("C2").Select
Set oRange = a find statement formula
Range("A1:D10").Select
Selection.Copy
ThisWorkbook.Activate
Sheets("C2").Select
Range("A1").Select
ActiveSheet.Paste
Part 3:
On Error GoTo Part4:
etc
etc
etc
Through all parts until the last "Part" where I have
On Error GoTo errorend:
errorend:
Exit Sub
So I envisioned the code going to try Part 1 successfully, then when it goes to part 2 it has the on error goto part 3 first so when it can't find sheet "C2" it errors and goes on to try part 3, which says on error goto part 4 so when it can't find sheet "C3" it goes to part 4, etc, etc, etc until it cycles through to the end and gets to the exit sub.
I guess this does not work though since I keep getting an error.
I'm sure I'm lacking some crucial understanding, but any links/info/direction you can point me in would be much appreciated
So I tried to put in some error buffers in case there is no bookA.SheetC so that it doesn't stop the code from running.
I ran an example and in my example I had two sheets bookA.SheetA, bookA.SheetB. The code successfully copied the info from Sheet A and Sheet B into bookB, and it successfully skipped the code to try and copy Sheet C, but then it fizzled and errored in the code to try and copy Sheet D.
I had initially envisioned that it would hit my on errors and then just go through to the end of the routine.
Here's some code:
Part1:
On Error GoTo Part2:
Windows(obk).Activate
Worksheets("C1").Select
Set oRange = a find statement formula
Range("A1:D10").Select
Selection.Copy
ThisWorkbook.Activate
Sheets("C1").Select
Range("A1").Select
ActiveSheet.Paste
Part 2:
On Error GoTo Part3:
Windows(obk).Activate
Worksheets("C2").Select
Set oRange = a find statement formula
Range("A1:D10").Select
Selection.Copy
ThisWorkbook.Activate
Sheets("C2").Select
Range("A1").Select
ActiveSheet.Paste
Part 3:
On Error GoTo Part4:
etc
etc
etc
Through all parts until the last "Part" where I have
On Error GoTo errorend:
errorend:
Exit Sub
So I envisioned the code going to try Part 1 successfully, then when it goes to part 2 it has the on error goto part 3 first so when it can't find sheet "C2" it errors and goes on to try part 3, which says on error goto part 4 so when it can't find sheet "C3" it goes to part 4, etc, etc, etc until it cycles through to the end and gets to the exit sub.
I guess this does not work though since I keep getting an error.
I'm sure I'm lacking some crucial understanding, but any links/info/direction you can point me in would be much appreciated