+ Reply to Thread
Results 1 to 6 of 6

Macro to copy specific entries and adjust value based on a table

Hybrid View

Seraph122 Macro to copy specific... 12-23-2012, 11:11 PM
jeffreybrown Re: Macro to copy specific... 12-23-2012, 11:49 PM
smuzoen Re: Macro to copy specific... 12-23-2012, 11:50 PM
Seraph122 Re: Macro to copy specific... 12-24-2012, 01:35 AM
smuzoen Re: Macro to copy specific... 12-24-2012, 05:17 AM
Seraph122 Re: Macro to copy specific... 12-24-2012, 11:03 PM
  1. #1
    Registered User
    Join Date
    10-15-2012
    Location
    Oakland, CA
    MS-Off Ver
    Excel 2010
    Posts
    43

    Macro to copy specific entries and adjust value based on a table

    Hi All,

    Happy Holidays everyone!!

    I could use some help with the following goal. In the attached file, I have an Import tab that shows 5 columns. I need a macro that only copies entries where the value in column D is equal to one of the values in the Adjustment Table tab.

    So in this case, whenever a row with a value G, H, or J in column D, the macro copies the row into the Export tab, and adjusts it based on the value next to the adjustment name. So in this case, all entries with "G" in column D will have their "E" column adjusted with an increased 2.2%, and placed into the Export tab.

    I think the example in the attachment will make more sense.

    Can anyone assist me with this?

    Thank you!
    Attached Files Attached Files

  2. #2
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Macro to copy specific entries and adjust value based on a table

    How about

    Sub MoveData()
        Dim destSht As Worksheet: Set destSht = Sheets("Export")
        Dim srcSht As Worksheet: Set srcSht = Sheets("Import")
        Dim srcLR As Long: srcLR = srcSht.Range("A" & Rows.Count).End(xlUp).Row
    
        Application.ScreenUpdating = False
        With destSht
            .Columns("A:E").Clear
        End With
        With srcSht
                .Range("F1").Value = "Hdr"
            With .Range("F2:F" & srcLR)
                .FormulaR1C1 = "=INDEX('Adjustment Table'!R4C2:R6C2,MATCH(RC[-2],'Adjustment Table'!R4C1:R6C1,0))+RC[-1]"
                .Value = .Value
                .AutoFilter Field:=1, Criteria1:=">0"
                .Offset(1).EntireRow.Copy Destination:=destSht.Range("A2")
                .AutoFilter
            End With
            .Range("F:F").ClearContents
        End With
        With destSht
            .Columns(6).Copy Destination:=.Columns(5)
            .Range("A1").Resize(, 5) = Array("Number", "Date", "Name", "Name", "Value")
            .Columns(6).Delete
            .Range("A:E").Columns.AutoFit
        End With
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
    End Sub
    HTH
    Regards, Jeff

  3. #3
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: Macro to copy specific entries and adjust value based on a table

    You could use something along these lines.
    Option Explicit
    
    Sub copyData()
    Dim wsI As Worksheet, wsAT As Worksheet, wsEx As Worksheet
    Dim matchRng As Variant
    Dim k As Long, adjV As Double
    Dim resRng As Range, rs As Range
    Set wsI = Worksheets("Import")
    Set wsAT = Worksheets("Adjustment Table")
    Set wsEx = Worksheets("Export")
    
    matchRng = Application.Index(Application.Transpose(wsAT.Range("A4:B" & wsAT.Cells(Rows.Count, "B").End(xlUp).Row)), 0, 0)
    For k = LBound(matchRng, 2) To UBound(matchRng, 2)
        Set resRng = Find_Range(matchRng(1, k), wsI.Columns("D"), xlValues, xlWhole)
        If Not resRng Is Nothing Then
            For Each rs In resRng
                wsI.Range("A" & rs.Row).Resize(, 5).Copy wsEx.Range("A" & wsEx.Cells(Rows.Count, "A").End(xlUp).Row + 1)
                adjV = wsEx.Range("E" & wsEx.Cells(Rows.Count, "E").End(xlUp).Row)
                adjV = adjV + (adjV * matchRng(2, k))
                wsEx.Range("E" & wsEx.Cells(Rows.Count, "E").End(xlUp).Row) = adjV
            Next
        End If
    Next
    End Sub
    
    Function Find_Range(Find_Item As Variant, _
        Search_Range As Range, _
        Optional LookIn As Variant, _
        Optional LookAt As Variant, _
        Optional MatchCase As Boolean) As Range
        Dim c As Range, firstAdd As String
        If IsMissing(LookIn) Then LookIn = xlValues 'xlFormulas
        If IsMissing(LookAt) Then LookAt = xlPart 'xlWhole
        If IsMissing(MatchCase) Then MatchCase = False
        With Search_Range
            Set c = .Find( _
            What:=Find_Item, _
            LookIn:=LookIn, _
            LookAt:=LookAt, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=MatchCase, _
            SearchFormat:=False)
            If Not c Is Nothing Then
                Set Find_Range = c
                firstAdd = c.Address
                Do
                    Set Find_Range = Union(Find_Range, c)
                    Set c = .FindNext(c)
                Loop While c.Address <> firstAdd
            End If
        End With
    End Function
    See attached workbook
    Attached Files Attached Files
    Hope this helps.
    Anthony
    Pack my box with five dozen liquor jugs
    PS: Remember to mark your questions as Solved once you are satisfied. Please rate the answer(s) by selecting the Star in the lower left next to the Triangle. It is appreciated?

  4. #4
    Registered User
    Join Date
    10-15-2012
    Location
    Oakland, CA
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: Macro to copy specific entries and adjust value based on a table

    Thanks for both of your responses! I have tested them and they both work great!

    I have one quick question, is there an easy way to sort the macro output in the Export tab after it has finished generating the lines? Smuzoen, I noticed your macro automatically sorts the output entries by the Name order, which is great. If the need came up for example, could I sort it first by Name and then by Store and then by Date?

    Thank you both again!

  5. #5
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: Macro to copy specific entries and adjust value based on a table

    It was only a coincidence that it sorted by Name as your workbook was sorted. I added an extra sub to sort however I was not sure which column was Store - I assumed column C was store so you will need to adjust sort macro as required. I added code to clear the output from Export sheet prior to data being copied across as well. See attached workbook V2.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    10-15-2012
    Location
    Oakland, CA
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: Macro to copy specific entries and adjust value based on a table

    This is excellent!!

    Thank you so much smuzoen!

+ 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