Macro to copy data to new sheet based on criteria, error 1004.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scrapcode
    New Member
    • Apr 2010
    • 26

    #1

    Macro to copy data to new sheet based on criteria, error 1004.

    Hi everyone. I'm trying to write a macro to copy data from one Excel sheet to another in the same workbook when a certain criteria is met.

    My data table is an import from an Access DB. After the data has been refreshed to the sheet I would like to pick out all the entries where the value in column I is "High". Once the table has been filtered the values from column A should be pasted to sheet 1("summary").

    I will need more that just the information from column A, but as it's a unique ID I can just run Vlookup's off it to keep things simple for now.

    I've tried a number of macro's but I'm always hanging up at the same point. Here's the code I'm using so far:

    Code:
    Application.ScreenUpdating = False
    With Worksheets("Data")
        .Range("$A$1:$N$5000").AutoFilter Field:=9, Criteria1:="High"
        .Range("A:A").SpecialCells(xlCellTypeVisible).Copy _
            Worksheets("Summary").Range("B18")
        .Range("G:G").SpecialCells(xlCellTypeVisible).Copy _
            Worksheets("Summary").Range("C18")
        .Range("J:J").SpecialCells(xlCellTypeVisible).Copy _
            Worksheets("Summary").Range("D18")
        .Range("a1").AutoFilter
    End With
    Application.ScreenUpdating = True
    The code is debugs here:
    ".Range("$A$1:$ N$5000").AutoFi lter Field:=9,"

    I've tried using a different macro, just to filter. Then I can use the parts of the above code to select and paste but what ever I do I get the run time error 1004 "Auto-filter method of range class failed".

    I'm using Excel 2010, the data has been automatically formatted as a table when it was brought in by a data import query from Access 2003. Also, as this is only a test version the table only has 6 rows of data.

    Thanks very much for any help or insight.
  • Mihail
    Contributor
    • Apr 2011
    • 759

    #2
    I have tried your code and is OK.

    I can think to only one problem:
    Have you a worksheet named... Data ?
    Sorry if it is a stupid question.

    Comment

    • Mihail
      Contributor
      • Apr 2011
      • 759

      #3
      Yes. It is a stupid question because your code execute line 2 with no problem.

      This code will do your job:
      Code:
      Sub CopyFilteredValues()
      'Preparatory
      Dim CriteriaValue As String, CriteriaColumn As Long, CriteriaFirstRow As Long, CriteriaLastRow As Long
          CriteriaValue = "High" 'Here you can change the criteria _
                                  without need to change the entire code
          CriteriaColumn = 9 'The column where to looking for Crit value
          CriteriaFirstRow = 1 'Starting row to search for CriteriaValue
          CriteriaLastRow = 50000 'Last row to search for CriteriaValue
      
      
      Dim WSdata As Worksheet, WSsummary As Worksheet
          Set WSdata = Worksheets("Data")
          Set WSsummary = Worksheets("Summary")
      
      Dim Rsummary As Long
          Rsummary = 18 - 1 'The row in Summary worksheet where the filtered values will be copied
          
      Dim Rdata As Long 'Row in worksheet "Data" - working variable
      
      'Start
          WSsummary.Cells.ClearContents 'Remove al data from "Summary"
          For Rdata = CriteriaFirstRow To CriteriaLastRow
              If WSdata.Cells(Rdata, CriteriaColumn) = CriteriaValue Then
                  Rsummary = Rsummary + 1 'Next row in worksheet "Summary"
                  WSsummary.Cells(Rsummary, 2) = WSdata.Cells(Rdata, 1) 'Copy column A (from "Data") to Column B (from "Summary")
                  WSsummary.Cells(Rsummary, 3) = WSdata.Cells(Rdata, 7) 'Copy column G (from "Data") to Column C (from "Summary")
                  WSsummary.Cells(Rsummary, 4) = WSdata.Cells(Rdata, 10) 'Copy column J (from "Data") to Column D (from "Summary")
              End If
          Next Rdata
          
          WSsummary.Activate 'Go to "Summary" worksheet
      Exit Sub
      
      'Your code Is here
      '    Application.ScreenUpdating = False
      '    With Worksheets("Data")
      '        .Range("$A$1:$N$5000").AutoFilter Field:=9, Criteria1:="High"
      '        .Range("A:A").SpecialCells(xlCellTypeVisible).Copy _
      '            Worksheets("Summary").Range("B18")
      '        .Range("G:G").SpecialCells(xlCellTypeVisible).Copy _
      '            Worksheets("Summary").Range("C18")
      '        .Range("J:J").SpecialCells(xlCellTypeVisible).Copy _
      '            Worksheets("Summary").Range("D18")
      '        .Range("a1").AutoFilter
      '    End With
      '    Application.ScreenUpdating = True
      End Sub

      Comment

      • scrapcode
        New Member
        • Apr 2010
        • 26

        #4
        Thanks very much for the suggested code Mihail. I'll have a look at it over the weekend and see if it does the business.

        Cheers.

        Comment

        • scrapcode
          New Member
          • Apr 2010
          • 26

          #5
          Thanks very much for the solution Mihail, worked a treat.

          Comment

          • jonnycakes
            New Member
            • Jan 2012
            • 24

            #6
            Could you not just create a query in access and use docmd.transfers preadsheet, dao, or ado connection?

            Comment

            Working...