+ Reply to Thread
Results 1 to 2 of 2

hello

  1. #1
    Registered User
    Join Date
    08-27-2020
    Location
    Texas
    MS-Off Ver
    365
    Posts
    3

    hello

    Hoping someone can help. I have a workbook with two sheets. The second sheet (sheet2) is my source sheet which is updated daily. In sheet1 I have Name in col A and type in col B. there are duplicate names in the source sheet but only one type per name. I need to be able to search for the name (column D in source) and match it with the type (column L in the source) to find the output for column C (column E in the source). I will then need to reference columns A, B and C to find in my source sheet, again, multiples for the name but when all three are matched the other information is accurate from source. I currently have XLOOKUP function doing all of this but having to update the source sheet daily (over 14000 lines) it takes quite awhile for the threads to calculate. I want to move all these functions to a macro module. I have one written that will search the name in column A and return information for column C but can’t figure out how to match both A and B so C is accurate and then of course use A, B and C to match other information. Please take a look at the code written and hopefully someone can help out. Thanks,

    Sub macroLookup()

    ' Disable these Excel properties whilst macro runs
    With Application
    .Calculation = xlCalculationManual
    .EnableEvents = False
    .ScreenUpdating = False
    End With

    ' Declare object variables for macro
    Dim lastRowIn, lastRowOut, i As Long
    Dim lookFor, j, inArray, outArray, findArray As Variant
    Dim inWks, outWks As Worksheet

    ' Set certain object variables
    Set inWks = ThisWorkbook.Sheets(2) ' source input sheet with data
    Set outWks = ThisWorkbook.Sheets(1) ' output sheet with specific values to find

    ' Find the last rows in the source & output fields of company names
    lastRowIn = inWks.Cells(Rows.Count, "D").End(xlUp).Row
    lastRowOut = outWks.Cells(Rows.Count, "A").End(xlUp).Row

    ' Record the input array of data from D1
    inArray = Range(inWks.Cells(1, 4), inWks.Cells(lastRowIn, 5))

    ' Load the find array from the output column in col A in Sheet2
    findArray = Range(outWks.Cells(1, 1), outWks.Cells(lastRowOut, 1))

    ' Define the output array in col C in Sheet2
    outArray = Range(outWks.Cells(1, 3), outWks.Cells(lastRowOut, 3))

    ' Error handler
    On Error Resume Next

    ' First loop through the output names in col A, sheet1
    For i = 2 To lastRowOut

    ' The second loop through the input names in col D, sheet2
    For j = 2 To lastRowIn

    lookFor = findArray(i, 1)

    If inArray(j, 1) = lookFor Then
    outArray(i, 1) = inArray(j, 2)
    Exit For
    End If

    ' move to next value in col D, sheet2
    Next j

    ' move to next value in col A, sheet1
    Next i

    ' write out the output array values in col C, sheet1
    Range(outWks.Cells(1, 3), outWks.Cells(lastRowOut, 3)) = outArray

    ' Re-enable Excel properties as macro is complete & finished running
    With Application
    .Calculation = xlCalculationAutomatic
    .EnableEvents = True
    .ScreenUpdating = True
    End With

    End Sub

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166
    Hello txsooner,

    Welcome to Excelforum. Be a part of large Excel community. Enjoy Learning.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ 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