UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Worksheets = <method 'worksheets Of'object '_global' Failed>    
 
   
classicxman
post Apr 17 2012, 07:17 AM
Post #1

UtterAccess Enthusiast
Posts: 61



Hi,

I am running the attached code to modify an Excel file and save a new copy. I am getting an error at the line "For Each Ws in Worksheets" (Worksheets = <Method 'Worksheets of'object '_Global' failed>)

any ideas?

CODE
Function mailversion02()

Dim xlApp As Excel.Application
Dim Ws As Worksheet, FirstSheet As Worksheet
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim TempFilePath As String
Dim TempFileName As String
Dim NewFileName As String
Set xlApp = CreateObject("Excel.Application")
Set FirstSheet = ActiveSheet

With xlApp.Application
    .Visible = False
    .ScreenUpdating = False
    .DisplayAlerts = False
    .Workbooks.Open "C:\Queries\FillRate\PTCFillRateDash.xlsx"

    'Remove formulas
        For Each Ws In Worksheets 'Error: "Worksheets = <Method 'Worksheets of'object '_Global' failed>"  
          If Ws.Name = "Calcs" Or Ws.Name = "ChartData" Then
            Ws.Activate
            With Ws.Cells
                .Select
                .Copy
                .PasteSpecial Paste:=xlPasteValues
                xlApp.Application.CutCopyMode = False
            End With
            Ws.[A1].Select
          End If
        Next
    
    'Delete sheets
    .Sheets(Array("LinkedData", "Calcs")).Select
    .ActiveWindow.SelectedSheets.Delete
    
    'Hide sheets
    .Sheets("ChartData").Select
    .ActiveWindow.SelectedSheets.Visible = False
    
    .Sheets("YesterdayShortOrders").Activate
    .EnableEvents = False
End With

    'Save email version of file
        FileExtStr = ".xlsx": FileFormatNum = 51
        TempFilePath = "C:\autoemailfiles\"
        TempFileName = "PTC_FillRateDashboard"
        NewFileName = TempFileName & " " & Format$(Date, "mm-dd-yyyy")
        
        With ActiveWorkbook
            .SaveAs TempFilePath & NewFileName, FileFormat:=FileFormatNum
            .Close SaveChanges:=False
        End With

'Clean up and quit
With xlApp.Application
        .ScreenUpdating = True
        .EnableEvents = True
        .DisplayAlerts = True
End With

xlApp.Quit
Set xlApp = Nothing

End Function
Go to the top of the page
 
+
the_captain_slog
post Apr 17 2012, 08:06 AM
Post #2

UtterAccess Veteran
Posts: 305
From: England - UK A small island north of France



i dont think you are referencing it correctly

had a quick look in help and came up with the below

CODE
Sub worksheets_range()
Dim ws As Worksheet, wb As Workbook
Set wb = ActiveWorkbook

For Each ws In wb.Worksheets
Debug.Print ws.Name
Next
End Sub


HTH
Go to the top of the page
 
+
classicxman
post Apr 17 2012, 08:40 AM
Post #3

UtterAccess Enthusiast
Posts: 61



Strange. The code works fine as is on a different Excel file, just not the one shown. I'll dig into the workbooks and see if there is any difference.
Go to the top of the page
 
+
classicxman
post Apr 17 2012, 10:52 AM
Post #4

UtterAccess Enthusiast
Posts: 61



The only difference between the two Excel files was--the one that wasn't working had an access linked table on the first worksheet. I could move the sheet anywhere else in the sheet order and the code works. Just doesn't like it as the first sheet. (IMG:style_emoticons/default/iconfused.gif)
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 19th June 2013 - 09:17 PM