+ Reply to Thread
Results 1 to 2 of 2

error 1004, method of class/object failed

  1. #1
    Registered User
    Join Date
    10-23-2006
    Posts
    1

    error 1004, method of class/object failed

    Hi there,

    I keep on running into some errors with vba excel. The goal is to copy some columns and delete some rows in a large file depending on the values of certain cells in the file. (I have the file open while trying to do so.) Heard it may have something to do with vba creating its own objects so I hardcoded that, to no avail.

    Option Explicit

    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
    Set xlApp = CreateObject("Excel.Application")
    Set xlBook = xlApp.Workbooks.Add
    Set xlSheet = xlBook.Worksheets("Sheet1")

    Dim i
    Dim CurrentCell
    Dim OldComp
    Dim NewComp

    xlSheet.Range("U1").Select
    ActiveCell.FormulaR1C1 = "Latency"
    xlSheet.Range("U2").Select
    ActiveCell.FormulaR1C1 = "=RC[-18]/256"
    xlSheet.Range("U2").Select
    Selection.AutoFill Destination:=Range("U2:U50000"), Type:=xlFillDefault

    HERE (LAST LINE) AN ERROR OCCURS: AUTOFILL METHOD OF RANGE CLASS FAILED. OCCURS ALSO IF I PUT 'xlSheet' IN FRONT OF 'RANGE' IN THIS LAST LINE. ERROR DISAPPEARS WHEN I GED RID OF 'xlSheet' IN THE LINE BEFORE THAT

    xlSheet.Range("V1").Select
    ActiveCell.FormulaR1C1 = "Type"
    Range("V2").Select
    ActiveCell.FormulaR1C1 = "target"
    Selection.AutoFill Destination:=Range("V2:V50000"), Type:=xlFillDefault
    xlSheet.Columns("T:T").Select
    Selection.Copy
    xlSheet.Columns("W:W").Select
    ActiveSheet.Paste

    OldComp = 0
    For i = 0 To 50000
    CurrentCell = "W" & i + 2
    xlSheet.Range("CurrentCell").Activate

    HERE AN ERROR OCCURS: METHOD RANGE OF OBJECT _WORKSHEET FAILED. IF I LOSE THE 'xlSheet' PART, I GET: METHOD RANGE OF OBJECT _GLOBAL FAILED.


    NewComp = ActiveCell.Value
    If NewComp = OldComp Then
    xlSheet.Rows(i + 2).Delete
    End If

    OldComp = xlSheet.Range("CurrentCell").Value
    Next 'i
    End Sub

    Thanks for any help!

    Roy

  2. #2
    Forum Contributor
    Join Date
    03-13-2005
    Posts
    6,195
    Quote Originally Posted by daboyroy
    Hi there,

    I keep on running into some errors with vba excel. The goal is to copy some columns and delete some rows in a large file depending on the values of certain cells in the file. (I have the file open while trying to do so.) Heard it may have something to do with vba creating its own objects so I hardcoded that, to no avail.

    Option Explicit

    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
    Set xlApp = CreateObject("Excel.Application")
    Set xlBook = xlApp.Workbooks.Add
    Set xlSheet = xlBook.Worksheets("Sheet1")

    Dim i
    Dim CurrentCell
    Dim OldComp
    Dim NewComp

    xlSheet.Range("U1").Select
    ActiveCell.FormulaR1C1 = "Latency"
    xlSheet.Range("U2").Select
    ActiveCell.FormulaR1C1 = "=RC[-18]/256"
    xlSheet.Range("U2").Select
    Selection.AutoFill Destination:=Range("U2:U50000"), Type:=xlFillDefault

    HERE (LAST LINE) AN ERROR OCCURS: AUTOFILL METHOD OF RANGE CLASS FAILED. OCCURS ALSO IF I PUT 'xlSheet' IN FRONT OF 'RANGE' IN THIS LAST LINE. ERROR DISAPPEARS WHEN I GED RID OF 'xlSheet' IN THE LINE BEFORE THAT

    xlSheet.Range("V1").Select
    ActiveCell.FormulaR1C1 = "Type"
    Range("V2").Select
    ActiveCell.FormulaR1C1 = "target"
    Selection.AutoFill Destination:=Range("V2:V50000"), Type:=xlFillDefault
    xlSheet.Columns("T:T").Select
    Selection.Copy
    xlSheet.Columns("W:W").Select
    ActiveSheet.Paste

    OldComp = 0
    For i = 0 To 50000
    CurrentCell = "W" & i + 2
    xlSheet.Range("CurrentCell").Activate

    HERE AN ERROR OCCURS: METHOD RANGE OF OBJECT _WORKSHEET FAILED. IF I LOSE THE 'xlSheet' PART, I GET: METHOD RANGE OF OBJECT _GLOBAL FAILED.


    NewComp = ActiveCell.Value
    If NewComp = OldComp Then
    xlSheet.Rows(i + 2).Delete
    End If

    OldComp = xlSheet.Range("CurrentCell").Value
    Next 'i
    End Sub

    Thanks for any help!

    Roy
    Your problem is that you are referencing an incorrect point, it appears that your 'xlbook' is not correctly assigned, then the Sheet1 is not assigned, then the Cell range is not assigned, thus the '=D2/256' goes into the (previously) 'active' cell instead of U2.

    (by 'assigned', I mean 'activated/selected' etc)

    I hope this helps you track the error, I may find more details but the 'Select' / 'Activate' and when/why to do those is a little ahead of my current learning.

    Added:

    With
    Please Login or Register  to view this content.
    I can achieve what you appear to be doing, but after the msgbox I do not understand what you are trying to do.

    What was 'target' put into column V ?

    hth
    ---
    ---
    Last edited by Bryan Hessey; 10-24-2006 at 06:12 AM.
    Si fractum non sit, noli id reficere.

+ 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