I'm using a long process which creates and populates a series of 'route' workbooks and populates a master 'summary' workbook while doing this.
In brief, the error occurs immediately at the start of importing the data from the route workbook as soon as I try to use the relevant worksheet in the summary.
While quoting the entire code would be impractical, I have copied from where I think to be 'relevant' here:
' Get Route Number
RNum = wshCtrl.Cells(2, 2 + i).Value ' Number for routes being processed (blank if not processed)
RawRNum = wshCtrl.Cells(4, 2 + i).Value ' Raw Number
' Get Route Direction
RDir = wshCtrl.Cells(9, 2 + i).Value
' Get Name of Worksheet
NamewshRoute = "Route " & RawRNum & " - " & RDir
' Check if Route Worksheet exists
Set wshRoute = Nothing
On Error Resume Next
Set wshRoute = Sheets(NamewshRoute)
On Error GoTo 0
' If Route Worksheet doesn't exist and Route being processed
If wshRoute Is Nothing And RNum <> "" Then
' Create Route Worksheet
' Copy Template
wshTemplate.Copy After:=wshTemplate
' Rename Copied Template
Worksheets("Template (2)").Name = NamewshRoute
' Set as Route Worksheet
Set wshRoute = Sheets(NamewshRoute)
' Enter Route Number and Direction
wshRoute.Cells(2, 3).Value = RNum
wshRoute.Cells(2, 4).Value = RDir
' If Route Worksheet doesn't exist and Route isn't being processes
ElseIf wshRoute Is Nothing Then
' Do Nothing
' Else Route Worksheet already exists and Route being processed
Else
' Update Progress Bar
Percent = j / (NumRoutes + 0.5) - (1 / (NumRoutes + 1) * 8 / 8)
RefreshStatusBar Percent, "Processing " & NamewshRoute, "Initialising Route"
' Update Date
wshRoute.Range("AW9").Value = Month & Year
The error occurs on one of the following two lines:
wshRoute.Cells(2, 3).Value = RNum
or
wshRoute.Range("AW9").Value = Month & Year
depending on whether the worksheet exists or not.
Note that:
- Option Explicit is present at the top of the code.
- As far as I can tell all variables are defined and set appropriately.
- I have used the debugging process and using 'Add Watch' have confirmed that wshRoute is set appropriately and so are RNum, Month and Year.
- wshRoute.Cells(2, 3).Value returns the correct value and so does wshRoute.Range("AW9").Value.
- The only inconsistency I can see is that when I look further into the properties of wshRoute, the properties 'OnCalculate' through to 'OnSheetDeactivate' all have a value of . Not sure if this is relevant or not.
This code worked fine (I have been using for approximately a year) until I added another macro at the start. The new macro opens a different workbook and creates 'data' files. It doesn't interact with the 'Summary' workbook in any way. If I run the code without the extra new macro, it runs fine again.
Any suggestions please fire them through. This is high priority for me so I will be testing promptly.
Thanks, Cameron
opens a different workbook
time to qualify your sheets with workbooks as well:Set wshRoute = Sheets(NamewshRoute)
. – findwindow Jun 23 '16 at 22:44wshRoute
to something then immediately afterIf wshRoute Is Nothing
???? – findwindow Jun 23 '16 at 22:50Set wshRoute = WorkSheets(NamewshRoute)
instead ofSet wshRoute = Sheets(NamewshRoute)
. – PaichengWu Jun 23 '16 at 22:51