dahveedoff
New Member
- Joined
- Jan 31, 2014
- Messages
- 7
Hi,
I have got a script that keeps failing the second time around when it meets the condition for my error handler. So basically, the loop goes to look through a function sub procedure and then the value is parsed back into the main code and it checks to see if that value already exists as a named worksheet in the work book. As part of the process, there are other scripts that would have been run before to populate the workbook with named worksheets and this script needs to create a new worksheet after the earlier sheet.
e,g. If I had a sheet created earlier called CTS2, in this new script, I would need this new procedure to use the new name which is a result of the function sub procedure that transforms this worksheet into a value of CTS3 and then subsequently put this after CTS2.
The main issue is that I have new worksheets without any precursor worksheets e.g PT3 has no PT2 tab created earlier. So in order to trap this, I set up an error handler which works when it encounters a worksheet that meets this criteria but then fails when another worksheet about to be created has this same issue.
Do you think it has to do with resetting the error handler? I f so, i have done that twice in the code but stillno joy.
Please see the routines below
Main Code below
Function
I have got a script that keeps failing the second time around when it meets the condition for my error handler. So basically, the loop goes to look through a function sub procedure and then the value is parsed back into the main code and it checks to see if that value already exists as a named worksheet in the work book. As part of the process, there are other scripts that would have been run before to populate the workbook with named worksheets and this script needs to create a new worksheet after the earlier sheet.
e,g. If I had a sheet created earlier called CTS2, in this new script, I would need this new procedure to use the new name which is a result of the function sub procedure that transforms this worksheet into a value of CTS3 and then subsequently put this after CTS2.
The main issue is that I have new worksheets without any precursor worksheets e.g PT3 has no PT2 tab created earlier. So in order to trap this, I set up an error handler which works when it encounters a worksheet that meets this criteria but then fails when another worksheet about to be created has this same issue.
Do you think it has to do with resetting the error handler? I f so, i have done that twice in the code but stillno joy.
Please see the routines below
Main Code below
Code:
Do Until ActiveCell.Value = "" And ActiveCell.Offset(1, 0).Value = ""
NewSheetName = FieldTrans(ActiveSheet.Range("B2").Value) & ActiveSheet.Range("C2").Value _
& "_T3"
SqlStr = NewSheetName
Debug.Print SqlStr
' Error handling
On Error Resume Next
On Error GoTo Sheetnotfound
ActiveWorkbook.Sheets.Add After:=ActiveWorkbook.Sheets(FieldTrans(ActiveSheet.Range("B2").Value) & ActiveSheet.Range("C2").Value _
& "_T2")
Sheetnotfound:
If Err.Number = 9 Then
'MsgBox ("Sheet name not found in " & ThisWorkbook.Name & " !")
ActiveWorkbook.Sheets.Add After:=ActiveWorkbook.Sheets("Source3")
Err.Clear
End If
On Error GoTo 0
'Exit Sub
ActiveSheet.Name = NewSheetName
ActiveWorkbook.Sheets("Source3").Select
ActiveSheet.Range("A1").Select
ActiveCell.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
B = ActiveCell.Row
ActiveSheet.Range(Cells(1, 1), Cells(B, LastColumn)).Copy
ActiveWorkbook.Sheets(NewSheetName).Select
ActiveSheet.Range("A1").Select
ActiveCell.PasteSpecial xlPasteAll
ActiveWorkbook.Sheets("Source3").Select
ActiveSheet.Range("A2").Select
Do Until ActiveCell.Value = ""
ActiveCell.EntireRow.Delete
Loop
ActiveCell.EntireRow.Delete
Loop
Function
Code:
If FN = "PG Accounting and Finance Portfolio BUS" Then
FNS = "A&F"
ElseIf FN = "PG MBA and PG Cert Management Portfolio BUS" Then
FNS = "MBA"
ElseIf FN = "PG Computer Science and Technology Portfolio CATS" Then
FNS = "CST"
etc...