Ozgrid Excel Help Forums & Excel Best Practices


XL Templates | XL Add-ins | XL Training | XL Estimating | XL Scheduling | XL Recovery | XL Trading | XL Financial | XL Conversion | XL Charting


<
+ Reply to Thread
Results 1 to 4 of 4

Thread: Run-time Error 1004 Global Object Failed

  1. #1
    Join Date
    15th September 2006
    Posts
    132

    Run-time Error 1004 Global Object Failed

    Hey guys, i've been doing some playing around trying to get this project im working on to come together. Below I wrote a code to export a query from access to excel. Now it works perfectly the first time I run it, but then if I run it again the line below in red stops the code and gives me a Run-time error '1004'

    "Method 'Range' of object'_Global' failed"

    I have tried everything I could find including closing down the application and setting all the objects to nothing... and i still get this message after everytime I run it once.

    Code:
    Private Sub Command44_Click()
    
    Dim objXls As Excel.Application
    Dim MyBook As Excel.Workbook
    Dim MySheet As Excel.Worksheet, MySheet2 As Excel.Worksheet
    Dim MyFile As String
    Dim MyRow As Integer
    Dim MyRange As Range
    
    DoCmd.TransferSpreadsheet acExport, 8, "qryWeeklyHSET", "C:\Documents and Settings\tmshepe\Desktop\HSET Database\HSETTest.xls", True
    
    Set objXls = CreateObject("Excel.Application")
        MyFile = "C:\Documents and Settings\tmshepe\Desktop\HSET Database\HSETTest.xls"
        objXls.Workbooks.Open ("" & MyFile)
        objXls.Visible = True
        
    Set MyBook = objXls.Workbooks("HSETTest.xls")
    Set MySheet = MyBook.Worksheets("qryWeeklyHSET")
    Set MySheet2 = MyBook.Worksheets("Incidents")
    
        MySheet.Activate
        
    MyRow = MySheet.Range(Range("A1"), Range("A1").End(xlDown)).Count
    
        MySheet.Range("A2:L" & MyRow).Copy
        MySheet2.Activate
        MySheet2.Range("A2").End(xlDown).Offset(1, 0).PasteSpecial
        objXls.Application.DisplayAlerts = False
        MySheet.Delete
        objXls.Application.DisplayAlerts = True
        MyBook.Saved = True
        MyBook.Close
        objXls.Application.Quit
        objXls.Visible = False
    Set objXls = Nothing
    Set MyBook = Nothing
    Set MySheet = Nothing
    Set MySheet2 = Nothing
    End Sub
    Any suggestions? Thanks in advance.

  2. #2
    Join Date
    12th September 2006
    Location
    Texas
    Posts
    4,120

    Re: Run-time Error 1004 Global Object Failed

    If MySheet is not active then you need to qualify all the references:

    Code:
    with mysheet
        MyRow = .Range(.Range("A1"), .Range("A1").End(xlDown)).Count 
    end with
    You name your kids--why not your variables? Use Option Explicit.

  3. #3
    Join Date
    15th September 2006
    Posts
    132

    Re: Run-time Error 1004 Global Object Failed

    Geez you guys are smart... worked perfectly. Thank you very much!

  4. #4
    Join Date
    12th September 2006
    Location
    Texas
    Posts
    4,120

    Re: Run-time Error 1004 Global Object Failed


    Create Excel dashboards quickly with Plug-N-Play reports.
    For what you're doing, this is simpler:

    Code:
    MyRow = mysheet.Range("A1").End(xlDown)).row
    You name your kids--why not your variables? Use Option Explicit.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Possible Answers

  1. Replies: 3
    Last Post: March 9th, 2007, 02:02
  2. Replies: 4
    Last Post: August 7th, 2006, 21:47
  3. Replies: 8
    Last Post: August 1st, 2006, 09:14
  4. Replies: 2
    Last Post: June 28th, 2006, 11:02
  5. Error - Method 'Range' of 'object' Global Failed
    By Zeigie in forum EXCEL HELP
    Replies: 7
    Last Post: June 18th, 2005, 02:00

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