Results 1 to 5 of 5

Thread: Activate A Workbook After It Is Open

  1. #1

    Activate A Workbook After It Is Open

    Hello, Please can someone help as I am about to through my computer out of the window and its a work machines so probably not a great idea. I have been trying to simply open a workbook and activate it so I can perform some filtering etc. When I run this from the code file there is no problem at all but when I come out of the file and click the workbook shortcut (as users would) I get an error stating "Method 'activate' of '_Object Workbook' failed. Not sure where i am going wrong with such a simple task. Any help would really help me. The code breaks on the line below I have in Red font.

    Code:
    Dim ShipmentTimelinessWeek As Workbook
    Application.ScreenUpdating = False
    If Dir("\\2.44.120.20\Felixstowe\Team KH\Timeliness report week.xlsx") = "" Then
    MsgBox ("The Shipment Timeliness Report (GTN) was not found" & vbNewLine & _
    "please make sure the file is saved to:" & vbNewLine & _
    "\\2.44.120.20\Felixstowe\Team KH\Input Reports\")
    Else
    Set ShipmentTimelinessWeek = Workbooks.Open("\\2.44.120.20\Felixstowe\Team KH\Timeliness report week.xlsx")
    End If
    '***************************************************************************
    'Delete any rows from Timeliness Report than are outside the date range
    Workbooks("Timeliness report week.xlsx").Activate
    With ActiveWorkbook.Worksheets("Shipment Timeliness")
    .AutoFilterMode = False
        With Range("F1", Range("F" & Rows.Count).End(xlUp))
            .AutoFilter 1, Criteria1:="<" & GetDateRangeFrom, Operator:=xlOr, Criteria2:=">" & GetDateRangeTo
            On Error Resume Next
            .Offset(1).SpecialCells(12).EntireRow.Delete
        End With
        .AutoFilterMode = False
    End With
    Last edited by xander1981; 03-14-2014 at 06:32 PM.

  2. #2
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    Rep Power
    0
    Not 100% sure you need to activate it (since it is already active), but you could try using ThisWorkbook.Activate instead.

  3. #3
    Moderator
    Join Date
    Jul 2012
    Posts
    156
    Rep Power
    13
    In case the file isn't found, you need to provide an exit so the code stops. Now you only give a messagebox saying the file isn't found but after that the code continues so you recieve an errormessage.
    Code:
    MsgBox ("The Shipment Timeliness Report (GTN) was not found" & vbNewLine & _
    "please make sure the file is saved to:" & vbNewLine & _
    "\\2.44.120.20\Felixstowe\Team KH\Input Reports\"):Exit sub

  4. #4
    Thanks guys, both responses correct, its the 'WITH' statement that seems to no longer work for me now. Not sure what I have done to my excel programme as used to do With sheet **** do this END WITH but now doesnt work?? v strange

  5. #5
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    13
    Code:
    sub M_snb()
      If Dir("\\2.44.120.20\Felixstowe\Team KH\Timeliness report week.xlsx") <>"" Then
       with Workbooks.Open("\\2.44.120.20\Felixstowe\Team KH\Timeliness report week.xlsx")
          with .sheets("Shipment Timeliness").usedrange.columns(6)
               .AutoFilter 1, "<" & GetDateRangeFrom, xlOr, ">" & GetDateRangeTo
               .Offset(1).SpecialCells(12).EntireRow.Delete
               .autofilter
           end with
        End With
      end if
    End With

Similar Threads

  1. Replies: 6
    Last Post: 08-25-2013, 12:35 PM
  2. Open And Activate Workbook Before Runing Macro
    By Howardc in forum Excel Help
    Replies: 5
    Last Post: 06-04-2013, 07:23 PM
  3. VBA Code to Open Workbook and copy data
    By Howardc in forum Excel Help
    Replies: 16
    Last Post: 08-15-2012, 06:58 PM
  4. Get Name List of All Open Workbook Files
    By princ_wns in forum Excel Help
    Replies: 5
    Last Post: 04-07-2012, 12:18 PM
  5. Assign an event to chart on workbook open
    By LalitPandey87 in forum Excel Help
    Replies: 2
    Last Post: 02-20-2012, 07:43 AM

Tags for this Thread

Posting Permissions

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