+ Reply to Thread
Results 1 to 15 of 15

Searches all sheets in wb for exact string in column, copy/paste certain data from row

Hybrid View

  1. #1
    Registered User
    Join Date
    06-05-2013
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    90

    Searches all sheets in wb for exact string in column, copy/paste certain data from row

    I have a macro (much thanks and kudos to arlu) that will search through a workbook and pull data from a certain column in each sheet. What I need now is to be able to pull other parts of the row that data is in onto that same sheet.
    Macro is:
    Sub cons_data()
    Dim i As Long, lrow As Long
    
    Application.ScreenUpdating = False
    
    For i = 1 To Worksheets.Count
        With Worksheets(i)
            If .Name <> "Recommendations" Then
                lrow = .Range("L" & .Rows.Count).End(xlUp).Row
                If lrow > 9 Then .Range("L10:L" & lrow).Copy Worksheets("Recommendations").Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
            End If
        End With
    Next i
    
    With Worksheets("Recommendations")
        lrow = .Range("B" & .Rows.Count).End(xlUp).Row
        .Range("B5:B" & lrow).RemoveDuplicates Columns:=1, Header:=xlYes
    End With
    
    
    MsgBox "Done"
    
    Application.ScreenUpdating = True
    
    End Sub
    I need to pull the person responsible, the Risk ranking L&S which are the highest for Personnel, and the highest for Environmental, and then the Item no. which corresponds to those rankings.

    I have attached a testbook which gives an example.

    Thank you very much to anyone who can assist me!
    Attached Files Attached Files
    Last edited by bcas77; 07-11-2013 at 01:36 PM.

  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: Searches all sheets in wb for exact string in column, copy/paste certain data from row

    bcas77,

    Something like this?
    Sub tgr()
        
        Dim ws As Worksheet
        Dim wsDest As Worksheet
        Dim rngFound As Range
        Dim arrData(1 To 65000, 1 To 9)
        Dim DataIndex As Long
        Dim strFirst As String
        
        DataIndex = 0
        Set wsDest = Sheets("Recommendations")
        wsDest.Range("B6:M" & Rows.Count).ClearContents
        
        For Each ws In Sheets
            If ws.Name <> wsDest.Name Then
                Set rngFound = ws.Range("L10:L" & Rows.Count).Find("*", ws.Cells(Rows.Count, "L"), xlValues, xlWhole)
                If Not rngFound Is Nothing Then
                    strFirst = rngFound.Address
                    Do
                        DataIndex = DataIndex + 1
                        arrData(DataIndex, 1) = ws.Cells(rngFound.Row, "L").Text    'Recommendation
                        arrData(DataIndex, 2) = ws.Name                             'Section
                        arrData(DataIndex, 3) = ws.Cells(rngFound.Row, "F").Text    'Personnel Risk Ranking L
                        arrData(DataIndex, 4) = ws.Cells(rngFound.Row, "G").Text    'Personnel Risk Ranking S
                        arrData(DataIndex, 5) = ws.Cells(rngFound.Row, "H").Text    'Personnel Risk Ranking R
                        arrData(DataIndex, 6) = ws.Cells(rngFound.Row, "I").Text    'Environmental Risk Ranking L
                        arrData(DataIndex, 7) = ws.Cells(rngFound.Row, "J").Text    'Environmental Risk Ranking S
                        arrData(DataIndex, 8) = ws.Cells(rngFound.Row, "K").Text    'Environmental Risk Ranking R
                        arrData(DataIndex, 9) = ws.Cells(rngFound.Row, "M").Text    'Person Responsible
                        Set rngFound = ws.Range("L10:L" & Rows.Count).Find("*", rngFound, xlValues, xlWhole)
                    Loop While rngFound.Address <> strFirst
                End If
            End If
        Next ws
        
        If DataIndex > 0 Then wsDest.Range("B6").Resize(DataIndex, UBound(arrData, 2)).Value = arrData
        
        Set ws = Nothing
        Set wsDest = Nothing
        Set rngFound = Nothing
        Erase arrData
        
    End Sub
    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
    06-05-2013
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    90

    Re: Searches all sheets in wb for exact string in column, copy/paste certain data from row

    Oh wow! That's really close to what I'm looking for. Just one thing:

    Is it possible to pull what Item No. it is instead of the Section? I have it labeled incorrectly on the Recommendations page.

  4. #4
    Registered User
    Join Date
    06-05-2013
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    90

    Re: Searches all sheets in wb for exact string in column, copy/paste certain data from row

    Also, there is special formatting on the R columns, is it possible to take that too?

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

    Re: Searches all sheets in wb for exact string in column, copy/paste certain data from row

    Yeah, just change it from ws.Name to the same ws.Cells(rngfound.row, "column letter").text format of the others for the desired column letter.

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

    Re: Searches all sheets in wb for exact string in column, copy/paste certain data from row

    couodln't you just set up the same conditional formats on your 'Recommendations' sheet? There doesn't really need to be VBA for that...

  7. #7
    Registered User
    Join Date
    06-05-2013
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    90

    Re: Searches all sheets in wb for exact string in column, copy/paste certain data from row

    You're right, I was assuming the clear contents part of the code would take out the conditional formatting. Not sure why I thought that. Thank you again so much for your help!

  8. #8
    Registered User
    Join Date
    06-05-2013
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    90

    Re: Searches all sheets in wb for exact string in column, copy/paste certain data from row

    Hey I just was using the code and I'm getting an "Object variable or With block variable not set" from the Loop While line in the code from post #2. I tried it from a test workbook and it was working but I can't figure out what could be causing that error. Do you have any idea why I may get one of those?

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

    Re: Searches all sheets in wb for exact string in column, copy/paste certain data from row

    Can you post the code you're using? It's likely that you've adapted the code a bit, so I'd like to see exactly what the code is before I conjecture.

  10. #10
    Registered User
    Join Date
    06-05-2013
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    90

    Re: Searches all sheets in wb for exact string in column, copy/paste certain data from row

    Hey thanks for the quick reply! I've got a business trip next week and this code really saves me tons of time.

    Sub Recommendation()
    ' Pulls all recommendations into the recommendations tab.
    ' Does NOT remove duplicates or merge cells.
    
        Dim ws As Worksheet
        Dim wsDest As Worksheet
        Dim rngFound As Range
        Dim arrData(1 To 65000, 1 To 9)
        Dim DataIndex As Long
        Dim strFirst As String
        
        DataIndex = 0
        Set wsDest = Sheets("Recommendations")
        wsDest.Range("B6:M" & Rows.Count).ClearContents
        
        For Each ws In Sheets
            If ws.Name <> wsDest.Name Then
                Set rngFound = ws.Range("L10:L" & Rows.Count).Find("*", ws.Cells(Rows.Count, "L"), xlValues, xlWhole)
                If Not rngFound Is Nothing Then
                    strFirst = rngFound.Address
                    Do
                        DataIndex = DataIndex + 1
                        arrData(DataIndex, 1) = ws.Cells(rngFound.Row, "L").Text    'Recommendation
                        arrData(DataIndex, 2) = ws.Cells(rngFound.Row, "A").Text    'Section
                        arrData(DataIndex, 3) = ws.Cells(rngFound.Row, "F").Text    'Personnel Risk Ranking L
                        arrData(DataIndex, 4) = ws.Cells(rngFound.Row, "G").Text    'Personnel Risk Ranking S
                        arrData(DataIndex, 5) = ws.Cells(rngFound.Row, "H").Text    'Personnel Risk Ranking R
                        arrData(DataIndex, 6) = ws.Cells(rngFound.Row, "I").Text    'Environmental Risk Ranking L
                        arrData(DataIndex, 7) = ws.Cells(rngFound.Row, "J").Text    'Environmental Risk Ranking S
                        arrData(DataIndex, 8) = ws.Cells(rngFound.Row, "K").Text    'Environmental Risk Ranking R
                        arrData(DataIndex, 9) = ws.Cells(rngFound.Row, "M").Text    'Person Responsible
                        Set rngFound = ws.Range("L10:L" & Rows.Count).Find("*", rngFound, xlValues, xlWhole)
                    Loop While rngFound.Address <> strFirst
                End If
            End If
        Next ws
        
        If DataIndex > 0 Then wsDest.Range("B6").Resize(DataIndex, UBound(arrData, 2)).Value = arrData
        
        Set ws = Nothing
        Set wsDest = Nothing
        Set rngFound = Nothing
        Erase arrData
        
    End Sub
    I don't think I changed it but it's obviously possible I did

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

    Re: Searches all sheets in wb for exact string in column, copy/paste certain data from row

    The code looks good to me. It might be a problem with merged cells. Are there any merged cells in column L of the other worksheets?

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

    Re: Searches all sheets in wb for exact string in column, copy/paste certain data from row

    Hmm... I tested it with some merged cells but that isn't causing the error for me. Can you post a sample workbook that is experiencing the problem?

  13. #13
    Registered User
    Join Date
    06-05-2013
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    90

    Re: Searches all sheets in wb for exact string in column, copy/paste certain data from row

    Tons of them. I'll run through and try and fix that. So there's no way I can have any merged cells and make that work?

  14. #14
    Registered User
    Join Date
    06-05-2013
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    90

    Re: Searches all sheets in wb for exact string in column, copy/paste certain data from row

    Yeah it was definitely the merged cells.

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

    Re: Searches all sheets in wb for exact string in column, copy/paste certain data from row

    Ok, I figured it might be, but I couldn't reproduce the error on my machine. Glad to hear you've got it sorted. As far as merged cells go, anybody who's worked with Excel a lot will always tell you to never use merged cells at all costs.

+ 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