+ Reply to Thread
Results 1 to 6 of 6

Analyze Within An Array and Pass Back Once vs. Pass at each applicable Instance

Hybrid View

AlvaroSiza Analyze Within An Array and... 05-09-2012, 04:29 PM
AlvaroSiza Re: Analyze Within An Array... 05-11-2012, 02:25 PM
AlvaroSiza Re: Analyze Within An Array... 05-11-2012, 02:26 PM
JosephP Re: Analyze Within An Array... 05-11-2012, 03:21 PM
AlvaroSiza Re: Analyze Within An Array... 05-11-2012, 04:45 PM
tigeravatar Re: Analyze Within An Array... 05-11-2012, 05:25 PM
  1. #1
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Analyze Within An Array and Pass Back Once vs. Pass at each applicable Instance

    A previous poster posed a problem outlined below (and demanded a programmatic approach, despite multiple attempts to 'nudge' a formulaic approach).

    I need a VBA based macro to find all matching jobs which are in Sheet1 column A, in Sheet2 column A and return the corresponding value in column B in Sheet2 to Column B, Sheet 1.
    I originally solved his the query (under the disclaimer that an array approach would be more efficient) with a 2x For Each Next; Offset syntax.

    I have since reapproached the issue for my own personal development via array. As it is widely held that the most efficient code touches the worksheet the least amount of times, my question is this.

    How would my code below (which works) be modified to store all instances of TRUE within the
    If varSource(i) = varTarget(j, 1) Then
    line within the array and pass those back to the worksheet in one fell swoop.

    Full code:
    Option Explicit
    
    Sub DoItArray()
    
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim rngSource As Range
    Dim rngTarget As Range
    Dim varSource() As Variant
    Dim varTarget() As Variant
    Dim i As Integer
    Dim j As Integer
    
    calcMode = Application.Calculation
    Set ws1 = ActiveWorkbook.Sheets("Sheet1")
    Set ws2 = ActiveWorkbook.Sheets("Sheet2")
    
       With Application
          .ScreenUpdating = False
          .EnableEvents = False
          .Calculation = xlCalculationManual
       End With
    
          With ws1
             Set rngSource = .Range("A2", .Range("A2").End(xlDown))
          End With
    
          With ws2
             Set rngTarget = .Range("A2", .Range("B2").End(xlDown))
          End With
    
          varSource() = WorksheetFunction.Transpose(rngSource.Value)
          varTarget() = rngTarget.Value
    
          For i = LBound(varSource) To UBound(varSource)
             For j = LBound(varTarget, 1) To UBound(varTarget, 1)
                If varSource(i) = varTarget(j, 1) Then
                   ws1.Cells(i, 2).Value = varTarget(j, 2)
                   Exit For
                End If
             Next j
          Next i
          
       With Application
          .ScreenUpdating = True
          .EnableEvents = True
          .Calculation = xlCalculationAutomatic
       End With
    
    End Sub
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  2. #2
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Analyze Within An Array and Pass Back Once vs. Pass at each applicable Instance

    Friday bump

  3. #3
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Analyze Within An Array and Pass Back Once vs. Pass at each applicable Instance

    Friday-Bump

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Analyze Within An Array and Pass Back Once vs. Pass at each applicable Instance

    you could just use another array, dimensioned to match the row count and populate that with the values in the loop, then write back to the sheet at the end.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  5. #5
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Analyze Within An Array and Pass Back Once vs. Pass at each applicable Instance

    Any chance you could show me how that might possibly look?

  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: Analyze Within An Array and Pass Back Once vs. Pass at each applicable Instance

    AlvaroSiza,

    This is how I would do it. It only uses a single array, and uses the .Find method to look for matches and then loads it into the array. It outputs the array at the end:
    Sub DoItArray()
        
        Dim CalcMode As Long
        Dim rngFound As Range
        Dim rngList As Range
        Dim arrList As Variant
        Dim i As Long
        
        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .EnableEvents = False
            .ScreenUpdating = False
        End With
        
        'This is the range in Sheet1!A:B
        'We are going to look for matches in Sheet2!A:A for each cell in Sheet1!A:A
        'We are getting the B column so that it is included in the array, as column B will contain the results
        Set rngList = Intersect(Sheets("Sheet1").UsedRange, Sheets("Sheet1").Range("A:B"))
        arrList = rngList.Value2
        
        For i = 1 To UBound(arrList, 1)
    	'Perform a .Find for the column A item in the array
            Set rngFound = Sheets("Sheet2").Columns("A").Find(arrList(i, 1), , , xlWhole)
            If Not rngFound Is Nothing Then
    	    'Found a match, load the result into column B in the array
                arrList(i, 2) = rngFound.Offset(, 1).Value2
                Set rngFound = Nothing
            End If
        Next i
        
        'Output results
        rngList.Value = arrList
        
        With Application
            .Calculation = CalcMode
            .EnableEvents = True
            .ScreenUpdating = True
        End With
        
    End Sub
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

+ 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