+ Reply to Thread
Results 1 to 5 of 5

Looping through data and finding cells that meet criteria

Hybrid View

  1. #1
    Registered User
    Join Date
    09-12-2013
    Location
    NW MN
    MS-Off Ver
    Excel 2010
    Posts
    17

    Looping through data and finding cells that meet criteria

    I have a worksheet that I wish to separate into separate worksheets by a persons name. I want to copy a row if it meets a certian criteria, D<>"" and paste it into the new worksheet. I can sort Column C wish will contain the persons name, and for every change in column C it will pull the next persons name and create a worksheet with that name and then loop through and if there is no change in column C and if column D <>"" then I want to copy that row into the worksheet. I kind of see what I want, and know what I want, I am just not familiar enough with VBA to understand how to GET what I want. I know there will be a loop and I feel like the more seasoned excellers out there will be able to get this.

    Thanks in advance!

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Looping through data and finding cells that meet criteria

    pbarry,

    Welcome to the forum!
    Have a look here: http://www.excelforum.com/tips-and-t...-criteria.html
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    09-12-2013
    Location
    NW MN
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Looping through data and finding cells that meet criteria

    Tiger, thanks for the welcome.

    I had a tough time going through that and picking out what I would need and what I wouldn't need. Is there a way possible that you could look at this file and tell me what I would need to do to make sheet1 turn into the tabs, by naming them and pulling the information where if it's blank skip and if it's populated, copy that row into the tab named the same as the offset of (0,-1). I think I may have an idea on how to do it with a workaround way, but i'd like an efficient way that will run the calculations clean and quick. This attachment shows the output, but in the code I will include to delete sheet1 but left it here so you can see a starting point. I'm not at that level yet, but hopefully soon after seeing the code and putting the pieces together.

    Test.xlsx

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Looping through data and finding cells that meet criteria

    Here's the code slightly customized. Note that I set the strCriteriaCol to "C" and I also added a filter for non blanks in column D:
    Sub SplitDataIntoSheetsByCriteria()
    'Macro created by TigerAvatar at www.excelforum.com, November 2012
    'Purpose is to split the data of a sheet into separate sheets based on a key column
    
        'Declare constants
        'Adjust these to suit your specific needs
        Const strDataSheet As String = "Sheet1"     'The name of the sheet that contains the data
        Const strCriteriaCol As String = "C"        'The letter of the column that will be evaluated
        Const lHeaderRow As Long = 1                'The number of the Header Row
        
        'Declare variables
        Dim ws As Worksheet             'Used to loop through the worksheets in order to alphabetize them
        Dim wsData As Worksheet         'Used to store the Data Sheet to a worksheet object variable
        Dim rngCriteria As Range        'Used to store the Range on the Data Sheet that will be evaluated
        Dim CriteriaCell As Range       'Used to loop through rngCriteria
        Dim lCalc As XlCalculation      'Used to store the current calculation state of the workbook
        Dim strNameWS As String         'Used to create legal worksheet names
        Dim i As Long                   'Generic looping variable
        
        'Set the wsData and rngCriteria variables using the constants declared at the top of this macro
        Set wsData = Sheets(strDataSheet)
        Set rngCriteria = wsData.Range(strCriteriaCol & lHeaderRow, wsData.Cells(Rows.Count, strCriteriaCol).End(xlUp).Offset(, 1))
        
        'Store the current calculation state, set calculation to manual, disable events, alerts, and screenupdating
        'This allows the code to run faster and prevents "screen flickering"
        With Application
            lCalc = .Calculation
            .Calculation = xlCalculationManual
            .EnableEvents = False
            .DisplayAlerts = False
            .ScreenUpdating = False
        End With
        
        'Assume code will fail and provide an error handler
        On Error GoTo CleanExit
        
        'Loop through each cell in rngCriteria
        For Each CriteriaCell In rngCriteria.Resize(, 1).Cells
            'Make sure the cell is after the Header Row
            If CriteriaCell.Row > lHeaderRow Then
                'Make sure the cell is not blank
                If Len(CriteriaCell.Text) > 0 Then
                    
                    'Generate a legal worksheet name
                    strNameWS = CriteriaCell.Text
                    For i = 1 To 7
                        strNameWS = Replace(strNameWS, Mid(":\/?*[]", i, 1), " ")
                    Next i
                    strNameWS = Left(WorksheetFunction.Trim(strNameWS), 31)
                    
                    'Check if there is already a sheet with the same name
                    If Not Evaluate("IsRef('" & strNameWS & "'!A1)") Then
                        'Need to create a new sheet
                        'Add the new sheet, name it appropriately, copy over the information based on the criteria
                        With Sheets.Add(After:=Sheets(Sheets.Count))
                            .Name = strNameWS
                            wsData.Rows(lHeaderRow).EntireRow.Copy .Range("A1")
                            rngCriteria.AutoFilter 1, CriteriaCell.Text 'Filter for the name
                            rngCriteria.AutoFilter 2, "<>"              'Filter for non-blanks
                            rngCriteria.Offset(1).EntireRow.Copy .Range("A2")
                        End With
                    End If
                End If
            End If
        Next CriteriaCell
        
        'Set the Data sheet to be the first sheet in the workbook and select that sheet so it is displayed after macro completes
        wsData.Move Before:=Sheets(1)
        wsData.Select
        
    'If there were any errors, the code immediately goes here
    'The code will also exit here even if there are no errors
    CleanExit:
        
        'Remove any remaining filters
        rngCriteria.AutoFilter
        
        'Set calculation back to what it was, re-enable events, alerts, and screenupdating
        With Application
            .Calculation = lCalc
            .EnableEvents = True
            .DisplayAlerts = True
            .ScreenUpdating = True
        End With
        
        'Display the error that occurred (if any) and clear the error
        If Err.Number <> 0 Then
            MsgBox Err.Description, , "Error: " & Err.Number
            Err.Clear
        End If
        
        'Object variable cleanup
        Set ws = Nothing
        Set wsData = Nothing
        Set rngCriteria = Nothing
        Set CriteriaCell = Nothing
        
    End Sub

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,816

    Re: Looping through data and finding cells that meet criteria

    Thread moved to appropriate forum.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Averaging data if cells in range meet criteria
    By gobbledok in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-24-2013, 02:28 AM
  2. move row data to a new tab when cells meet specific criteria
    By brian807 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 10-10-2012, 10:18 AM
  3. Finding how many cells meet a criteria
    By stujoed in forum Excel General
    Replies: 3
    Last Post: 06-30-2011, 04:55 PM
  4. Excel 2007 : Finding the first value to meet my criteria
    By bmercer54 in forum Excel General
    Replies: 5
    Last Post: 12-09-2009, 12:28 PM
  5. Delete data in cells that don't meet criteria
    By SITCFanTN in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 06-10-2006, 04:10 PM

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