+ Reply to Thread
Results 1 to 3 of 3

Tweak my macro, is there a way to have it move only filtered items?

Hybrid View

  1. #1
    Registered User
    Join Date
    07-01-2010
    Location
    Sacramento, CA
    MS-Off Ver
    O365
    Posts
    70

    Tweak my macro, is there a way to have it move only filtered items?

    Hey all,

    So I have this macro that moves data from one column to the top of another and removes data from the original. My scope has changed a bit and I was wondering if there was a way to tweak it so that it would only move filtered or selected items.

    For example, my spreadsheet is roughly 1000 rows. I get comments from another team, but of the 100 or so comments that I need to move weekly 80 of them offer no real value so it's strictly me moving them. What I would like to do is filter for those 80 and move them leaving the 20 or so that I actually need to pay attention to.

    Here's the code I am currently using, it works perfectly, just wondered if it could be better.

    I am running Office 2007 and using xlsx files if that makes a difference.

    Sub aComments2012()
    '
    ' aComments2012 Macro
        Dim cell As Range
        Dim s As String
        Dim r As Range
        Dim sFrom As String
        Dim sTo As String
        Dim lFrom As Long
        Dim lOffset As Long
        Dim rRange As Range
        Dim rTop As Range
        Dim rBottom As Range
        Dim sh As Worksheet
    
        sFrom = "AM"
        sTo = "AK"
    
        On Error GoTo EF
        Application.EnableEvents = False
    
        lFrom = Range(sFrom & 1).Column
        lOffset = Range(sTo & 1).Column - lFrom
    
        Set sh = ActiveSheet
        With sh
            Set rTop = .Cells(2, lFrom)
            Set rBottom = rTop.Offset(.UsedRange.Rows.Count - 1)
            Set rRange = Range(rTop, rBottom)
        End With
    
        For Each cell In rRange
            With cell
                Set r = .Offset(0, lOffset)
                s = r.Value
                If InStr(s, .Value) = 0 Then
                    If (s <> "") Then
            s = .Value & vbLf & s
            Else
            s = .Value
            End If
                    With r
                        .Value = s
                        .VerticalAlignment = xlTop
                    End With
                    .Value = ""        'remove this line if you want column(sFrom) to remain as is.
                End If
            End With
        Next cell
    
    EF:
        Application.EnableEvents = True
    End Sub

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,920

    Re: Tweak my macro, is there a way to have it move only filtered items?

    Well, since your posted code does not autofilter, you might do something like this:
        Selection.AutoFilter
        ActiveSheet.Range("$A$1:$J$631").AutoFilter Field:=1, Criteria1:="Cherry"
        ActiveSheet.Range("$A$1:$J$631").AutoFilter Field:=2, Criteria1:="19th"
        ActiveSheet.Cells.SpecialCells(xlCellTypeVisible).Copy Destination:=Sheets("sheet1").Range("a1")
    ,etc.
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    07-01-2010
    Location
    Sacramento, CA
    MS-Off Ver
    O365
    Posts
    70

    Re: Tweak my macro, is there a way to have it move only filtered items?

    [QUOTE=protonLeah;2731909]Well, since your posted code does not autofilter, you might do something like this:

    Thanks Ben, I have not forgotten about this, just been too busy to test it.

    Would this code go in front of what I am already using?

+ 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