I'm having the same kind of question as in this link -
Get AutoFilter sort criteria and apply on second sheet
I've gone thru the link but not able to get the required output.
I've the filtered criteria in Sheet1 (which we can change as required) on one of the column values (eg: col 10) and now based on what ever the data in column 10 which are shown based on the filter criteria, I want to filter on sheet2 with the data in sheet 1.
I have seen that many of them using with static values in ARRAY as shown but how can I autofilter dynamically changing values in the sheet1 and filtering in Sheet2. Please advise
.AutoFilter Field:=10, Criteria1:=Array("value1", "value2"), Operator:=xlFilterValues
What if you just define the array in VBA?
Dim CritArray(2) as String
CritArray(0) = Cells(1,1).Value
CritArray(1) = Cells(2,1).Value
Then just editing your line of code:
.AutoFilter Field:=10, Criteria1:=Array(CritArray(0),CritArray(1)), Operator:=xlFilterValues
I dont know how many criteria you have (or their location), but you can add/edit as such. I based this off the fact you only have 2 criteria, but it can be enlarged of course.
I think you want something like this:
Sub tgr()
Dim wsData As Worksheet
Dim wsCriteria As Worksheet
Dim arrCriteria As Variant
Set wsData = Sheets("Sheet2")
Set wsCriteria = Sheets("Sheet1")
arrCriteria = Application.Transpose(wsCriteria.Range("J4", wsCriteria.Range("J4").End(xlDown)).Value)
With wsData.UsedRange
.AutoFilter 10, arrCriteria, xlFilterValues
End With
Set wsData = Nothing
Set wsCriteria = Nothing
If IsArray(arrCriteria) Then Erase arrCriteria
End Sub
Related
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
Trying to apply data to a combobox, which works out great, except it also include filtered values.
i filter on field 1, filter by a number, there are several empty cells in this row,
those with empty cells in field1 i dont want to see this time.
I pupulate the databodyrange value from column 13 into the combobox list, however even when filtered correctly
it also adds the rows i filtered away.
code..
Private Sub UserFrom_Initialize()
Dim db As ListObject
Set db = Worksheets("baseOfData").ListObjects("database")
db.Range.AutoFilter Field:=1, Criteria1:="<>"
Me.cmbTasks.List = db.ListColumns(13).DataBodyRange.Value
End Sub
I can solve it by running a for loop, and checking every cell before adding it
but that would kinda defeat the purpose of doing it all with 2 lines of code.
any suggestions
however even when filtered correctly it also adds the rows i filtered away.
Me.cmbTasks.List = db.ListColumns(13).DataBodyRange.Value
That is because you are incorrectly doing it. You are referring to complete column and not the filtered range. Try this
Dim db As ListObject
Set db = Worksheets("baseOfData").ListObjects("database")
db.Range.AutoFilter Field:=1, Criteria1:="<>"
Me.cmbTasks.List = db.DataBodyRange.Columns(13).SpecialCells(xlCellTypeVisible).Value
The next problem that you may face will be that it will show values from the first Area only if there are multiple areas.
To handle this, try
Dim db As ListObject
Dim aCell As Range, rngArea As Range
Set db = Worksheets("baseOfData").ListObjects("database")
db.Range.AutoFilter Field:=1, Criteria1:="<>"
'~~> Loop through each area
For Each rngArea In db.DataBodyRange.Columns(13).SpecialCells(xlCellTypeVisible).Areas
'~~> Loop though each cell in the area
For Each aCell In rngArea
cmbTasks.AddItem aCell.Value
Next aCell
Next rngArea
Why not a little simpler (if iteration is an option):
Dim db As ListObject, cel As Range
Set db = Worksheets("baseOfData").ListObjects("database")
For Each cel In db.DataBodyRange.Columns(13).Cells
If cel.EntireRow.Hidden <> True Then
cmbTasks.AddItem cel.value
End If
Next
Would you like to do some modifications on the table, according to selected combo value? If yes, the real row number associated to each combo value must be memorized. In a hidden column of the combo, for instance (using cel.Row).
Excel - VBA
I want to count how many rows there are after filtering the table.
How do I do this?
I have tried rows_count = Range("AX:AX").SpecialCells(xlCellTypeVisible).Count
but that gives me full number of rows there are in Excel 2010.
Once you've applied your filter, just use something like this:
rows_count = Worksheets("Sheet1").AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
For example, I filled A2:C20 with =RAND() and used the following code:
Sub filter()
Dim sht As Worksheet
Dim rng As Range
Set sht = ThisWorkbook.Worksheets("Sheet1")
Set rng = sht.Range("A1:C20")
sht.AutoFilterMode = False
rng.AutoFilter Field:=1, Criteria1:="<0.5"
MsgBox sht.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
End Sub
This is due to that you're referring to the entire column instead of just your table. Try with
rows_count = Range(cells(1,"AX"), cells(cells(rows.count,"AX").end(xlup).row,"AX")).SpecialCells(xlCellTypeVisible).Count
It would be better to declare which sheet you're referrring to so use
With Sheets("Sheet1")
rows_count = Range(.Cells(1, "AX"), .Cells(.Cells(.Rows.Count, "AX").End(xlUp).Row, "AX")).SpecialCells(xlCellTypeVisible).Count
End With
Where Sheet1 is the name of your sheet you're referring to.
Or if you're using a table object you can get your answer with
With Sheets("Sheet1").ListObjects(1)
rows_count = .ListColumns(Columns("AX").Column).DataBodyRange.SpecialCells(xlCellTypeVisible).Count
End With
I have put together a calendar (see image) in excel and one of the features I am trying to work out is how to extract the tasks assigned that day and copy these tasks onto another worksheet.
I have updated the code to the following
Sub OtherTask()
Dim DRng As Range
ActiveSheet.Range("g2:ah2").Find(Date).Select
ActiveCell.Resize(5).Offset(4).Select
Selection.AutoFilter Field:=1, Criteria1:="1", Operator:=xlFilterValues
'Selection.Copy
'ActiveSheet.Range("r12").PasteSpecial xlPasteValues
'Application.CutCopyMode = False
If ActiveSheet.AutoFilterMode = "True" Then
ActiveSheet.AutoFilterMode = "False"
End If
End Sub
I have declared DRng as the Range but I cannot figure out how to set it so that DRng represents the following
ActiveSheet.Range("g2:ah2").Find(Date).Select
ActiveCell.Resize(5).Offset(4).Select
Selection.AutoFilter Field:=1, Criteria1:="1", Operator:=xlFilterValues
The end goal is to have the excel sheet recognise the that day's date - accomplished with the following
ActiveSheet.Range("g2:ah2").Find(Date).Select
Once the date is established look down the column and filter any cells that have the # "1" in the cell - accomplished with the following
ActiveCell.Resize(5).Offset(4).Select
Selection.AutoFilter Field:=1, Criteria1:="1", Operator:=xlFilterValues
Once this has occurred I would ideally like excel to then go back towards Column B (I've been trying this will off set and a negative number - no luck) and then copy what is in Column B corresponding to the Row where a "1" is present. Looking at the image an example would be on February 19th I would like excel to be able to find activity 1A (which is in Cell B7) and copy this cell.
My thinking was that once I was able to locate the day and the cells with a "1" in them I would be able to Set this as the Range DRng and then use DRng to offset over to column B and grab the appropriate cells. Perhaps (more than likely) I am thinking about this the wrong way as the goal cannot be accomplished with what i have here. I've been toying with an if statement between With/End With but no luck. Any ideas/direction is greatly appreciated. Thanks
Hmm I'm not sure if I interpret your question correctly since it's a little vague. Do you want to go down the column and grab the corresponding Activity and put them in a new sheet? If so, try this
Sub TodaysDate()
Dim DRng As Range
Dim TASKng As Range
Dim newSh As Worksheet
'set the column to loop
col = Range("g2:ah2").Find(Date).column
'create new worksheet
Set newSH = thisworkbook.worksheets.add
tarRow = 1 'set which row to start writing data
For i = 6 to 36 'set which rows to loop
If activesheet.cells(i,col).value = 1 then
newSH.cells(tarRow,1) = activesheet.cells(i,1)
tarRow = tarRow +1
End if
Next i
Could anyone give me some insight on how to Filter/Delete Blanks using VBA code? For some reason when I record a Macro to do this it is not allowing some of my custom functions built using VBA to hold their values. Thanks.
The below code will delete out rows that have a blank in a selected column. The code below assumes the second column in your data is being tested for blanks. Let us know if you need additional assistance.
Sub DeleteBlanks()
Dim rDataToProcess As Range
Set rDataToProcess = Sheet1.Range("A1").CurrentRegion
'Field in the below method refers to the column that is being filtered, so the second colum
rDataToProcess.AutoFilter field:=2, Criteria1:=""
rDataToProcess.Offset(1).Resize(rDataToProcess.Rows.Count).EntireRow.Delete
Sheet1.AutoFilterMode = False
End Sub
An alternative to delete cells that are blank is to set a range, and use Range([your range]).SpecialCells(xlCellTypeBlanks).Delete
edit: If you want to delete the entire row, Range([your range]).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Just as #user3561813 said and please take a look at this link for more complicated filters such as multiple criteria, etc:
ActiveSheet.Range("AD1").AutoFilter Field:=30, Criteria1:="", Operator:=xlOr, Criteria2:="XXXX"
For example, the above code filters for both Blanks and "XXXX" fields
Sub Blank_Cells_Filter()
'Apply filters to include or exclude blank cells
Dim lo As ListObject
Dim iCol As Long
'Set reference to the first Table on the sheet
Set lo = Sheet1.ListObjects(1)
'Set filter field
iCol = lo.ListColumns("Product").Index
'Blank cells – set equal to nothing
lo.Range.AutoFilter Field:=iCol, Criteria1:="="
'Non-blank cells – use NOT operator <>
lo.Range.AutoFilter Field:=iCol, Criteria1:="<>"
End Sub