Consulting

Results 1 to 3 of 3

Thread: Selecting a range of sheets using crit in my Criteria

  1. #1

    Selecting a range of sheets using crit in my Criteria

    I have the following code which works fine.

    [VBA]Sub ACreateKeyProgrammeReports()

    myCriteria = Range("A2:A5")
    SourceSheetNames = Array("Slip No Issue", "Slip No Issue PP2", "Slip With Issue", "Slip To Left", "New MS", "Deleted", "Future Complete", "Past Incomplete", "Missing", "No_Pres", "Estimated_Duration", "Overdue_Tasks", "Dependencies", "Dependencies2")
    For Each Crit In myCriteria
    With Sheets(Crit)
    ' Delete Old Stuff First
    Range(.Range("A1"), .Range("A1").End(xlDown)).EntireRow.Delete
    ' This will put the relevant line in from the summary tab for this programme
    ' Summary Line
    Sheets("Key Summary").Range("A1").AutoFilter Field:=1, Criteria1:=Crit
    Sheets("Key Summary").AutoFilter.Range.Copy .Range("A1")
    Sheets("Key Summary").Range("A1").AutoFilter Field:=1
    For Each SourceShtNme In SourceSheetNames
    .Range("A1").End(xlDown).Offset(1).Value = SourceShtNme
    Sheets(SourceShtNme).Range("A1").AutoFilter Field:=23, Criteria1:=Crit
    Sheets(SourceShtNme).AutoFilter.Range.Copy .Range("A1").End(xlDown).Offset(1)
    Sheets(SourceShtNme).Range("A1").AutoFilter Field:=23
    Next SourceShtNme
    End With
    Next Crit
    End Sub[/VBA]

    However, I want to change the line [VBA]With Sheets(Crit)[/VBA] to [VBA]With Sheets("Key Best Network", "Key Capacity", "Key NTT", "Key NTP")[/VBA]. This code does not work, I've also tried using array and this does not work either.

    I need to keep [VBA]myCriteria = Range("A2:A5")[/VBA] but need a second range for the sheets I need to use.

    Any help is much appreciated!

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I don't believe you can autofilter on multiple sheets. You will need to loop as shown. Simple items such as this will function.
    [VBA]Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
    Range("A3") = 3

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    The code as it stands does loop through multiple sheets. The original code has uses the same names for both criteria and sheets. All I am trying to change is the names of the sheets it pastes the info to.

Posting Permissions

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