+ Reply to Thread
Results 1 to 20 of 20

Method 'Range' Error When Code is Run Automatically When Opening Workbook

Hybrid View

TBrooker Method 'Range' Error When... 06-15-2013, 02:38 PM
jason_lee_91 Re: Method 'Range' Error When... 06-15-2013, 03:02 PM
tehneXus Re: Method 'Range' Error When... 06-15-2013, 03:54 PM
TBrooker Re: Method 'Range' Error When... 06-15-2013, 04:22 PM
Norie Re: Method 'Range' Error When... 06-15-2013, 06:21 PM
TBrooker Re: Method 'Range' Error When... 06-25-2013, 04:38 AM
Norie Re: Method 'Range' Error When... 06-25-2013, 04:49 AM
TBrooker Re: Method 'Range' Error When... 06-25-2013, 05:02 AM
Norie Re: Method 'Range' Error When... 06-25-2013, 05:09 AM
TBrooker Re: Method 'Range' Error When... 06-25-2013, 05:50 AM
Norie Re: Method 'Range' Error When... 06-25-2013, 05:54 AM
TBrooker Re: Method 'Range' Error When... 06-25-2013, 06:26 AM
Norie Re: Method 'Range' Error When... 06-25-2013, 06:40 AM
TBrooker Re: Method 'Range' Error When... 06-25-2013, 06:56 AM
Norie Have you tried saving the... 06-25-2013, 08:17 AM
TBrooker Re: Method 'Range' Error When... 06-25-2013, 08:25 AM
Norie I think the problem could be... 06-25-2013, 09:17 AM
TBrooker Re: Method 'Range' Error When... 06-25-2013, 09:27 AM
Norie Re: Method 'Range' Error When... 06-25-2013, 11:05 AM
TBrooker Re: Method 'Range' Error When... 06-26-2013, 02:46 PM
  1. #1
    Registered User
    Join Date
    07-14-2009
    Location
    Brighton, England
    MS-Off Ver
    Excel 97
    Posts
    49

    Method 'Range' Error When Code is Run Automatically When Opening Workbook

    Hello,

    I have a piece of code that I have run every time I open up a workbook but unfortunately I get: Run time error '1004' Method 'Range' of object '_Global' failed. When I download and open a workbook. If I choose to "End" there and then press play to run the code again it will run smoothly. It appears as though the code runs before the workbook has fully opened and as such it does not work.

    Any suggestions?

    The code that is run is as follows:

    Sub CheckBook()
        'Only run if correct worksheet opens
        If Not Range("C1").Value = "Transfer Bookings Report" Then
        Exit Sub
        End If
        DialogBox1.Show
    End Sub
    The code which causes it to run each time a workbook is opened is:

    Private WithEvents App As Application
    
    Private Sub Workbook_Open()
        Set App = Application
    End Sub
    
    Private Sub App_WorkbookOpen(ByVal wb As Workbook)
        CheckBook
    End Sub
    As I say this works perfectly well if I end it running and then run it again once the workbook has fully opened.


    Many thanks for any help.

  2. #2
    Forum Contributor
    Join Date
    07-16-2012
    Location
    Ontario Canada
    MS-Off Ver
    Excel 2010
    Posts
    124

    Re: Method 'Range' Error When Code is Run Automatically When Opening Workbook

    try changing the workbook open function to:
    Private Sub Workbook_Open()
    Checkbook
    End Sub
    Or if you have two subroutines called Checkbook, or if you have a module name Checkbook, then you will need to fully qualify it:

    ModuleName.Checkbook

  3. #3
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Method 'Range' Error When Code is Run Automatically When Opening Workbook

    Hi,

    I believe that the event is in an Add-IN and when you open a workbook the addin is opened first but as there are no sheets in an addin you cannot get a value there, try:
    Private Sub App_WorkbookOpen(ByVal wb As Workbook)
        If Not wb.IsAddin Then
            CheckBook
        End If
    End Sub
    Last edited by tehneXus; 06-15-2013 at 03:59 PM.
    Please use [CODE]-TAGS
    When your problem is solved mark the thread SOLVED
    If an answer has helped you please click to give reputation
    Read the FORUM RULES

  4. #4
    Registered User
    Join Date
    07-14-2009
    Location
    Brighton, England
    MS-Off Ver
    Excel 97
    Posts
    49

    Re: Method 'Range' Error When Code is Run Automatically When Opening Workbook

    Thanks guys, tried both solutions but with no luck.

    jason's solution gave an "ambiguous" error while tehneXus' left me with the exact same problem.
    Last edited by TBrooker; 06-15-2013 at 04:34 PM.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Method 'Range' Error When Code is Run Automatically When Opening Workbook

    Have you tried specifying the workbook and worksheet you want to check C1 in?
    If posting code please use code tags, see here.

  6. #6
    Registered User
    Join Date
    07-14-2009
    Location
    Brighton, England
    MS-Off Ver
    Excel 97
    Posts
    49

    Re: Method 'Range' Error When Code is Run Automatically When Opening Workbook

    Hello,

    No I haven't because the workbook opened will be named differently every time it is opened. I'm running a report once an hour or so and using the macro above to run a series of formatting changes so I can work through it. I need the macro to check every workbook that is opened.

    I also noted the other day when I was working on another PC which had a more up-to-date version of excel that I got a different error, I didn't have time to write it down though so will have to test it some other time. It was a more specific description of the fact the macro can't target that range though because it is not yet open.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Method 'Range' Error When Code is Run Automatically When Opening Workbook

    You don't need to know the workbook name.

  8. #8
    Registered User
    Join Date
    07-14-2009
    Location
    Brighton, England
    MS-Off Ver
    Excel 97
    Posts
    49

    Re: Method 'Range' Error When Code is Run Automatically When Opening Workbook

    Oh really? How would I best go about that?

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Method 'Range' Error When Code is Run Automatically When Opening Workbook

    This sub is passed a variable, wb, which should refer to the workbook that's just been opened.
    Private Sub App_WorkbookOpen(ByVal wb As Workbook)
        CheckBook
    End Sub
    So you could pass that variable to the CheckBook sub and use it there.

    This is how you would pass it.
    Private Sub App_WorkbookOpen(ByVal wb As Workbook)
        CheckBook wb
    End Sub
    This is how you would use it
    Sub CheckBook(wb As Workbook)
        'Only run if correct worksheet opens
        If Not wb.ActiveSheet.Range("C1").Value = "Transfer Bookings Report" Then
                Exit Sub
        End If
        DialogBox1.Show
    End Sub
    By the way, what sheet is active when you get an error?

    Could it possibly be a chart sheet or similar?

  10. #10
    Registered User
    Join Date
    07-14-2009
    Location
    Brighton, England
    MS-Off Ver
    Excel 97
    Posts
    49

    Re: Method 'Range' Error When Code is Run Automatically When Opening Workbook

    Hello,

    This now actually gives me a "Compile Error: Wrong number of arguments or invalid property assignment." at the
    Checkbook wb
    part of the code.

    and if I reset that to just Checkbook and run it I get an "Object required" error at the
    If Not wb.ActiveSheet.Range("C1").Value = "Transfer Bookings Report" Then
    part.

    I've reset the code back to how it was for now and included a picture below of when the error actually occurs. So essentially as far as I can tell at the point the error occurs no sheet is active.


    http://i.imgur.com/PFzpT0A.png

    Thank you.

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Method 'Range' Error When Code is Run Automatically When Opening Workbook

    Can you upload an example workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic


    PS What exactly is the code meant to do?

    Where is DialogBox1 located and what is it?
    Last edited by Norie; 06-25-2013 at 05:56 AM.

  12. #12
    Registered User
    Join Date
    07-14-2009
    Location
    Brighton, England
    MS-Off Ver
    Excel 97
    Posts
    49

    Re: Method 'Range' Error When Code is Run Automatically When Opening Workbook

    Hello,

    Attached a sample workbook, with personal information omitted.

    Also below here is the full code:

    In ThisWorkbook I have:

    Private WithEvents App As Application
    
    Private Sub Workbook_Open()
        Set App = Application
    End Sub
    
    Private Sub App_WorkbookOpen(ByVal wb As Workbook)
        If Not wb.IsAddin Then
            CheckBook
        End If
    End Sub
    In a module I have:

    Sub CheckBook()
        'Only run if correct worksheet opens
        If Not Range("C1").Value = "Transfer Bookings Report" Then
        Exit Sub
        End If
        DialogBox1.Show
    End Sub
    DialogBox1 gives a yes no option to "tidy the report" by running the sub TidyUp.

    Sub TidyUp()
    
    'Only run if correct worksheet opens
            Dim wb As Workbook
            With wb
            If Not Range("C1").Value = "Transfer Bookings Report" Then
            Exit Sub
            End If
            End With
            
    Application.ScreenUpdating = False
    
    'Remove logo area if it exists still
            If Range("C1").Value = "Transfer Bookings Report" Then
                Range("A1:A12").EntireRow.Delete
            End If
            
    'Searches for empty columns and delete them
        Range("A1:AN500").UnMerge
        Dim CCount As Long
        On Error Resume Next
        
        With Range("A1:AL1")
    
            CCount = .SpecialCells(xlCellTypeBlanks).Areas(1).Cells.Count
    
            If CCount = 0 Then
                MsgBox "There are no blank cells"
            ElseIf CCount = .Cells.Count Then
                MsgBox "There are more then 8192 areas"
            Else
                .SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
            End If
    
        End With
     
        On Error GoTo 0
    
    'Remove unwanted columns
        If Range("A1").Value = "Source" Then
                Range("A1:B1").EntireColumn.Delete
            End If
        If Range("B1").Value = "Booking Id" Then
                Range("B1:C1").EntireColumn.Delete
            End If
        If Range("U1").Value = "Accommodation Start Date" Then
                Range("U1").EntireColumn.Delete
            End If
        If Range("V1").Value = "Accommodation note" Then
                Range("V1").EntireColumn.Delete
            End If
        If Range("W1").Value = "Iac Contact" Then
                Range("W1:Z1").EntireColumn.Delete
            End If
            
    'Replacement code for inbound/outbound and female/male
            Range("A1").Value = "In/Out"
            Range("F1").Value = "M/F"
            Range("B1").Value = "Student ID"
            
        With Range("H:H")
            Cells.Replace What:="Female", Replacement:="F", Lookat:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        End With
        
        With Range("H:H")
            Cells.Replace What:="Male", Replacement:="M", Lookat:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        End With
        
        With Range("A:A")
            Cells.Replace What:="Inbound", Replacement:="I", Lookat:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        End With
        
         With Range("A:A")
            Cells.Replace What:="Outbound", Replacement:="O", Lookat:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        End With
    
            
    'Adjust height, width and formatting
        Dim Lastrow As Long
        Lastrow = Range("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Range("A2:Z" & Lastrow).RowHeight = 100
        Rows("1").RowHeight = 60
        Range("A1:Z" & Lastrow).Font.Size = 16
        
        Columns("A").EntireColumn.AutoFit
        Columns("B").ColumnWidth = 18.14
        Columns("C").ColumnWidth = 18.71
        Columns("D").ColumnWidth = 22
        Columns("E").ColumnWidth = 20.46
        Columns("F").EntireColumn.AutoFit
        Columns("G").ColumnWidth = 7.14
        Columns("L").ColumnWidth = 16.29
        Columns("J").ColumnWidth = 19.29
        Columns("K").ColumnWidth = 19.71
        Columns("L").ColumnWidth = 18.71
        Columns("U").ColumnWidth = 42.14
        Columns("V").ColumnWidth = 32
        
    ActiveWindow.Zoom = 40
    
    Application.ScreenUpdating = True
    Range("A1").Select
    End Sub
    Selecting either option on DialogBox1 also opens a new dialogbox which offers the option to remove people with no travel information by running the sub RemoveBlanks.

    Sub RemoveBlanks()
        Dim Firstrow As Long
        Dim Lastrow As Long
        Dim Lrow As Long
        Dim CalcMode As Long
        Dim ViewMode As Long
    
        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        End With
    
        With ActiveSheet
    
            .Select
            
            ViewMode = ActiveWindow.View
            ActiveWindow.View = xlNormalView
    
            .DisplayPageBreaks = False
    
            Firstrow = .UsedRange.Cells(1).Row
            Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
            
            For Lrow = Lastrow To Firstrow Step -1
    
            If Application.CountA(.Cells(Lrow, 1).Range("H1:S1")) = 0 Then .Rows(Lrow).Delete
    
            Next Lrow
    
        End With
    
        ActiveWindow.View = ViewMode
        With Application
            .ScreenUpdating = True
            .Calculation = CalcMode
        End With
    
    End Sub
    All of this code is in the PERSONAL.XLSB so that it runs every time.

    You'll need to fully close Excel to experience the error. Like I say if you put the code in and run it when excel is already open it will work completely fine. But if you open the file before excel is opened it will attempt to run the code (as I want it to) but it won't work.
    Last edited by TBrooker; 06-26-2013 at 02:50 PM. Reason: Removed attachment as problem solved and to protect privacy.

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Method 'Range' Error When Code is Run Automatically When Opening Workbook

    Sorry, you've lost me.

    There's no code in the workbook you attached.

    If all this code is in your PERSONAL.XLSB that might be the problem - I would have expected the code to be in an add-in.

    Can I ask exactly what the process is?

    Are you downloading workbooks and then opening (with/without saving) them?

    Then the code should be executed, and DialogButton1 should only be shown if the downloaded workbook's active sheet has "Transfer Bookings Report" in B3.

    By the way, when did you start having problems with the code?

  14. #14
    Registered User
    Join Date
    07-14-2009
    Location
    Brighton, England
    MS-Off Ver
    Excel 97
    Posts
    49

    Re: Method 'Range' Error When Code is Run Automatically When Opening Workbook

    None of the code is in the workbook I am opening.

    Since I am opening a new workbook which is produced by an outside program I have to run all the code from the personal.xlsb workbook since that is always opened. The workbook I have uploaded is a sample of the workbook produced by this program. The code I posted is all present in the personal.xlsb (which for some reason I can't export -.-).

    The process is as follows:

    Report is produced by a program.
    I open the report (without saving).
    Excel opens.
    The code runs as a result of what is in the personal.xlsb.
    The error appears.
    I press debug and click play and the code runs perfectly fine.
    I can then close the VBA code and open up any future excel documents and the code will work fine and show the dialog box or not show the dialog box depending what document I open.

    I've had the same problem since the code was first created.

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646
    Have you tried saving the workbook(s) first?

  16. #16
    Registered User
    Join Date
    07-14-2009
    Location
    Brighton, England
    MS-Off Ver
    Excel 97
    Posts
    49

    Re: Method 'Range' Error When Code is Run Automatically When Opening Workbook

    Yes, it makes no difference. I can replicate the error opening saved versions of the same workbook. It will give me the error regardless of what I open in fact as it's an error at the start of excel opening rather than one as a result of anything in or not in the workbook.

    I think what really needs to happen is the code needs to delay itself from running until after the worksheet is actually open.

  17. #17
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646
    I think the problem could be because the code is running on PERSONAL.XLSB.

    You check if the workbook being opened is an add-in.

    Now as far as I know PERSONAL.XLSB isn't an add-in so it doesn't get caught by that check and the code proceeds.

    Since there are no worksheets in PERSONAL.XLSB you get the error.

  18. #18
    Registered User
    Join Date
    07-14-2009
    Location
    Brighton, England
    MS-Off Ver
    Excel 97
    Posts
    49

    Re: Method 'Range' Error When Code is Run Automatically When Opening Workbook

    That makes sense. Would there be a way to point it to the workbook which is being opened? Otherwise perhaps I can run something which opens up a sheet in PERSONAL.XLSB while the macro runs and then closes it again afterwards hopefully all in the background.

    Edit: Actually it wouldn't sorry. Since the error occurs on
    If Not Range("C1").Value = "Transfer Bookings Report" Then
    and not at an earlier point.

    Edit Again: Just looking into it more it is almost definitely a problem in the fact that the macro runs through before the workbook has had a chance to open. So I need to find a way to stop the macro running until the workbook is open. I could do this by instead making the personal.xlsb open a dialog box asking if I want to run the macro instead and then clicking yes or no to run everything I ran automatically every time before but this seems like a bad workaround so has anyone any other ideas?
    Last edited by TBrooker; 06-25-2013 at 09:35 AM.

  19. #19
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Method 'Range' Error When Code is Run Automatically When Opening Workbook

    I'm pretty sure the problem is that the code runs on PERSONAL.XLSB as that's actually the first workbook that's opened when you open Excel.

    Pretty sure the last time I used a PERSONAL workbook that's what happened, I can even remember the splash screen showing a progress bar with Loading PERSONAL.XLSM..., or something similar.

    Why not add a check to see if the workbook that's been opened is the PERSONAL.XLSB, if it is don't run the code and if it isn't run the code?
    Private Sub App_WorkbookOpen(ByVal wb As Workbook)
        If wb.Name <> "PERSONAL.XLSB" Then
            CheckBook
        End If
    End Sub

  20. #20
    Registered User
    Join Date
    07-14-2009
    Location
    Brighton, England
    MS-Off Ver
    Excel 97
    Posts
    49

    Re: Method 'Range' Error When Code is Run Automatically When Opening Workbook

    It worked!

    Better yet I understand why it works with your explanation.

    Thank you very much for this.

+ 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