VBA Vlookup on applied filtered data - excel

I would highly appreciate someone's help with my case here. I'm aiming to write a macro that uses Vlookup to find Quantity and Price from Monthly reports to the Master File (35K Rows). However, I would like to apply the filter first (e.g. the Product File & Date column) before using the Vlookup. Is my approach so far correct? I'm able to apply the Autofilter function, but I'm struggling to:
Skip the header row
Create `For Each function´ to run the vlookup on the visible cells only
Please have a look at my code, and let me know how I could move forward with it.
Sub VolumeVlookup1()
'Source Workbook & Worksheet
Dim ConsWB As Workbook
Dim ConsWS As Worksheet
Dim ReportingFile As Range
Set ConsWB = Workbooks.Open("https://Sharepoint.xlsm")
Set ConsWS = Sheets("ConsolidatedReports")
ConsWS.Select
Set ConsTable = ConsWS.ListObjects("ConsolidatedReports")
Set ReportingFile = ConsWS.Range("I1")
'Master Data File
Dim MasterFile As Workbook
Dim Oiv1 As Worksheet
Dim tbl3 As ListObject
'Dim rng As Range, Ffr As Range
Set MasterFile= Workbooks.Open("C:\Users\O\Downloads\XYZ One.xlsx")
Set Oiv1 = Sheets("Oiv One")
Set tbl3 = Oiv1.ListObjects("Table3")
'Starting Point Quantity Column in the Master File
Set rng = Oiv1.Range("K1")
Dim Lastrow As Long
'Filtering Master Data table
With tbl3.Range
.AutoFilter Field:=9, Criteria1:=xlFilterLastMonth, Operator:=xlFilterDynamic
.AutoFilter Field:=38, Criteria1:=ReportingFile
End With
Lastrow = ActiveSheet.UsedRange.Rows.Count
' Trying to assign dynamic first cell in the quantity column *Note that 11 is the Column Index number of Quantity in the Master Data*
Set Ffr = Rng.SpecialCells(xlCellTypeVisible).Cells(2, 11)
'-9 is the Product ID the value I need to look it up
Ffr.Value = WorksheetFunction.VLookup(Ffr.Offset(0, -9), ConsWS.Range("A:D"), 2, 0)
With ffr
.AutoFill Destination:=Range(Cells(ffr.Row, 11), Cells(Lastrow, 11)), Type:=xlFillDefault
End With
End Sub

Related

List items with quantity above greater or equal to 1

I have to do this excel sheet at work and I'm kindof stuck at this problem.
I need to create a list in sheet 3 with the items that have been selected in sheet 2 (valid selection is when Quantity is equal or greater then 1).
So that in the cells in sheet 3 only the items requested appear and the quantity desired.
I was going to try and use a filter function but I cannot use that because I must use Excel 2016 which does not have it.
I have attached 2 screenshots to better illustrate my problem.
Thanks in advance. (Image 1) (Image 2)
It is easier to create a macro.
ALT + F11 and copy and paste the text below in a module. You can modify the rng and other variables if you want.
Public Sub copy_quantity()
Dim ws As Worksheet
Dim ws_copy As Worksheet
Dim rng As Range
Dim lr As Long
Set ws = ThisWorkbook.Sheets("Dati Richiesti") 'The source worksheet
Set ws_copy = ThisWorkbook.Sheets("Ripilogo Richiesti") 'destination worksheet
Set rng = ws.Range("C6:C600") 'The range to check quantity
'Now loop through all quantities
For Each cell In rng
If cell > 0 Then
lr = ws_copy.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row 'Determine where to paste on the first empty row
ws.Range(cell.Offset(0, -1), cell.Offset(0, 1)).Copy Destination:=ws_copy.Range("a" & lr) ' copy paste from one sheet to the other
End If
Next ' check the following cell for the quantity
End Sub

Get distinct values from a dynamic column into another worksheet

I have a sheet named Inventory which has a list of sample numbers. This list grows automatically, so I have to include the whole column. It looks something like this:
I have another sheet named Inventory Count in which I want to count the unique sample numbers.
For this data, the inventory count should look something like this:
I tried doing this with advanced filter but it doesn't allow me to have 2 separate sheets which is crucial in my case.
Please help me with any formula or VBA code. Thanks in advance
Reference link
You can define name for the Column B in "Inventory" sheet like InventoryRecords
InventoryRecords = =OFFSET(Inventory!$B$1,,,COUNTA(Inventory!$B:$B),1)
In "inventory Count" Sheet, enter formula in A2
=IFERROR(INDEX(InventoryRecords,MATCH(0,INDEX(COUNTIF($A$1:A1,InventoryRecords),0,0),0)),"")
Copy it down until you get blanks and additional say 100 rows. I would copy for number of rows equivalent to the number of relevant inventory items in the inventory master.
In column B enter countif formula.
Ron Rosenfeld's suggestion in comments to the question to select unique items using advance filter, you can also record a macro. It is more efficient than the formula above which will keep calculating every time.
In "Inventory" Sheet
Private Sub Worksheet_Change(ByVal Target As Range)
Application.DisplayAlerts = False
If Not Intersect(Target, Range("B:B")) Is Nothing Then
Dim SourceSh As Worksheet, DestSh As Worksheet
Set SourceSh = Worksheets("Inventory"): Set DestSh = Worksheets("Inventory Count")
Dim FilterRng As Range, UniqueRng As Range, DestRng As Range, Cl As Range
Set FilterRng = SourceSh.Range("B1:B" & Range("B" & SourceSh.Rows.Count).End(xlUp).Row)
FilterRng.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Set UniqueRng = FilterRng.SpecialCells(xlCellTypeVisible)
Set DestRng = DestSh.Range("A1")
UniqueRng.Copy DestRng
Application.CutCopyMode = False
Set DestRng = DestSh.Range("A2:A" & UniqueRng.Cells.Count)
For Each Cl In DestRng
Cl.Offset(0, 1) = WorksheetFunction.CountIf(FilterRng, Cl)
Next
FilterRng.AdvancedFilter Action:=xlFilterInPlace, Unique:=False
End If
Application.DisplayAlerts = True
End Sub

How to copy and paste values in a row based on table input and paste them into a dynamic Worksheet name

I have a workbook with two sheets. The "Companies" Worksheet has a dynamic rows and with set columns A - J.
In Worksheet "Table - Summary" I have a summary of the unique company names and I have used a code to get the unique names from column b in the "Companies" Worksheet. In the "Table Summary" sheet is where people get assign to the unique companies and sheets are distributed according to which companies an individual has been assign to. Which the name of the individual is entered in column 3 in the "Table - Summary" Sheet.
I have some code where it creates a worksheet according to what was enter in the "Table - Summary" sheet in cells(LastRow, 3). There are more than 10 individuals that are assign to several companies which varies depending what name the assigner inputs in column C. Please see picture. I don't want to create duplicate worksheets for each assignee. I did a google search for suggestions, such as a function were it checks if the worksheet exists but had no clue what it was doing. If I could get assistance with this too. Please and thank you.
How can I tell VBA to check through columns b in the "Table - Summary" sheet to copy and paste the rows that have the customer names in column b of the "companies" sheet. And place it into the corresponding worksheet of the assignee.
I'm very new to VBA. If I was unclear. P Lease let me know
Sub GetAssignedCompanies()
Dim wbMaster As Workbook
Dim shI As Worksheet
Dim shS As Worksheet
Set wbMaster = Workbooks("Workbook1.xlsx")
Set shI = wbMaster.Worksheets("Companies")
Set shS = wbMaster.Worksheets("Table - Summary")
Dim LastRow As Integer
Dim EndRow As Integer
Dim aName As String
LastRow = 4
EndRow = 2
While Len(shS.Cells(LastRow, 2).Value) > 0
aName = shS.Cells(LastRow, 3).Value
If Not aName = vbNullString Then
Sheets.Add(After:=Sheets(Sheets.count)).Name = aName
End If
LastRow = LastRow + 1
Wend
End Sub
This is an example; You need to create the new worksheets for each employee before you Run this macro; It will write the company names from "Table - Summary" column 1, into column 1 of the employee's worksheet. Change the names of your employee worksheets as needed.
'You need to create the individual worksheet before running this macro.
Dim nameShtArr, i As Long, shS As Worksheet, shI As Worksheet
Set shI = ThisWorkbook.Worksheets("Companies")
Set shS = ThisWorkbook.Worksheets("Summary")
nameShtArr = Array("Tom", "Bob", "Joe")
For i = LBound(nameShtArr) To UBound(nameShtArr)
With shS.Range("C2:C" & Cells(Rows.Count, "C").End(xlUp).Row)
.AutoFilter Field:=1, Criteria1:=nameShtArr(i)
End With
'Place the company names assigned to the employee in column A.
shS.Range("B2:B20").SpecialCells(xlCellTypeVisible).Copy Worksheets(nameShtArr(i)).Range("A1")
'This next section will loop through each company name in the current worksheet and find it in ColB,
'if it finds the company name it will copy the data in the row to ColJ and paste it into the current worksheet.
Dim lRow As Long
lRow = Worksheets(nameShtArr(i)).Range("A" & Rows.Count).End(xlUp).Row
For Each Cel In Sheets(nameShtArr(i)).Range("A1:A" & lRow)
Dim fndCel As Range
Set fndCel = shI.Range("B:B").Find(Cel.Value)
If Not fndCel Is Nothing Then
fndCel.Offset(, 1).Resize(, 10).Copy Cel.Offset(, 1)
End If
Next Cel
Next i
shS.Cells.AutoFilter

Use a range of cell values for multiple worksheet names

I have a range of cell numbers that I need for multiple worksheet names.
I create multiple worksheets based on the number of rows.
Sub Copier()
Dim x As Integer
x = InputBox("Enter number of times to copy worksheet")
For numtimes = 1 To x
ActiveWorkbook.Sheets("OMRS 207").Copy _
After:=ActiveWorkbook.Sheets("OMRS 207")
Next
End Sub
That grabs only one name, OMRS 207.
I want to generate these worksheets using the entire range of cells in the original worksheet.
Try below code.
Dim data As Worksheet
Dim rng As Range
Set data = ThisWorkbook.Sheets("Sheet1")
Set rng = data.Range("A2")
Do While rng <> ""
ThisWorkbook.Worksheets.Add
ActiveSheet.Name = rng.Value
Set rng = rng.Offset(1, 0)
Loop
I assumed that your data starts from 2nd row in Sheet1 and you want the sheet name as per values in Column A.
If you want row number as sheet name for newly added sheet just use rng.row while assigning name to sheet.

Sheet generation in Excel

In Sheet1, I have rows of data points for several different measures (columns). I manually created a second sheet which performs some analysis on the first column of data. How can I create a macro or VBA code such that a new sheet is created for each of the columns in Sheet1 and the exact same analysis is performed as the one I created manually? Thanks.
Create a "template" analysis sheet with a place to paste a column of data, then make copies of that template and copy-paste in the relevant column from Sheet1.
Eg:
Sub Tester()
Dim rngData As Range, col As Range, colNum As Integer
Dim shtTemplate As Worksheet, shtData As Worksheet
Set shtData = Sheets("Sheet1")
Set shtTemplate = Sheets("Template")
Set rngData = shtData.Range("A1").CurrentRegion
colNum = 0
For Each col In rngData.Columns
colNum = colNum + 1
shtTemplate.Copy before:=shtTemplate
With Sheets(shtTemplate.Index - 1)
.Name = "Column " & colNum
col.Copy .Range("A1")
End With
Next col
End Sub

Resources