Page 1 of 2 1 2 LastLast
Results 1 to 10 of 17

Thread: Error 1004 Select method of range class failed

  1. #1

    Error 1004 Select method of range class failed



    Register for a FREE account, and/
    or Log in to avoid these ads!

    Hi, I have this code that I am trying to generate a report
    Code:
    rivate Sub Cmdprintpreview_Click()
     
        Dim GetDate
        Dim strMonth, strYear
        Dim ws As Worksheet
     
       Set ws = Sheets("Returns")
       
        'get values from comboboxes
        strMonth = Me.Cbomonth.Value
        strYear = Me.Cboyear.Value
        
        
        GetDate = strMonth & "/" & strYear
        
        
        ws.Range("Period").Select
        Selection.AutoFilter
        Selection.AutoFilter Field:=3, Criteria1:=GetDate
        
        Unload Me
        ActiveSheet.PrintPreview    
        Selection.AutoFilter   
        FrmReport.Show
    I am getting this error can anyone help thanks

  2. #2
    Super Moderator JeffreyWeir's Avatar
    Join Date
    Mar 2011
    Location
    New Zealand
    Posts
    357
    Articles
    0
    IF you push DEBUG when the error comes up, what line is highlighted?

    If it's the ews.Range("Period").Select line, then you need to activate the sheet first.

    with ews
    .activate
    .range("Period").Select
    end with

  3. #3
    It is highlighting this line ws .Range("Period").Select

  4. #4
    Super Moderator JeffreyWeir's Avatar
    Join Date
    Mar 2011
    Location
    New Zealand
    Posts
    357
    Articles
    0
    Okay, so did my code above fix it?

  5. #5
    It didn't I'm getting a compilation error

  6. #6
    Super Moderator JoePublic's Avatar
    Join Date
    Sep 2011
    Location
    Askew
    Posts
    183
    Articles
    0
    Excel Version
    2010
    How about (air code)
    Code:
    Private Sub Cmdprintpreview_Click()
         Dim GetDate
        Dim strMonth, strYear
        Dim ws As Worksheet
        Set ws = Sheets("Returns")
           'get values from comboboxes
        strMonth = Me.Cbomonth.Value
        strYear = Me.Cboyear.Value
                GetDate = strMonth & "/" & strYear
    with ws.Range("Period")
    .AutoFilter
    .AutoFilter Field:=3, Criteria1:=GetDate
            Unload Me
    ws.PrintPreview
    .AutoFilter
    end with
    FrmReport.Show
    Circumference of a circle = 2πr²



    ²the circle's radius

  7. #7
    Super Moderator JeffreyWeir's Avatar
    Join Date
    Mar 2011
    Location
    New Zealand
    Posts
    357
    Articles
    0
    My mistake, that should have been:
    Code:
    with ws
         .activate
         .range("Period").Select
    end with
    I accidentally typed an e in front of ws.

    And JoePublic makes an excellent point that you don't always have to select something to do something to it. Although you do have to select a worksheet if you then want to select a range within that sheet.

  8. #8
    I realized the mistake I am still getting the runtime error on this line Selection.AutoFilter

    thanks

  9. #9
    Thanks Joe

    I tried the code but is receiving the runtime error on this line .AutoFilter
    Last edited by jams; 2013-06-15 at 01:09 AM.

  10. #10
    Super Moderator JeffreyWeir's Avatar
    Join Date
    Mar 2011
    Location
    New Zealand
    Posts
    357
    Articles
    0
    is the range Period referring to the entire table, or just one column?

Page 1 of 2 1 2 LastLast

Posting Permissions

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