Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Ogilvy Help :) - "Object variable or With block variable not set"

When I execute code below, a Run-time error '91': Object variable or
With block variable not set occurs on the line marked with the ***.
Suspecting the problem is related to the declarations, I
expereiemented with a couple of modifications, but nothing worked. I
am unsure how to correct...any help is greatly appreciated. Kind
Regards, Mike

Sub VisibleTrue()
'code that will programatically loop
'through all the .xls files (each file has 10 worksheets) in a
'directory and

'1) delete the same four worksheets ("Sheet1", "Sheet10", "Sheet3", &
'"Sheet6" from each of the .xls files, and then
'3) change the worksheet Name property of the remaining six worksheets
'so that the worksheet Name property of the first worksheet is
'"Sheet1", the second worksheet is "Sheet2", etc.

Dim basebook As Workbook
'Dim mybook As Workbook
'Dim Item As Worksheet
Dim i As Long, j As Long
Dim mybook As Object
Dim sh As Object
Application.ScreenUpdating = False
With Application.FileSearch
.NewSearch
.LookIn = "C:\Data\DataFiles\Sept"
.SearchSubFolders = False
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
Set basebook = ThisWorkbook
For i = 1 To .FoundFiles.Count
Set mybook = Workbooks.Open(.FoundFiles(i))
Application.DisplayAlerts = False
*** sh.Worksheets(Array("Sheet1", "Sheet2", _
"Sheet3", "Sheet6")).Delete
Application.DisplayAlerts = True
j = 0
For Each sh In Sheets
j = j + 1
sh.Name = j
sh.Visible = True
Next sh
j = 0
For Each sh In Sheets
j = j + 1
sh.Name = "Sheet" & j
Next
mybook.Close SaveChanges:=True
Next i
End If
End With
Application.ScreenUpdating = True
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 811
Default Ogilvy Help :) - "Object variable or With block variable not set"

Hi Mike,

In this line of code:

*** sh.Worksheets(Array("Sheet1", "Sheet2", _
"Sheet3", "Sheet6")).Delete


you have not set the sh variable. Based on the surrounding context, are you
sure you didn't mean that to be mybook.Worksheets(....).Delete?

--
Rob Bovey, MCSE, MCSD, Excel MVP
Application Professionals
http://www.appspro.com/

* Please post all replies to this newsgroup *
* I delete all unsolicited e-mail responses *


"Mike Taylor" wrote in message
om...
When I execute code below, a Run-time error '91': Object variable or
With block variable not set occurs on the line marked with the ***.
Suspecting the problem is related to the declarations, I
expereiemented with a couple of modifications, but nothing worked. I
am unsure how to correct...any help is greatly appreciated. Kind
Regards, Mike

Sub VisibleTrue()
'code that will programatically loop
'through all the .xls files (each file has 10 worksheets) in a
'directory and

'1) delete the same four worksheets ("Sheet1", "Sheet10", "Sheet3", &
'"Sheet6" from each of the .xls files, and then
'3) change the worksheet Name property of the remaining six worksheets
'so that the worksheet Name property of the first worksheet is
'"Sheet1", the second worksheet is "Sheet2", etc.

Dim basebook As Workbook
'Dim mybook As Workbook
'Dim Item As Worksheet
Dim i As Long, j As Long
Dim mybook As Object
Dim sh As Object
Application.ScreenUpdating = False
With Application.FileSearch
.NewSearch
.LookIn = "C:\Data\DataFiles\Sept"
.SearchSubFolders = False
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
Set basebook = ThisWorkbook
For i = 1 To .FoundFiles.Count
Set mybook = Workbooks.Open(.FoundFiles(i))
Application.DisplayAlerts = False
*** sh.Worksheets(Array("Sheet1", "Sheet2", _
"Sheet3", "Sheet6")).Delete
Application.DisplayAlerts = True
j = 0
For Each sh In Sheets
j = j + 1
sh.Name = j
sh.Visible = True
Next sh
j = 0
For Each sh In Sheets
j = j + 1
sh.Name = "Sheet" & j
Next
mybook.Close SaveChanges:=True
Next i
End If
End With
Application.ScreenUpdating = True
End Sub



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Runtime Error '91' Object variable or With block variable not set Alec Coliver Excel Discussion (Misc queries) 2 October 24th 09 02:29 PM
object variable or with block variable not set Diego Excel Discussion (Misc queries) 1 August 9th 05 02:46 PM
Error 91 - Object variable with block variable not set Jim[_35_] Excel Programming 2 November 27th 03 03:34 AM
Object Variable or With Block variable not set? Chris M.[_3_] Excel Programming 3 August 26th 03 04:30 PM
Exel2000 VBA - Creating a Command Bar - Error: "Object Variable or With not Set" keepitcool Excel Programming 0 August 11th 03 12:29 PM


All times are GMT +1. The time now is 03:39 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"