0

I am running into an issue with my current code - I am hoping someone can assist:

Issue: I am trying to do a Vlookup from Workbook "ABC". The issue is, I am trying to change the VLOOKUP Col_Index_Number based on a header on workbook ABC...

So for example: For MyArray "Food" I am looking to Vlookup Column_Index_Num for "Food-Mexican" Column on workbook ABC, For MyArray Appetizers, I am looking to Vlookup Column_Index-Num for "Appetizers-American"...

Additionally, the columns will not always be in the same place for each report so it has to be based on the row 1 header of ABC workbook.

Also, sometimes the Array iteration might be skipped, if say for example, "Non-AlcoholicDrinks" is not found.


Sub WIP()
    Dim wb As Workbook
    Dim wsMain As Worksheet
    Dim wsLookup As Worksheet
    Dim rng As Range
    Dim rng2 As Range
    Dim rFind1 As Range
    Dim rFind2 As Range
    Dim rFind3 As Range
    Dim MyArray As Variant
    Dim LookupHeaders As Variant
    Dim LookupHeaders2 As Variant
    Dim LR As Long
    Dim i As Long
    Dim PriceCol As Long
    Dim pricecol2 As Long
    Dim LastColumn As Long
     Dim LastColumn2 As Long
       Dim LastColumn3 As Long
     Dim LastColumn4 As Long
    Dim IndexCol As Long

     'Unformatted Price Row
  Sheets("Consolidate List").Select
    LR = Range("A" & Rows.Count).End(xlUp).Row
    Columns("H:H").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("J:N").Delete
    Columns("J:J").Select
    ActiveWindow.FreezePanes = True
    Range("H2").Select
    ActiveCell.FormulaR1C1 = "New Price"
    ActiveCell.Interior.ColorIndex = 22
         Range("H3:H" & LR).Formula = "=VLOOKUP(RC[-7],'Connect Report'!C[-7]:C[-6],2,FALSE)"
         ActiveCell.EntireColumn.Resize(Rows.Count - 2).Offset(2).Select
 Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        Range("I2").Select
    ActiveCell.FormulaR1C1 = "Difference"
    ActiveCell.Interior.ColorIndex = 22
    Range("I3:I" & LR).Formula = "=IF(OR(OR(RC[-2]="""",RC[-1]="""",RC[-1]=""x"",)),"""",RC[-1]-RC[-2])"
         ActiveCell.EntireColumn.Resize(Rows.Count - 2).Offset(2).Select
 Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Set wb = ActiveWorkbook
     Sheets("Consolidate List").Select
    Set wsMain = wb.ActiveSheet
    Set wsLookup = wb.Sheets("Connect Report")     '<-- Change to correct sheet name for the Lookup sheet
    LR = wsMain.Cells(wsMain.Rows.Count, "A").End(xlUp).Row
    MyArray = Array("US", "SPAIN", "California")
    LookupHeaders = Array("TTIER", "Time333", "Round6")
  LookupHeaders2 = Array("TELLER5", "Fly7", "Mine4")

    For i = LBound(MyArray) To UBound(MyArray)
        With wsMain.Rows(1)
            Set rFind1 = .Find(What:=MyArray(i), LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
            If Not rFind1 Is Nothing Then
                Set rng = rFind1.Offset(1).Resize(, 8)
                PriceCol = Application.Match("New Opposed Price", rng, 0)
                LastColumn = rFind1.Column + PriceCol
                If wsMain.Cells(rng.Row, LastColumn) <> "New Opposed Price" Then
                    wsMain.Columns(LastColumn).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                    wsMain.Cells(rng.Row, LastColumn).Value = "New Opposed Price"
                    wsMain.Cells(rng.Row, LastColumn).Interior.ColorIndex = 22
                    LastColumn2 = LastColumn + 1
                     wsMain.Columns(LastColumn2).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                    wsMain.Cells(rng.Row, LastColumn2).Value = "Difference"
                    wsMain.Cells(rng.Row, LastColumn2).Interior.ColorIndex = 22

                Set rFind2 = wsLookup.Rows(1).Find(LookupHeaders(i), wsLookup.Range("A1"), xlValues, xlWhole)
                If Not rFind2 Is Nothing Then
                    IndexCol = rFind2.Column
                    wsMain.Cells(rng.Row + 1, LastColumn).Resize(LR - 2).Formula = "=VLOOKUP(A" & rng.Row + 1 & ",'Connect Report'!$A:$AL," & IndexCol & ",FALSE)"

                      wsMain.Cells(rng.Row + 1, LastColumn2).Resize(LR - 2).Formula = "=IF(OR(OR(RC[-2]="""",RC[-1]="""",RC[-1]=""x"",)),"""",RC[-1]-RC[-2])"
                              Else
                    MsgBox "Excel could not find " & LookupHeaders(i) & " in the lookup table."
                End If

                Set rng2 = rFind1.Offset(1).Resize(, 8)
                pricecol2 = Application.Match("New Muted Price", rng, 0)
                LastColumn3 = rFind1.Column + pricecol2
                   If wsMain.Cells(rng.Row, LastColumn3) <> "New Muted Price" Then
                    wsMain.Columns(LastColumn3).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                    wsMain.Cells(rng2.Row, LastColumn3).Value = "New Muted Price"
                    wsMain.Cells(rng2.Row, LastColumn3).Interior.ColorIndex = 22
                    LastColumn4 = LastColumn3 + 1
                      wsMain.Columns(LastColumn4).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                    wsMain.Cells(rng2.Row, LastColumn4).Value = "Difference"
                    wsMain.Cells(rng2.Row, LastColumn4).Interior.ColorIndex = 22
                End If

                      Set rFind3 = wsLookup.Rows(1).Find(LookupHeaders2(i), wsLookup.Range("A1"), xlValues, xlWhole)
                If Not rFind3 Is Nothing Then
                    IndexCol = rFind3.Column
                    wsMain.Cells(rng2.Row + 1, LastColumn3).Resize(LR - 2).Formula = "=VLOOKUP(A" & rng2.Row + 1 & ",'Connect Report'!$A:$AL," & IndexCol & ",FALSE)"

                      wsMain.Cells(rng2.Row + 1, LastColumn4).Resize(LR - 2).Formula = "=IF(OR(OR(RC[-2]="""",RC[-1]="""",RC[-1]=""x"",)),"""",RC[-1]-RC[-2])"

                    Else
                    MsgBox "Excel could not find " & LookupHeaders2(i) & " in the lookup table."
                End If
                End If
             End If
        End With
    Next i
End Sub

Can anyone help with this? I am completely lost on how to resolve this issue. Also, I am hoping that I described the issue clearly... it is quite confusing.

Nic
  • 15
  • 5
  • you are using `xlWhole` in your `.Find`, change that to `xlPart` in order to search for partial matches so that when you search for "Food" it can find "Food-Mexican", etc – tigeravatar Jan 25 '18 at 15:34
  • You also use `MyNumber` in your VLookup formula string, but I don't see `MyNumber` being assigned a value anywhere? You also have your vlookup table pointed to a single cell `'[ABC.xlsx]Sheet1'!C1` so that needs to be updated to be the actual table to search instead of just one cell. – tigeravatar Jan 25 '18 at 15:37
  • I think there is some confusion. The array and all work good, however, I am trying to get the VLOOKUP function to do a col_Index_Num based on a SEPARATE header based on the iteration of the array. For example if the ARRAY is on "FOOD", I want the Col_Index_Num to look up a header in a different workbook of "American Food", if the ARRAY is on "Appetizers", I want the Col_Index_Num to look up a header in a different workbook of "Mexican Food", etc. – Nic Jan 25 '18 at 15:47
  • That's fine, you still need to actually assign a value to `MyNumber`, preferably by performing another `Range.Find` using `Lookat:=xlPart` and you need to change your VLookup table to be more than just a single cell. Was that code just not in your original post? Because it's plainly missing. – tigeravatar Jan 25 '18 at 15:50
  • @tigeravatar I am not sure exactly how to construct that to work within the Array, based on the array iteration and be for a Vlookup, also I am aware that the Vlookup is pointing to a single cell at the moment, it is a placeholder until I can figure out how to solve the rest of the formula.. Also I did not define MyNumber, however it would be part of the Range.Find? – Nic Jan 25 '18 at 15:54
  • Ok, I can help with that. We're still missing information though. If you were to do the VLookup manually for just one of the iterations, what would be the VLookup formula? (Please actually do this manually so you get the desired result and then post that formula here). Also, what row are the headers in within the ABC.xlsx workbook, Sheet1? – tigeravatar Jan 25 '18 at 15:57
  • @tigeravatar the vlookup formual would be "=VLOOKUP(RC[-7],'ABC'!C[-7]:C[30],1,FALSE)" with the (1) in the formula needing to be the header name on Worksheet ABC Row 1... I have merged the separate workbook to a new worksheet to make it easier. – Nic Jan 25 '18 at 16:01
  • `C[-7]:C[30]` are relative columns. Do you have the actual columns? Like `A:AL` or `C:AN`? Also, is it actually in a different workbook? It looks like it's in the same workbook, just in a different worksheet. – tigeravatar Jan 25 '18 at 16:03
  • The columns change based on the Array iteration. And I am not sure of what all of the new column rows will be. Ultimately I want all the rows that are created with a header of "Difference" to provide the same formula. And yes, I have merged the data onto the same workbook, different worksheet to make it easier. – Nic Jan 25 '18 at 16:08
  • I understand that the whole point is to change the `Col_Index_Number`, but does your actual lookup table (the search area / table_array) also change? That is almost always static and would be very strange if it wasn't. – tigeravatar Jan 25 '18 at 16:11
  • No the lookup table is static – Nic Jan 25 '18 at 16:15
  • Right, so what are the actual columns? Like `A:AL` or `C:AN`? – tigeravatar Jan 25 '18 at 16:16
  • columns on the lookup table are A:AL – Nic Jan 25 '18 at 16:18
  • I have posted two photos to the initial question to try and make it clearer, In the difference columns, I am trying to VLOOKUP based on the iteration of the array, so for "US" in the photo, I am looking to col_index_num Header "d" in the lookup table. for "Food", I am looking to col_index_num header "b" in the lookup table... hopefully that helps. – Nic Jan 25 '18 at 16:20
  • Your provided images are not representative of your code. Where are the "Price" columns? Why column "d" in the lookup table? I thought we needed to do a partial match so that if we are on iteration "US" in the array, there should be a column in the lookup table that is something like "US-Stuff"? It's really hard to get a clear picture without accurate sample representation. – tigeravatar Jan 25 '18 at 16:30
  • @tigeravatar one second, redoing images to represent code – Nic Jan 25 '18 at 16:35
  • @tigeravatar okay I have updated the photos, so as they show, the Array first looks for the header in Row 1 - "Food, "Non-Alc Drinks", Then looks to see if "Price" is listed in row 2, if it is, it puts a column "Difference" to the right, I am then looking to do a vlookup in each difference row with the col_Index_Num BASED on the array iteration, for example if Array iteration is on "Food", I am looking for col_Index_Num of "American-Food" so column C, If array iteration is on "Non-AlcDrinks" I am looking for col_index_Num of "Chinese Food" so column E – Nic Jan 25 '18 at 16:45
  • Rather than vlookup with a variable column, then converting, why not do index/match using find (for a specific header name) or select case (where, say the 4th column could be x, y, or z, and each of those has +n units for the index printout)? – Cyril Jan 25 '18 at 16:46
  • @Cyril, I am not sure how to do that, but it would have to be based on the Array iteration, would that work? – Nic Jan 25 '18 at 16:47
  • You should be able to, given For i = lbound to ubound step 1, application.index(outputarray, application.match(array(i), searcharray,0)), where the 0 in the match section specifies an exact match. – Cyril Jan 25 '18 at 16:51
  • @Cyril - I am sorry, I am completely lost, can you please explain a little bit? – Nic Jan 25 '18 at 16:52
  • @Nic As I attempted to start typing something up for you, I'm thrown on how what you're matching for the output. Specifically, you want to use a vlookup for an output, and we can do that vlookup with application.index [Application.Index(Range(Cells(2,rFind.Column), Cells(LR,rFind.Column)), Application.Match())]... saves you some steps. I am unsure what you're looking up to find the appropriate row to output from. Is that the Match("price",rng,0)? If so, then MyPrice = Application.Index(Range(Cells(2,rFind.Column), Cells(LR,rFind.Column)), Application.Match("price",rng,0)). – Cyril Jan 25 '18 at 17:18
  • @cyril, Basically for my whole code, In an array I am looking to find a header in row 1 (they are merged 7 cells each), IF that header is found, It is then looking for a header in row 2 named "Price", due to the headers in row 1 being merged, the code allows 7 cells over). IF it then finds "Price", it adds a new column to the right named "Difference". I am then trying to do a Vlookup, or index-match in EACH difference column that was created with name (on the source file) and PID (on the lookup file) being the identifiers. – Nic Jan 25 '18 at 17:22
  • @Cyril Then I am looking for the vlookup or index-match to find, based on the array iteration, so if the array is under "Food", the column header looks for a specific word on the lookup source, if array iteration is under "Non-Alc Drink", the column header looks for a different word on the lookup source. – Nic Jan 25 '18 at 17:23
  • @Nic I can follow that part, where you choose the column to work in (you have rFind for that). What row in the found column are you looking for the output? Is the specific price just in the last row, or what is choosing/designating the row? If the price is the output, and it's in a designated location, there doesn't seem to be a need for even vlookup tables, as .find gave you the column. – Cyril Jan 25 '18 at 18:08

2 Answers2

0

This is a User Defined Function I've wrote to find the range of based on column header, it uses .find method to find the target cells. Works well if you column titles are are the top of your worksheet.

I hope this solves your problem, you could just findout the target column, by using .column on the range returned.

'define a range by looking for a specific text title, and return all the cells to the lastrow of the sheet as a range
Private Function defineColRange(ByVal targetWorkSheet As Worksheet, ByVal targetValue As String, _
                                Optional ByVal visibleOnly As Boolean, Optional ByVal rtnNoTitle As Boolean, _
                                Optional ByVal searchByColumn, Optional ByVal searchBackwards) As Range
    Dim targetlastRow As Long
    Dim targetlastCol As Long
    Dim returnRange As Range
    Dim findTarget As Range

'default visible only mode off

    If IsMissing(visibleOnly) Then
         visibleOnly = False
    End If
    If IsMissing(rtnNoTitle) Then 'Don't return title cell in the range returned
        rtnNoTitle = False
    End If
    If IsMissing(searchByColumn) Then 'Search vertically by column, instead of by rows
        searchByColumn = False
    End If
    If IsMissing(searchBackwards) Then 'Search backwards by rows
        searchBackwards = False
    End If

    'test if targetWorkSheet is not empty
    If targetWorkSheet Is Nothing Then
        MsgBox ("Worksheet pass failed!"), vbExclamation
        Exit Function
    End If

    targetWorkSheet.Activate
    targetlastRow = targetWorkSheet.UsedRange.Find(What:="*", _
                    after:=Range("A1"), _
                    Lookat:=xlPart, _
                    LookIn:=xlFormulas, _
                    Searchorder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Row

    targetlastCol = targetWorkSheet.UsedRange.Find(What:="*", _
                    after:=Range("A1"), _
                    Lookat:=xlPart, _
                    LookIn:=xlFormulas, _
                    Searchorder:=xlByColumns, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Column

    'find the range

    If searchByColumn = True Then
        Set findTarget = targetWorkSheet.UsedRange.Find(What:=targetValue, after:=Cells(1, 1), _
                        LookIn:=xlFormulas, Lookat:=xlPart, Searchorder:=xlByColumns, _
                        SearchDirection:=xlNext, MatchCase:=False)
    ElseIf searchBackwards = True Then
        Set findTarget = targetWorkSheet.UsedRange.Find(What:=targetValue, after:=Cells(1, targetlastCol), _
                        LookIn:=xlFormulas, Lookat:=xlPart, Searchorder:=xlByRows, _
                        SearchDirection:=xlPrevious, MatchCase:=False)
    Else
        Set findTarget = targetWorkSheet.UsedRange.Find(What:=targetValue, after:=Cells(1, 1), _
                        LookIn:=xlFormulas, Lookat:=xlPart, Searchorder:=xlByRows, _
                        SearchDirection:=xlNext, MatchCase:=False)
    End If

    If findTarget Is Nothing Then
        Debug.Print ("Did not find columne title """ & targetValue & ""), vbExclamation
        Exit Function
    Else
        Dim tRow, tCol As Long
        tRow = findTarget.Row
        tCol = findTarget.Column

        On Error Resume Next
        If visibleOnly = False Then
            If rtnNoTitle = False Then
                Set returnRange = targetWorkSheet.Range(Cells(tRow, tCol), Cells(targetlastRow, tCol))
                Set defineColRange = returnRange
            Else
                Set returnRange = targetWorkSheet.Range(Cells(tRow + 1, tCol), Cells(targetlastRow, tCol))
                Set defineColRange = returnRange
            End If
        Else
            If rtnNoTitle = False Then
                Set returnRange = targetWorkSheet.Range(Cells(tRow, tCol), Cells(targetlastRow, tCol)).SpecialCells(xlCellTypeVisible)
                Set defineColRange = returnRange
            Else
                Set returnRange = targetWorkSheet.Range(Cells(tRow + 1, tCol), Cells(targetlastRow, tCol)).SpecialCells(xlCellTypeVisible)
                Set defineColRange = returnRange
            End If
        End If

        If Err <> 0 Then
            Debug.Print "Worksheet: " & targetWorkSheet.Name & " Column Name: " & targetValue
        End If
        On Error GoTo 0
        Err.Clear
    End If

