+ Reply to Thread
Results 1 to 7 of 7

Search matching Info in One Column and carry those rows into new worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    09-19-2012
    Location
    Florida
    MS-Off Ver
    Excel 2010
    Posts
    4

    Search matching Info in One Column and carry those rows into new worksheet

    I am going to go crazy. I see similar issues, but cannot adjust the macros I have accordingly. I am no excel pro, but know a lot. Any help is so much appreciated! I am creating a directory and would like to search column F for matching village initials, then create sub worksheets, carry specific rows (shown in macros) over for each of the members for live in those specific villages. So I will ultimately end up having x amount of worksheets. But after I have the initial macro worked out, I can create it for each village. Thank you! This is the macros I have:

    Sub test()
    Application.ScreenUpdating = False
    On Error Resume Next
    Application.DisplayAlerts = False
    Sheets("NewSheet").Delete
    Application.DisplayAlerts = True
    x = Directory
    Sheets.Add
    Directory = "NewSheet"
    Range("A1") = "Surname"
    Range("B1") = "title"
    Range("C1") = "Address2"
    Range("D1") = "telephone"
    Range("E1") = "phone/fax"
    Range("F1") = "village"
    Range("G1") = "email1"
    Range("H1") = "email2"
    Sheets(x).Select
    x = Range("F" & Rows.Count).End(xlUp).Row
    For Each Cell In Range("F2:F734" & x)
    If InStr(LCase(Cell), "BB") <> 0 Then
    Cell.EntireRow.Copy _
    Sheets("NewSheet").Range("A" & Rows.Count).End(xlUp).Offset(1)
    Cell.Value = "#N/A"
    End If
    Next Cell
    Columns("F:F").SpecialCells(xlCellTypeConstants, 9).EntireRow
    Sheets("NewSheet").Columns.AutoFit
    Application.ScreenUpdating = True
    End Sub
    I had to delete a lot of the information for confidentiality. Hope it is still understandable.

    DIRECTORYTEST.xls
    Last edited by Cutter; 09-20-2012 at 09:56 AM. Reason: Added code tags

  2. #2
    Registered User
    Join Date
    05-30-2012
    Location
    St Paul, MN
    MS-Off Ver
    2010
    Posts
    51

    Re: Search matching Info in One Column and carry those rows into new worksheet

    Try this on a COPY of your workbook.
    Sub TestCleanedUP()
    'This macro creates a new worksheet for each village name in Column F of the 'Directory' sheet when
    'it is ran.  It will then copy over the applicable columns from the active sheet to the appropriate
    'new sheets columns.
    'CAUTION: As written, this macro First DELETES all sheets that aren't named 'Directory'.
    'If instead, you wish it to continue adding onto existing worksheets, you'll need to remove
    'the commented snippet.
    
        Dim wsSource As Worksheet
        Dim lOutputRow As Long
        Dim i As Long
        Dim lr As Long
        Dim sVillageCode As String
        Dim ws As Worksheet
    
        Set wsSource = Worksheets("directory")
    
    
        Application.ScreenUpdating = False
        'For now, I put in a "Delete all sheets but Directory"  remove this snippet if needed.
        For Each ws In Worksheets
            If LCase(ws.Name) <> "directory" Then
                Application.DisplayAlerts = False
                ws.Delete
                Application.DisplayAlerts = True
            End If
        Next ws
    
        lr = wsSource.Range("F" & wsSource.Rows.Count).End(xlUp).Row
    
        For i = 2 To lr
            'I'm assuming that the village names/initials are case-insensitive, but could have
            'leading or trailing spaces.
            sVillageCode = Trim(UCase(wsSource.Range("F" & i)))
    
            'Check to see if a village code is listed.  If it isn't, we'll just put it into a sheet called
            'Unknown Village
            If Trim(sVillageCode) = "" Then sVillageCode = "Unknown Village"
            'check to see if there's currently a sheet with that villages name.  If not, then create a sheet and
            'put in the headers.
            If Not WorksheetExists(sVillageCode) Then
                Call CreateSheetWithHeaders(sVillageCode)
            End If
            'find the currently last used row on the ouput sheet, and add one to place stuff in the right spot
            lOutputRow = Worksheets(sVillageCode).Range("F" & Worksheets(sVillageCode).Rows.Count).End(xlUp).Row + 1
    
            'I personally like to avoid using copy and paste in code, unless I'm messing around with fancy cell formatting...
            'Also, The output sheets columns don't match up with a simple copy/paste.
            'we'll need to Explicitly say which column in the output sheet comes from which input sheet.
            Worksheets(sVillageCode).Range("A" & lOutputRow) = wsSource.Range("C" & i)
            Worksheets(sVillageCode).Range("B" & lOutputRow) = wsSource.Range("D" & i)
            Worksheets(sVillageCode).Range("C" & lOutputRow) = wsSource.Range("E" & i)
            Worksheets(sVillageCode).Range("D" & lOutputRow) = wsSource.Range("I" & i)
            Worksheets(sVillageCode).Range("E" & lOutputRow) = wsSource.Range("J" & i)
            Worksheets(sVillageCode).Range("F" & lOutputRow) = wsSource.Range("F" & i)
            Worksheets(sVillageCode).Range("G" & lOutputRow) = wsSource.Range("V" & i)
            Worksheets(sVillageCode).Range("H" & lOutputRow) = wsSource.Range("W" & i)
    
        Next i
    
        'Clean up the column sizes.
        For Each ws In Worksheets
            ws.Cells.EntireColumn.AutoFit
        Next ws
        Application.ScreenUpdating = True
    End Sub
    
    Private Sub CreateSheetWithHeaders(sWSName As String)
        Dim wsNew As Worksheet
        Set wsNew = Worksheets.Add
        wsNew.Name = sWSName
        wsNew.Range("A1") = "Surname"
        wsNew.Range("B1") = "title"
        wsNew.Range("C1") = "Address2"
        wsNew.Range("D1") = "telephone"
        wsNew.Range("E1") = "phone/fax"
        wsNew.Range("F1") = "village"
        wsNew.Range("G1") = "email1"
        wsNew.Range("H1") = "email2"
        Set wsNew = Nothing
    End Sub
    
    Private Function WorksheetExists(sWSName As String) As Boolean
        Dim ws As Worksheet
        On Error GoTo NotThere
        Set ws = Worksheets(sWSName)
        Set ws = Nothing
        WorksheetExists = True
        Exit Function
    NotThere:
        WorksheetExists = False
    End Function

  3. #3
    Registered User
    Join Date
    09-19-2012
    Location
    Florida
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Search matching Info in One Column and carry those rows into new worksheet

    That was amazingly PERFECT. Exactly what I was looking to do! Thank you so much!

  4. #4
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Search matching Info in One Column and carry those rows into new worksheet

    @ saduff

    Welcome to the forum.

    Please notice that code tags have been added to your post(s). The forum rules require them so please keep that in mind and add them yourself whenever showing code in any of your future posts. To see instructions for applying them, click on the Forum Rules button at the top of the page and read Rule #3.
    Thanks.

    Based on your last post it seems that you are satisfied with the solution(s) you've received but you haven't marked your thread as SOLVED. I'll do that for you now but please keep in mind for your future threads that Rule #9 requires you to do that yourself. If your problem has not been solved you can use Thread Tools (located above your first post) and choose "Mark this thread as unsolved".
    Thanks.

    Also, as a new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

  5. #5
    Registered User
    Join Date
    09-19-2012
    Location
    Florida
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Search matching Info in One Column and carry those rows into new worksheet

    cjo...can you help me again, PLEASE!? I would like to add a sheet called "Master Directory" that the village sheets information would be pulled from...this in turn would be my master worksheet. My "Directory" file would be a secondary worksheet that all of the identical information from "Master Directory" is pulled from. "Directory" will almost be a carbon copy of "Master Directory"--but I need to have the capability of deleting rows in the "Directory" that will be pulled over from Master Directory and stay deleted when the macros is run. Can the worksheets all just be updates rather than deleted?

    I really appreciate any help? I tried to examine the macros you created, but anything I worked out wasn't working, for fear I might make it worse, I am turning to you for more help! Thx!

  6. #6
    Registered User
    Join Date
    05-30-2012
    Location
    St Paul, MN
    MS-Off Ver
    2010
    Posts
    51

    Re: Search matching Info in One Column and carry those rows into new worksheet

    I'm not entirely certain what you're asking...
    Let me restate: sheet 'Directory' is a subset of 'Master Directory' (everything on 'Directory' is on 'Master Directory', but not everything on 'Master Directory' is on 'Directory'), and you're creating this manually by copying 'Master Directory' and deleting rows. You then want the various village sheets to be created (if not already existing) or updated (if existing) using the 'Directory' sheet as a source.

    The problem I can see happening with this is the addition of duplicate records onto the various Village tabs -- as written, the macro doesn't check for uniqueness. It's quite possible to trap for this, but I don't see a (single field) unique identifier. I do notice that it appears that you have what appears to be near duplicate records already in you 'Directory' sheet, though; for example, Member ID 2435 appears twice, with the only difference being the values in the 'Title' field being slight rearrangements of each other.

  7. #7
    Registered User
    Join Date
    09-19-2012
    Location
    Florida
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Search matching Info in One Column and carry those rows into new worksheet

    First let me explain that I am creating a Member Directory Book for a Country Club. Some of our members own more than one property within the club, so, their name would be listed 2 or more times pending on how many properties they own in the Master Directory Worksheet. The Directory worksheet is what I am pulling from to list the members phone numbers, etc. to publish. I don't want to pull or publish their information 2 or more times if they own more than one property or not. HOWEVER...within the villages sheets, I want to pull from the Master Directory worksheet because I want their names listed in each village they do own a property. So, I want to maintain the integrity of the cells in the Master Directory Worksheet and thru out at all times.

    Does that make any more sense? Sorry...I know what I want, but its hard to explain!

    I'm not entirely certain what you're asking...
    Let me restate: sheet 'Directory' is a subset of 'Master Directory' (everything on 'Directory' is on 'Master Directory', but not everything on 'Master Directory' is on 'Directory')(correct)[/COLOR], and you're creating this manually by copying 'Master Directory' and deleting rows. But I don't want to just copy/paste, I want to keep the integrity of the cells. You then want the various village sheets to be created (if not already existing) or updated (if existing) using the 'Directory' sheet as a source. No, using Master Directory.

    The problem I can see happening with this is the addition of duplicate records onto the various Village tabs -- as written, the macro doesn't check for uniqueness. It's quite possible to trap for this, but I don't see a (single field) unique identifier. I do notice that it appears that you have what appears to be near duplicate records already in you 'Directory' sheet, though; for example, Member ID 2435 appears twice, with the only difference being the values in the 'Title' field being slight rearrangements of each other.

+ 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