+ Reply to Thread
Results 1 to 10 of 10

Method 'Range' of object '_Worksheet' failed

Hybrid View

  1. #1
    Registered User
    Join Date
    10-23-2012
    Location
    Sussex
    MS-Off Ver
    Excel 2007
    Posts
    12

    Method 'Range' of object '_Worksheet' failed

    I have set up a worksheet that has activex clickbuttons to insert some values into the criteria cells before I do a filter and then run a couple of macros. This works well with everything on a sheet named "dataextract", but I want to move the buttons or place new buttons on another sheet called "graph". The code that I use and works fine from the dataextract sheet is

    Private Sub OptionButton1_Click()
    
    If OptionButton1.Value = True Then Range("crit1").Value = ">1500"
    If OptionButton1.Value = True Then Range("crit2").Value = "<1650"
    
    Call dataextract
    Call datacopy      
    
    End Sub
    I then have other buttons with different values to copy to the two named cells crit1 and crit2 and run the 2 macros using basically the same scripts.

    However if I create a new button on the graph sheet with the same code it fails at the first step and gives a run time error 1004. I think that I have a basic problem addressing the cells on the dataextract sheet but can't seem to work out the correct format. any advice would be welcome.
    Last edited by jeffreybrown; 10-23-2012 at 10:12 AM. Reason: Please use code tags...Thanks.

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Method 'Range' of object '_Worksheet' failed

    I reckon you probably just need
    Private Sub OptionButton1_Click()
    Dim ws as worksheet
    set ws = sheets("dataextract")
    If OptionButton1.Value = True Then ws.Range("crit1").Value = ">1500"
    If OptionButton1.Value = True Then ws.Range("crit2").Value = "<1650"
    
    Call dataextract
    Call datacopy      
    
    End Sub
    Last edited by JosephP; 10-23-2012 at 12:42 PM. Reason: typo
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Registered User
    Join Date
    10-23-2012
    Location
    Sussex
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Method 'Range' of object '_Worksheet' failed

    Thank you Joseph, you are absolutely correct and now the first part of the process works fine to copy values into the crit1 and crit2 ranges. I thought I might be able to fix the macros that are called in the same manner so what I have come up with for the filtering process is

    Sub dataextract()
    '
    ' dataextract Macro
    '
    '
    Dim ws As Worksheet
    Set ws = Sheets("dataextract")
    
        ws.Range("A11").Select
        Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
        Selection.ClearContents
        ws.Range("criteria").Select
        Range("datarange").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:= _
            Range("criteria"), CopyToRange:=Range("outputrange"), Unique:=False
    End Sub
    but I have messed up somewhere along the line or specifying the work on the dataextract sheet, any ideas please ?

    This is the macro before I messed with it, and it works fine when run from the dataextract sheet.
    Sub dataextract()
    '
    ' dataextract Macro
    '
        Range("A11").Select
        Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
        Selection.ClearContents
        Range("B2:C3").Select
        Range("datarange").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:= _
            Range("B2:C3"), CopyToRange:=Range("A10:i10"), Unique:=False
    End Sub
    as you can see, this is all quite new to me !
    Last edited by Burli; 10-23-2012 at 01:07 PM.

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Method 'Range' of object '_Worksheet' failed

    please edit your post to use code tags before we proceed in accordance with the forum rules, as Jeffrey requested in his edit to your original post

  5. #5
    Registered User
    Join Date
    10-23-2012
    Location
    Sussex
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Method 'Range' of object '_Worksheet' failed

    Thanks Joseph for the tag advice, I had missed the note from Jeffery, and as this is my first time on this forum I didn't know how you made your reply look so neat

  6. #6
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Method 'Range' of object '_Worksheet' failed

    Sub dataextract()
    '
    ' dataextract Macro
    '
    '
    Dim ws As Worksheet
    Set ws = Sheets("dataextract")
    with ws
        .Range("A11", .usedrange.SpecialCells(xlLastCell)).ClearContents
        .Range("datarange").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:= _
            .Range("criteria"), CopyToRange:=.Range("outputrange"), Unique:=False
    end with
    End Sub

  7. #7
    Registered User
    Join Date
    10-23-2012
    Location
    Sussex
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Method 'Range' of object '_Worksheet' failed

    Thank you Joseph for your help with this and it has been a great help. I think the VBA bug has bitten and I have ordered a book on the subject to try to learn a bit more about it. I used the last code that you sent me, but have a slight problem with it as it again fails with a 1004 error and highlights the following text
        .Range("datarange").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:= _
            .Range("criteria"), CopyToRange:=.Range("outputrange"), Unique:=False
    I believe that this may come from the fact that the data in "datarange" is in fact in Sheet1, something that I failed to mention. Is it possible to include a statement to add Sheet1?

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Method 'Range' of object '_Worksheet' failed

    yes-it's just
    Sheets("Sheet1").Range("datarange").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:= _
            .Range("criteria"), CopyToRange:=.Range("outputrange"), Unique:=False

  9. #9
    Registered User
    Join Date
    10-23-2012
    Location
    Sussex
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Method 'Range' of object '_Worksheet' failed

    Yes, just perfect. Many many thanks for your help Joseph, it has solved what I have spent hours trying to resolve.

  10. #10
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Method 'Range' of object '_Worksheet' failed

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

+ 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