Results 1 to 11 of 11

Object Variable Or With Block Not Set

Threaded View

  1. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Object Variable Or With Block Not Set

    Option Explicit will not stop errors, it helps prevent you making them
    Also, look at your code you needlessly switch off screen updating twice. You switch off displaalerts but don't restore it

    It looks like your error handling was clicking in if the sheet isn't found. Never use error handling until the code is working

    See if this is better. I've tidied the code up
    Option Explicit
    Public Sub Alex()
    
        Const strFldrPath As String = "C:\Data\Communications Division\Testing Folder\Working Time Master\Working Time Records\"
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim Filecell As Range
        
    'On Error GoTo err_trap '< remove ' to restore error handling
        With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
        
           
        For Each Filecell In Intersect(ActiveWorkbook.Sheets("Sheet1").UsedRange, ActiveWorkbook.Sheets("Sheet1").[A:A])
            If ActiveWorkbook.ReadOnly = True Then 'Why ????
                ThisWorkbook.Sheets("Sheet2").Cells(This 'Why?Workbook.Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row + 1, 1).Value = ActiveWorkbook.Name
                ActiveWorkbook.Close False
            ElseIf Dir(strFldrPath & Filecell.Text) <> vbNullString Then
                Set wb = Workbooks.Open(Filename:=strFldrPath & Filecell.Text)
                ws.Copy After:=wb.Sheets(wb.Sheets.Count)
                       
                For Each ws In ActiveWorkbook.Worksheets
                    If ws.Name = "20 DEC 10 - 20 MAR 11" Then
                    .DisplayAlerts = False
                    ws.Delete
                    .DisplayAlerts = True
                End If
              Next ws
            End If
          ActiveWorkbook.Close True
        Next Filecell
    err_trap:
        .DisplayAlerts = True
        .ScreenUpdating = True
      End With
    On Error GoTo 0
    End Sub
    Last edited by royUK; 10-28-2011 at 04:51 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1