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

Welcome Guest ( Log In | Register )

> 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
 
+

Posts in this topic


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 May 2013 - 05:43 AM