Full Version: Worksheets = <method 'worksheets Of'object '_global' Failed>
UtterAccess Discussion Forums > Microsoft® Access > Access Modules
classicxman
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
the_captain_slog
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
classicxman
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.
classicxman
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. iconfused.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.