PC Review


Reply
Thread Tools Rate Thread

Code Fails Intermittently

 
 
Keith Wilby
Guest
Posts: n/a
 
      10th Jun 2008
I'm using this VBA code snippet in an MS Access form to manipulate and Excel
workbook. The code works out the current week number in the format "yww" (eg
"824") and uses that to test the workbook for a sheet with that name. If it
finds one it uses it, if it doesn't it looks for the previous week's and
copies and renames it.

The code intermittently fails at the line with the asterisks. The error I
get is "Application-defined or object-defined error". The funny thing is
that it worked perfectly until I introduced an option group on my MS Access
form ("Me.ogrPeriod") to let the user choose "This Week" or "Last Week" and
the error seems to be happening at random intervals. A long shot but, does
anyone know what's going on?

Many thanks.
Keith.

'Open the Excel spreadsheet
Set objXL = New Excel.Application
Set objWkb = objXL.Workbooks.Open(strFilePath)
objXL.Visible = False

'Copy the previous week's sheet if there is not one for this week in the
workbook
strWeekNo = IIf(Len(Format(Date, "ww")) = 1, "0" & Format(Date, "ww"),
Format(Date, "ww"))
strWeekNo = Right(Date, 1) & strWeekNo
'Take into account the reporting period
strWeekNo = strWeekNo - Me.ogrPeriod

If libSheetExists(objWkb, objSht, strWeekNo) = False Then
Dim strPreviousWeek As String
strPreviousWeek = strWeekNo - 1
'Check whether there's a sheet for the previous week. If there isn't
then create a new, unformatted one.
If libSheetExists(objWkb, objSht, strPreviousWeek) = False Then
MsgBox "No suitable sheets were found in the selected workbook. A
new, unformatted sheet will be created.", vbExclamation, "Sheet not found"
Call CreateNew
GoTo ResumeNew
End If
Set objSht = objWkb.Worksheets(strPreviousWeek) 'Select the previous
week's sheet
objSht.Select
objSht.Copy After:=Worksheets(strPreviousWeek) ' ***********************
code fails here
strPreviousWeek = strPreviousWeek & " (2)" 'The copied sheet bears the
same name as the original plus " (2)"
Set objSht = objWkb.Worksheets(strPreviousWeek) 'Select the copied sheet
objSht.Name = strWeekNo 'Rename the copied sheet

'DO STUFF

'Save the workbook
objXL.ActiveWorkbook.Save

'Give the user the option to view the workbook
If MsgBox("Data exported. Do you want to view the spreadsheet?", vbYesNo,
"Data exported") = vbNo Then
objXL.Quit
Else
objSht.Activate
objXL.Visible = True
End If

'Tidy Up
Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing

 
Reply With Quote
 
 
 
 
Bob Phillips
Guest
Posts: n/a
 
      10th Jun 2008
Long shot, but try this

strPreviousWeek = Cstr(Val(strWeekNo) - 1)

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Keith Wilby" <(E-Mail Removed)> wrote in message
news:484e75ab$(E-Mail Removed)...
> I'm using this VBA code snippet in an MS Access form to manipulate and
> Excel workbook. The code works out the current week number in the format
> "yww" (eg "824") and uses that to test the workbook for a sheet with that
> name. If it finds one it uses it, if it doesn't it looks for the previous
> week's and copies and renames it.
>
> The code intermittently fails at the line with the asterisks. The error I
> get is "Application-defined or object-defined error". The funny thing is
> that it worked perfectly until I introduced an option group on my MS
> Access form ("Me.ogrPeriod") to let the user choose "This Week" or "Last
> Week" and the error seems to be happening at random intervals. A long
> shot but, does anyone know what's going on?
>
> Many thanks.
> Keith.
>
> 'Open the Excel spreadsheet
> Set objXL = New Excel.Application
> Set objWkb = objXL.Workbooks.Open(strFilePath)
> objXL.Visible = False
>
> 'Copy the previous week's sheet if there is not one for this week in the
> workbook
> strWeekNo = IIf(Len(Format(Date, "ww")) = 1, "0" & Format(Date, "ww"),
> Format(Date, "ww"))
> strWeekNo = Right(Date, 1) & strWeekNo
> 'Take into account the reporting period
> strWeekNo = strWeekNo - Me.ogrPeriod
>
> If libSheetExists(objWkb, objSht, strWeekNo) = False Then
> Dim strPreviousWeek As String
> strPreviousWeek = strWeekNo - 1
> 'Check whether there's a sheet for the previous week. If there isn't
> then create a new, unformatted one.
> If libSheetExists(objWkb, objSht, strPreviousWeek) = False Then
> MsgBox "No suitable sheets were found in the selected workbook. A
> new, unformatted sheet will be created.", vbExclamation, "Sheet not found"
> Call CreateNew
> GoTo ResumeNew
> End If
> Set objSht = objWkb.Worksheets(strPreviousWeek) 'Select the previous
> week's sheet
> objSht.Select
> objSht.Copy After:=Worksheets(strPreviousWeek) '
> *********************** code fails here
> strPreviousWeek = strPreviousWeek & " (2)" 'The copied sheet bears the
> same name as the original plus " (2)"
> Set objSht = objWkb.Worksheets(strPreviousWeek) 'Select the copied
> sheet
> objSht.Name = strWeekNo 'Rename the copied sheet
>
> 'DO STUFF
>
> 'Save the workbook
> objXL.ActiveWorkbook.Save
>
> 'Give the user the option to view the workbook
> If MsgBox("Data exported. Do you want to view the spreadsheet?", vbYesNo,
> "Data exported") = vbNo Then
> objXL.Quit
> Else
> objSht.Activate
> objXL.Visible = True
> End If
>
> 'Tidy Up
> Set objSht = Nothing
> Set objWkb = Nothing
> Set objXL = Nothing
>



 
Reply With Quote
 
Keith Wilby
Guest
Posts: n/a
 
      10th Jun 2008
"Bob Phillips" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Long shot, but try this
>
> strPreviousWeek = Cstr(Val(strWeekNo) - 1)
>


Thanks Bob but sadly made no difference.

Regards,
Keith.

 
Reply With Quote
 
Keith Wilby
Guest
Posts: n/a
 
      10th Jun 2008
"Keith Wilby" <(E-Mail Removed)> wrote in message
news:484e75ab$(E-Mail Removed)...

It seems that I relied too heavily on the help and not my common sense
because the offending line:

objSht.Copy After:=Worksheets(strPreviousWeek)

should read:

objSht.Copy after:=objSht

Obvious now that I know ;-)

Keith.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Winpopup intermittently fails Robert S Windows XP General 0 12th Nov 2008 11:49 PM
Remote Desktop Intermittently Fails =?Utf-8?B?RHIuSm9uZXM=?= Windows XP Basics 3 10th Apr 2006 09:30 PM
IIS Fails Intermittently Barry McConomy Microsoft Windows 2000 3 4th Apr 2005 02:55 PM
diskpart fails intermittently Voltaic Windows XP Setup 0 11th Aug 2004 06:28 AM
InvokeDispMethod fails intermittently RichB Microsoft C# .NET 2 13th Jul 2004 01:36 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:01 PM.