End Function
Middle
  • 143
  • 1
  • 8
  • I am not sure where this portion of the code would go? I do not see any part about looking for the first two headers, adding a column " difference" based on "Price" and then doing a VLOOKUP with a col_Index_Num? – Nic Jan 25 '18 at 16:55
  • With my code, it does the following -- I have updated the photos, so as they show, the Array first looks for the header in Row 1 - "Food, "Non-Alc Drinks", Then looks to see if "Price" is listed in row 2, if it is, it puts a column "Difference" to the right, I am then looking to do a vlookup in each difference row with the col_Index_Num BASED on the array iteration, for example if Array iteration is on "Food", I am looking for col_Index_Num of "American-Food" so column C, If array iteration is on "Non-AlcDrinks" I am looking for col_index_Num of "Chinese Food" so column E – – Nic Jan 25 '18 at 16:57
  • @This UDF defines the entire column range UNDER the column title you want to find, provided it has data, you can then have all the options that Range object provide, including '.column' or '.row'. You can read up on Match-Index method instead of vlookup to solve your problem as well: https://www.deskbright.com/excel/using-index-match/. – Middle Jan 25 '18 at 17:04
  • @Nic nono, it is a function you can call, and you just need to stick at the end of code. Use it by calling it entering the appropriate parameters. – Middle Jan 25 '18 at 17:07
  • Im so confused :( . im still decently new to VBA.. I have added it to the end of my macro, after End Sub But I am not entirely sure how to use this to resolve my Vlookup (or Index,Match) issue. – Nic Jan 25 '18 at 17:08
  • @Nic start by reading up Index-Match method, others have mentioned this as well. You should be able better solve this problem after reading about index-match. – Middle Jan 25 '18 at 17:12
  • @Nic Index-Match would not require you to supply column number, with my UDF, you can fit in index-match easily. Though you do have to rewrite you code. If that's not an option, I will have some time later to test your code. – Middle Jan 25 '18 at 17:15
  • I have never used index-match but have heard of it. The column names on the lookup sheet are different than the column names in the regular sheet though. I can change the vlookup to index-match if that works but I am just not sure how – Nic Jan 25 '18 at 17:17
  • @Nic by using code like 'application.match' and 'application.index' , also after taking a quick look at your code, the reason why your column number isn't changing, is because like tigeravartar mentioned, your '.find' is finding you the FIRST match with cell that only has "FOOD" as a value in the WHOLE cell, which means it won't return "Chinese-Food" as a valid choice – Middle Jan 25 '18 at 17:21
  • I dont need it to find chinese food, the headers for the vlookup and the headers for the array are two separate things and they dont match. the Array portion of the code works as intended – Nic Jan 25 '18 at 17:24
0

I believe something like this should work for you. Give it a try and let me know.

Sub tgr()

    Dim wb As Workbook
    Dim wsMain As Worksheet
    Dim wsLookup As Worksheet
    Dim rng As Range
    Dim rFind1 As Range
    Dim rFind2 As Range
    Dim MyArray As Variant
    Dim LookupHeaders As Variant
    Dim LR As Long
    Dim i As Long
    Dim PriceCol As Long
    Dim LastColumn As Long

    Set wb = ActiveWorkbook
    Set wsMain = wb.ActiveSheet
    Set wsLookup = wb.Sheets("ABC")     '<-- Change to correct sheet name for the Lookup sheet
    LR = wsMain.Cells(wsMain.Rows.Count, "A").End(xlUp).Row
    MyArray = Array("TEST", "Food", "Non-AlcoholicDrinks", "Appetizers", "Alcoholic Drinks")
    LookupHeaders = Array("TestHeader", "FoodHeader", "Non-AlcoholicDrinksHeader", "AppetizersHeader", "Alcoholic DrinksHeader")

    For i = LBound(MyArray) To UBound(MyArray)
        Set rFind1 = wsMain.Rows(1).Find(What:=MyArray(i), LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
        If Not rFind1 Is Nothing Then
            Set rng = rFind1.Offset(1).Resize(, 8)
            PriceCol = Application.Match("Price", rng, 0)
            LastColumn = rFind1.Column + PriceCol
            If wsMain.Cells(rng.Row, LastColumn) <> "Difference" Then
                wsMain.Columns(LastColumn).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                wsMain.Cells(rng.Row, LastColumn).Value = "Difference"
                wsMain.Cells(rng.Row, LastColumn).Interior.ColorIndex = 22
            End If
            Set rFind2 = wsLookup.Rows(1).Find(LookupHeaders(i), wsLookup.Range("A1"), xlValues, xlPart)
            If Not rFind2 Is Nothing Then
                With wsMain.Cells(rng.Row + 1, LastColumn).Resize(LR - 2)
                    .Formula = "=VLOOKUP(A" & rng.Row + 1 & "," & wsLookup.Range("A:AL").Address(External:=True) & "," & rFind2.Column & ",FALSE)"
                    .Value = .Value 'Convert to values
                End With
            End If
        End If
    Next i

End Sub
tigeravatar
  • 26,199
  • 5
  • 30
  • 38
  • I tried this code, it creates the column however, it is not filling out the formulas in the newly created column.. I believe below Set rFind 2 is not correct? I am not sure where IndexCol is getting the number from as it needs to be a different name based on each array – Nic Jan 25 '18 at 17:14
  • @Nic It populates the formula properly for me on my test workbook, so I know it's working. Can you step through the code (using F8) and when it gets to the formula line, hover over both rng.Row, LR, and LastColumn and let me know the values? – tigeravatar Jan 25 '18 at 17:18
  • it doesnt get to the formula, as rFind2 = nothing... it looks like rfind2 is looking for the header in row 1 of the source file (which is merged), which is different than the header in row 1 of the lookup file. – Nic Jan 25 '18 at 17:27
  • @Nic, Oh, you probably need to change the array values in the code ("TEST", "Food", "Non-AlcoholicDrinks", etc). The ones shown here are just a copy from the code in your original post so they're probably sample data and not the actual values it should be looking for. – tigeravatar Jan 25 '18 at 17:28
  • that portion of the code works, and it creates a new column "difference" - its the portion rfind2 – Nic Jan 25 '18 at 17:30
  • @Nic you will probably also need to change the worksheet name that `wsLookup` is assigned to so that it is pointing to the correct lookup sheet. Right now it's just pointed to a sheet named "ABC" – tigeravatar Jan 25 '18 at 17:30
  • I have also changed that – Nic Jan 25 '18 at 17:30
  • Alright, change the `xlWhole` in the rFind2 line to be `xlPart` and remove the `& "*"` – tigeravatar Jan 25 '18 at 17:31
  • I think the issue is that my headers in my lookup sheet are not the same as my array "Test","Food", etc.) – Nic Jan 25 '18 at 17:33
  • Yes, Tigeravatar, thats it... It is because my headers in my Lookup Sheet are not the same as my headers in my source sheet... is there any way to make it so if header in source sheet is "Food", look for header "XXX" in lookup sheet? – Nic Jan 25 '18 at 17:33
  • See updated code, notice the second array named LookupHeaders. Put the matching header info in there. – tigeravatar Jan 25 '18 at 17:36
  • THAT ALMOST WORKS! One issue, IF one of the arrays aren't valid, for example "Test", the Lookup header still goes to the next variable which throws the data off.. For example in your code, "Test" is not valid, and then it goes to "Food" which still uses "TestHeader" – Nic Jan 25 '18 at 17:45
  • It shouldn't because they both go off of `i`, so if a value in `MyArray` is skipped, it should skip for the other as well. Is it possible that the partial search is finding the LookupHeader(i) text in a previous cell? Maybe with using two different arrays, we need to switch the `xlPart` back to `xlWhole` for `rFind2`? – tigeravatar Jan 25 '18 at 17:50
  • I will try a couple more times, One other issue, the vlookup is looking at offset 2, how do I change it to Column A – Nic Jan 25 '18 at 18:00
  • It's not always column A though, right? It depends on where it found the appropriate header? – tigeravatar Jan 25 '18 at 18:09
  • Thank you! I have to step away but will try shortly and let you know. Thank you very much for your help so far!!! I could not have done this without you. – Nic Jan 25 '18 at 18:15
  • Hi @Tigeravatar -- I have been testing it and it works very well! One issue that i am seeing, is that if LookupHeaders has less in the array, then MyArray, it comes up with an error "subscript out of range". I tried doing "IF Lookupheaders(i) then ---code --- but it then did a mismatch. do you know how I would fix that error? – Nic Jan 26 '18 at 14:16
  • Is there any reason you wouldn't have the same number of elements in both arrays? – tigeravatar Jan 26 '18 at 14:20
  • I do not believe so. I just tested it and was curious. Also, overall it runs quite slow if i have 15+ line items, would it be faster if I copied each "difference" vlookup row and pasted as value? Im assuming that code would just be lastcolumn.select, lastcolumn.copy, lastcolumn.paste? – Nic Jan 26 '18 at 14:32
  • If Not rFind2 Is Nothing Then With wsMain.Cells(rng.Row + 1, LastColumn).Resize(LR - 2).Formula = "=VLOOKUP(A" & rng.Row + 1 & "," & wsLookup.Range("A:AL").Address(External:=True) & "," & rFind2.Column & ",FALSE)" .Value = .Value End With – Nic Jan 26 '18 at 15:01
  • I have actually posted my updated code, as you can see I duplicated some things as I needed to add extra columns and such. Can you work with me on that one since it has changed so much? – Nic Jan 26 '18 at 15:10