+ Reply to Thread
Results 1 to 3 of 3

Coding a Vlookup Loop

  1. #1
    Registered User
    Join Date
    06-09-2014
    Posts
    39

    Coding a Vlookup Loop

    I have a VBA program I am working on, which so far involves 2 steps.

    Step 1:

    Takes from the tab "Compiler" all the elements in 3 adjacent columns and arranges the unique combinations that occur across the rows of these columns in the first three columns of the "Annual Summary" tab.

    Ex/
    In the compiler tab the three columns look like:
    2013 Montreal OCC
    2013 Montreal OCC
    2013 Sarnia OSH
    2012 Montreal OCC

    The code will output in the annual summary tab:
    2012 Montreal OCC
    2013 Montreal OCC
    2013 Sarnia OSH

    This was working fine, and then I tried to add the next step...

    Step 2:

    In the annual summary tab, I then want to perform a vlookup off of the third column (OCC, OSH). The range I want the vlookup to be performed using is in "conversion units" tab. I want to have a loop that checks for the third column (OCC, OSH) cells to be full and then performs the vlookup returning the desired value to the adjacent cell in the fourth column; if they are not full I want the execution to stop.

    I wrote a code for this, but two things have happened that I wish to rectify:

    1) My original list of unique combinations in the first three columns repeats itself once (so I have two times the number of entries I want); and

    2) I have a #N/A error filling the fourth column.

    I have attached a sample version of the file so you can see the layout, but I had to remove the VBA code due to size. I have posted the code I current have below for your review. Note that the actual cell references are slightly different than the sample file I have provided. I don't think this should matter though.

    Any help would be hugely appreciated.

    Sub AnnualSummary()
    'Column titles
    Worksheets("Annual Summary").Activate
    Range("A1").Value = "Annum"
    Range("B1").Value = "Refinery"
    Range("C1").Value = "Crudes"
    Range("D1").Value = "Sample Site"
    Range("E1").Value = "Quantity (bbl)"
    Range("F1").Value = "Average Price Per Bbl"
    Range("G1").Value = "Average API"
    Range("H1").Value = "Average % wt. Sulphur"
    'Column titles font setup
    Range("A1:H1").Font.Bold = True
    Range("A1:H1").Font.Underline = True
    Range("A1:H1").HorizontalAlignment = xlCenter
    'Naming ranges
    Workbooks("Co-op Compiler.xlsm").Worksheets("Compiler").Range("B2:B1436").Name = "Year"
    Workbooks("Co-op Compiler.xlsm").Worksheets("Compiler").Range("C2:C1436").Name = "Location"
    Workbooks("Co-op Compiler.xlsm").Worksheets("Compiler").Range("E2:E1436").Name = "Crude"

    'Identifying and placing unique combinations of named ranges
    Dim sh As Worksheet, target_sh As Worksheet
    Dim lrow As Long, data, result, i As Long, n As Long, j As Long, mystr As String

    Set sh = Sheets("Compiler")

    lrow = sh.Cells(Rows.Count, 2).End(xlUp).Row
    If lrow = 1 Then Exit Sub

    Set target_sh = Sheets("Annual Summary")

    data = sh.Range("r1:t" & lrow)

    ReDim result(1 To lrow, 1 To 3)

    For i = 2 To lrow
    If InStr(mystr, "|" & data(i, 1) & data(i, 2) & data(i, 3) & "|") = 0 Then
    mystr = mystr & "|" & data(i, 1) & data(i, 2) & data(i, 3) & "|"
    j = j + 1
    For n = 1 To 3
    result(j, n) = data(i, n)
    Next
    End If
    Next

    Application.ScreenUpdating = 0

    target_sh.Cells(Rows.Count, 1).End(xlUp).Offset(1).Resize(j, 3) = result
    target_sh.UsedRange.EntireColumn.AutoFit

    Application.ScreenUpdating = 1

    'Vlookup crude source off of crude type
    Workbooks("Co-op Compiler.xlsm").Worksheets("Conversion Units").Range("D2:E45").Name = "Sources"
    Dim CrudeType As Range
    Dim Source As Variant
    Set CrudeType = Workbooks("Co-op Compiler.xlsm").Worksheets("Annual Summary").Range("c:c")

    Sheets("Annual Summary").Activate

    For Each cell In CrudeType
    If cell.Value <> "" Then
    cell.Offset(0, 1).Value = Source
    Source = WorksheetFunction.VLookup(CrudeType, Range("Sources"), 2, False)
    End If
    Next
    End Sub
    Attached Files Attached Files

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

    Re: Coding a Vlookup Loop

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    06-09-2014
    Posts
    39

    Re: Coding a Vlookup Loop

    Sorry, I am new to this forum. And by forum I also mean VBA in general.

    With the code below (modified slightly from above) I now don't have the repeated values from the indexing in step one. However, my vlookup gives me a blank cell in D2 and thereafter returns the same result for each cell in the column rather than the correctly matched result.

    What it looks like:

    OCC blank
    OSH Sadland
    OCC Sadland

    What it should look like:

    OCC Sadland
    OSH Happyland
    OCC Sadland

    Please Login or Register  to view this content.
    Last edited by bakeraj256; 06-13-2014 at 04:43 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Problem with loop coding and formatting providing a #N/A error
    By Sue991 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-24-2012, 06:43 AM
  2. VLOOKUP VBA Coding Help
    By spyne1000 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-15-2012, 03:20 PM
  3. (Solve) How to add second loop in VBA coding
    By SubwAy in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-04-2011, 03:30 PM
  4. Help in VBA Coding (DO WHILE / LOOP UNTIL) FUNCTION
    By gvreddyhr in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-29-2011, 03:05 AM
  5. [SOLVED] Cannot find inefficient coding loop
    By Trent in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-23-2005, 10:05 AM

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