+ Reply to Thread
Results 1 to 11 of 11

Object Variable Or With Block Not Set

Hybrid View

  1. #1
    Registered User
    Join Date
    10-24-2010
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    49

    Object Variable Or With Block Not Set

    Originally and after some help I had 2 separate pieces of code, the first to copy the active worksheet into each workbook in a folder and the second to delete a named worksheet if it existed.

    To make the code more efficient and use techniques I liked from both pieces of code, I have tried to merge them, the result being the code below.

    However, I get the message Object Variable Or With Block Not Set.

    What have I done wrong?

    Public Sub Alex()

    Const strFldrPath As String = "C:\Data\Communications Division\Testing Folder\Working Time Master\Working Time Records\"

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim Filecell As Range

    Application.ScreenUpdating = False

    Set ws = ActiveWorkbook.ActiveSheet

    For Each Filecell In Intersect(ActiveWorkbook.Sheets("Sheet1").UsedRange, ActiveWorkbook.Sheets("Sheet1").[A:A])
    If ActiveWorkbook.ReadOnly = True Then
    ThisWorkbook.Sheets("Sheet2").Cells(ThisWorkbook.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
    Application.DisplayAlerts = False
    ws.Delete
    On Error Resume Next
    End If

    Next ws

    End If

    ActiveWorkbook.Close True

    Next Filecell

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

    End Sub
    Last edited by AlexRoberts; 10-28-2011 at 07:35 AM. Reason: Solved

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Object Variable Or With Block Not Set

    Its compiling on my system but since I don't have any dummy files, I can't run the full code. Could you upload a dummy workbook with a similar structure that you are using and indicate which line is giving you the error message.

    abousetta

    P.S. Please wrap the code is the [ CODE ][ /CODE ] rather than [ QUOTE ][ /QUOTE ] because the quote are more difficult to read.
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  3. #3
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Object Variable Or With Block Not Set

    AlexRoberts,


    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


    
    Option Explicit
    Public Sub Alex()
    Const strFldrPath As String = "C:\Data\Communications Division\Testing Folder\Working Time Master\Working Time Records\"
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim Filecell As Range
    Application.ScreenUpdating = False
    Set ws = ActiveWorkbook.ActiveSheet
    
    For Each Filecell In Intersect(ActiveWorkbook.Sheets("Sheet1").UsedRange, ActiveWorkbook.Sheets("Sheet1").[A:A])
      
      If ActiveWorkbook.ReadOnly = True Then
        ThisWorkbook.Sheets("Sheet2").Cells(ThisWorkbook.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
            Application.DisplayAlerts = False
            ws.Delete
            On Error Resume Next
          End If
        Next ws
        
        ActiveWorkbook.Close True
        
      End If
    
    Next Filecell
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    End Sub
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  4. #4
    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

    Please use code tags not quote tags
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Registered User
    Join Date
    10-24-2010
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Object Variable Or With Block Not Set

    Sorry about using the wrong Tag type.

    I have added the line 'Option Explicit to the code by the error still happens.

    So I have now enclosed the template and the way it should work is as follows,

    1. find the spreadsheet(s) in Sheet1 of the template.
    2. if the sheet is open put its' name in Sheet2 and move on to the next one in the list.
    3. insert the active worksheet.
    4. delete a sheet '20 DEC 10 - 20 Mar 11' if it exists.

    Two things are happening with the code,

    1. it never gets to the second for each statement, to delete a sheet and,
    2. I get the 'Object Variable Or With Block Not Set' error when it opens a second workbook.

    I cannot find many examples of muliple For Each statements, so any help would be greatly appreciated
    Attached Files Attached Files

  6. #6
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Object Variable Or With Block Not Set

    Hi Alex,

    I'm just still trying to figure out what it is you are doing exactly. Are you trying to open each of the files in the list on Sheet 1 and then if available copy the first sheet over to the current workbook?

    Also this line confuses me:

        For Each Filecell In Intersect(ActiveWorkbook.Sheets("Sheet1").UsedRange, ActiveWorkbook.Sheets("Sheet1").[A:A])
            If ActiveWorkbook.ReadOnly = True Then
    what are you trying to do exactly here?

    Here is your code with slight modifications until we can understand it better. This should be in a standared module, not a sheet.

    Option Explicit
    Sub Alex()
    
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim Filecell As Range
        
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        
        Const strFldrPath As String = "C:\Data\Communications Division\Testing Folder\Working Time Master\Working Time Records\"
        
        Set ws = ActiveWorkbook.ActiveSheet
    
        For Each Filecell In Intersect(ActiveWorkbook.Sheets("Sheet1").UsedRange, ActiveWorkbook.Sheets("Sheet1").[A:A])
            If ActiveWorkbook.ReadOnly = True Then
                ThisWorkbook.Sheets("Sheet2").Cells(ThisWorkbook.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 ws.Delete
                    Next ws
            End If
          ActiveWorkbook.Close True
        Next Filecell
           
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
        
    End Sub
    abousetta

  7. #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.

  8. #8
    Registered User
    Join Date
    10-24-2010
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Object Variable Or With Block Not Set

    Yes, I am trying to open each of the files in the list on Sheet 1 and then if they exist copy the activeworksheet, in this case '19 DEC 11 - 18 MAR 12' to the current (open) workbook.

    If the workbook is already open I then want to list it in Sheet2 hence the line of code

    If ActiveWorkbook.ReadOnly = True Then
                ThisWorkbook.Sheets("Sheet2").Cells(ThisWorkbook.Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row + 1, 1).Value = _
                    ActiveWorkbook.Name
                ActiveWorkbook.Close False
    I have tried both code examples after putting them in a Module but,

    1. still get a Object Variable Or With Block Not Set in the folowing line ws.Copy After:=wb.Sheets(wb.Sheets.Count) on the second workbook it opens.
    2. the 'worksheet 20 MAR 11 - 19 JUN 12' in the first workbook it opended the has not been deleted.

    Any ideas?

  9. #9
    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

    You wiil get that error if the worksheet doesn't exist.

    If a workbook is opened read only then sheets cannot be deleted

  10. #10
    Registered User
    Join Date
    10-24-2010
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Object Variable Or With Block Not Set

    Roy,

    I need to check what happens to 'Read Only' files.

    There was a line miising from the code
    Set wsActive = ActiveWorkbook.ActiveSheet
    I have put this into your code and all works fine, thank you.

    As a matter of interest do you know what code I need to use to leave the workbook as I found it after deleting and inserting worksheets, what was the activeworkdheet.

  11. #11
    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

    You would need to store that initial worksheet as a Public Variable & reset to that when closing

+ Reply to Thread

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