Results 1 to 6 of 6

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

Threaded View

  1. #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?

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