Results 1 to 14 of 14

Vba/Macro Search Rows for value and copy/paste accordingly

Threaded View

  1. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Vba/Macro Search Rows for value and copy/paste accordingly

    Hi Aquabat

    This Code is in the attached and appears to do as you require...let me know of issues...keyboard shortcut...CTRL + x
    Option Explicit
    
    Sub Create_Reports()
        Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
        Dim cel As Range
        Dim LR As Long
        Dim vWs As Variant
    
        Set ws1 = Sheets("Store Ad Display")
        Set ws2 = Sheets("Core Products")
        Set ws3 = Sheets("Email to Locals")
    
        Application.ScreenUpdating = False
        If Not Evaluate("ISREF(Lists!A1)") Then
            Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Lists"
        Else
            Sheets("Lists").Cells.Clear
        End If
    
        With ws1
            LR = .Range("C" & .Rows.Count).End(xlUp).Row
    
            .Range("C2:C" & LR).AdvancedFilter Action:=xlFilterCopy, _
                    CopyToRange:=Sheets("Lists").Range("A1"), Unique:=True
    
            ActiveWorkbook.Names.Add Name:="Products", RefersTo:= _
                    "=OFFSET(Lists!$A$2,0,0,(COUNTA(Lists!$A:$A)-1),1)"
        End With
    
        For Each cel In Range("Products")
            For Each vWs In Array(ws1, ws2, ws3)
                With vWs
                    Select Case vWs.Name
                    Case ws1.Name
                        With ws1
                            If Not .AutoFilterMode Then
                                .Range("A2").AutoFilter
                                .Range(("A2"), .Range("A2").End(xlDown)).AutoFilter Field:=3, Criteria1:=cel.Value & "*"
    
                                If Not Evaluate("ISREF('" & cel.Value & " Store Ad" & "'!A1)") Then
                                    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = cel.Value & " Store Ad"
                                Else
                                    Sheets(cel.Value & " Store Ad").Cells.Clear
                                End If
                                .AutoFilter.Range.Copy
    
                                With Sheets(cel.Value & " Store Ad")
                                    .Range("A2").PasteSpecial Paste:=8
                                    .Range("A2").PasteSpecial
                                    .Range("A1").Value = "Store Ad"
                                End With
    
                                .AutoFilterMode = False
                            End If
                        End With
                    Case ws2.Name
                        With ws2
                            If Not .AutoFilterMode Then
                                .Range("A2").AutoFilter
                                .Range(("A2"), .Range("A2").End(xlDown)).AutoFilter Field:=4, Criteria1:=cel.Value & "*"
    
                                If Not Evaluate("ISREF('" & cel.Value & " Core products" & "'!A1)") Then
                                    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = cel.Value & " Core products"
                                Else
                                    Sheets(cel.Value & " Core products").Cells.Clear
                                End If
    
                                .AutoFilter.Range.Copy
    
                                With Sheets(cel.Value & " Core products")
                                    .Range("A2").PasteSpecial Paste:=8
                                    .Range("A2").PasteSpecial
                                    .Range("A1").Value = "Core products"
                                End With
    
                                .AutoFilterMode = False
                            End If
                        End With
                    Case ws3.Name
                        With ws3
                            If Not .AutoFilterMode Then
                                .Range("A2").AutoFilter
                                .Range(("A2"), .Range("A2").End(xlDown)).AutoFilter Field:=7, Criteria1:=cel.Value & "*"
    
                                If Not Evaluate("ISREF('" & cel.Value & " Email to Locals" & "'!A1)") Then
                                    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = cel.Value & " Email to Locals"
                                Else
                                    Sheets(cel.Value & " Email to Locals").Cells.Clear
                                End If
    
                                .AutoFilter.Range.Copy
    
                                With Sheets(cel.Value & " Email to Locals")
                                    .Range("A2").PasteSpecial Paste:=8
                                    .Range("A2").PasteSpecial
                                    .Range("A1").Value = "Email to Locals"
                                End With
    
                                .AutoFilterMode = False
                            End If
                        End With
    
                    End Select
                End With
            Next vWs
        Next cel
        Application.DisplayAlerts = False
        Sheets("Lists").Delete
        Application.DisplayAlerts = True
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    Last edited by jaslake; 11-20-2013 at 02:32 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Macro to search a string, then search above for another one and copy the rows between them
    By DarKDjinni in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 11-20-2012, 09:36 PM
  2. Search cell in rows and copy paste
    By nicolaforni in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-27-2011, 12:59 PM
  3. Add rows and search/copy/paste text into added rows
    By HelenW in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-14-2010, 04:06 AM
  4. Replies: 1
    Last Post: 09-07-2010, 06:23 PM
  5. Search for multiple words and copy/paste rows
    By Excelcreep in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-19-2010, 08:28 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