My Assistant
![]() ![]() |
|
|
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 |
|
|
|
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 |
|
|
|
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.
|
|
|
|
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 Top · Lo-Fi Version | Time is now: 20th May 2013 - 07:10 AM |