VBA to Vlookup Concatenation and Paste as Values

nirvehex

Well-known Member
Joined
Jul 27, 2011
Messages
505
Office Version
  1. 365
Platform
  1. Windows
I have a Table, called "Table2" on "Sheet1". I also have a PivotTable, called "PivotTable2" on "Pivot" tab.

Headers that will be important in Table2 are "Location" and "IDNum" and "Service" and "Size".

I'm trying to write some VBA that does this:

1. Filters "Service" column for values of "CS", "DS", "LR", "M", "R", "RC". Sometimes all of these will be present, sometimes only some of them, so it shouldn't error out if it doesn't find some of these values, should just move on to looking for the next value.

2. In the "Size" column for only these filtered rows, Vlookup the concatenation of the corresponding values on same row in "Location"-"IDNum" against "PivotTable2" with the TableArray being columns "Y:AB" on the "Pivot" tab, returning the 4th column,"AB".

3. Paste these Vlookups as Values.

I feel like this should be easy enough, but I can't quite get it with the Macro recorder. Any ideas?

Thanks!
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Code:
Option Explicit

Sub FilterIT()
    Dim rngCell As Range
    Dim sKey As String
    With ActiveSheet.ListObjects("Table2")
        .Range.AutoFilter Field:=3, Criteria1:= _
            Array("CS", "DS", "LR", "M", "R", "RC"), Operator:=xlFilterValues
        If Application.WorksheetFunction.Subtotal(3, .Range.Columns(3)) > 1 Then
            'There are some filtered rows visible
            For Each rngCell In .DataBodyRange.Columns(1).SpecialCells(xlCellTypeVisible).Cells
                sKey = rngCell.Value & "-" & rngCell.Offset(0, 1).Value
                rngCell.Offset(0, 3).Value = _
                    Application.WorksheetFunction.VLookup(sKey, Sheets("Pivot").Range("Y:AB"), 4, False)
            Next
            If .Range.AutoFilter Then .Range.AutoFilter
        End If
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,253
Members
452,900
Latest member
LisaGo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top