I have Excel file that I want to filter about one column and on another column add specific value
Sub Macro1()
'
' Macro1
'
'
Range("Table1[type phone]").Select
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:= _
"=*samsung*", Operator:=xlAnd
Range("Table1[company]").Select
'Here I want to add the specific value "Samsung"'
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:= _
"=*iphone*", Operator:=xlAnd
Range("Table1[company]").Select
'Here I want to add the specific value "Apple"'
End Sub
Also how can I filter about one column Blanks cells and on another column add specific value "other"?
[
What conditions can be written if, for example, the Samsung Devices table is missing?
Because if I run the code it crashes in the line it is looking for Samsung.
How can I do it?
Thank You for helping!
To change the value of only cells that are visible (aka those that are show in the filter) you can use SpecialCells(xlCellTypeVisible)
So for your example the code that would go in the first break would be
"Range("B2:B18").SpecialCells(xlCellTypeVisible).Value= "Samsung"
and the second break
"Range("B2:B18").SpecialCells(xlCellTypeVisible).Value= "Apple"
You need to change "18" to whatever the last row is. (Or you can define a variable called LastRow and then call that instead of hard-coding the number which means it will dynamically change based on how many rows there are.)
With LastRow
Sub Macro1()
Dim LastRow As Long
LastRow = ActiveSheet.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
Range("Table1[type phone]").Select
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:= _
"=*samsung*", Operator:=xlAnd
Range("Table1[company]").Select
Range("B2:B" & LastRow).SpecialCells(xlCellTypeVisible).Value= "Samsung"
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:= _
"=*iphone*", Operator:=xlAnd
Range("Table1[company]").Select
Range("B2:B" & LastRow).SpecialCells(xlCellTypeVisible).Value= "Apple"
End Sub
We created a new variable called LastRow and defined it as long (A type of number). Then we defined LastRow according to a formula I use from this site:
https://www.thespreadsheetguru.com/blog/2014/7/7/5-different-ways-to-find-the-last-row-or-last-column-using-vba
Finally we replace Range("B2:B18") with Range("B2:B" & LastRow) which dynamically replaces the 18 with the number LastRow.
Related
I have following code in which I have tried to applied 3 filters, now what I want is, to apply this filter criteria based on named range
I have stored relevant value in specific cell and given names to that range
for first criteria i.e. >=200 named range is VOLUME
for second criteria i.e. >=0.07 named range is MOVE
for third criteria i.e. >=400 named range is STRENTH
So what are the changes that I need to make in following code?
Sub FILTER2()
'FILTER CRITERIA
ActiveSheet.Range("$A$2:$AC$500").AutoFilter Field:=6, Criteria1:=">=200", _
Operator:=xlAnd
ActiveSheet.Range("$A$2:$AC$500").AutoFilter Field:=21, Criteria1:=">=0.07" _
, Operator:=xlAnd
ActiveSheet.Range("$A$2:$AC$500").AutoFilter Field:=28, Criteria1:=">=400" _
, Operator:=xlAnd
Range("A1").Select
End Sub
As per my comment, you might want to consider a With statement, better not to refer to ActiveSheet
Sub FILTER2()
With Sheet1 'Change according to your sheets CodeName
.Range("$A$2:$AC$500").AutoFilter 6, ">=" & .Range("VOLUME")
.Range("$A$2:$AC$500").AutoFilter 21, ">=" & .Range("MOVE")
.Range("$A$2:$AC$500").AutoFilter 28, ">=" & .Range("STRENTH")
End With
End Sub
you can try defining a variable to contain the value of your named range.
Named Range in your sheet as "Volume" then i can store that value in var namedRange1.
example below:
Sub FILTER2()
namedRange1 = range("Volume")
'FILTER CRITERIA
ActiveSheet.range("$A$2:$AC$500").AutoFilter Field:=6, Criteria1:=">=" & namedRange1, _
Operator:=xlAnd
....same with the other lines, just define new variable to contain new named range
The code is designed to remove the column headers (after importing several files) for the data. But I get error: "1004" Which is "Application-defined or object-defined error". I've referenced different solutions on SO but to no avail.
Before I run this code snippit I remove the blank rows and have included this to show what does work as well and might even hold the key.
'Remove all rows with blanks first
Set wb = Workbooks(ThisWorkbook.Name)
'Remove blank rows based on if column 'B' is blank as a and B are filled when there is risk key showing
wb.Worksheets("BatchData").Activate
Columns("B:B").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
'Now to delete based on column headers!
Dim LastRow As Long
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
'Filter Column A and delete selection
'############# Error line below
ActiveSheet.Range("A1:" & A & ":" & LastRow).AutoFilter Field:=2, Criteria1:= _
"=Item", Operator:=xlOr, Criteria2:="="
Selection.EntireRow.Delete
EDIT:
Amended code, some tweaks as per comments and also I had field "2" referenced and was trying to use 'A' which is 1.
Dim LastRow As Long
LastRow = wb.Worksheets("BatchData").Cells(Rows.Count, 1).End(xlUp).Row
'Filter Column A and delete selection
ActiveSheet.Range("A1:A" & LastRow).AutoFilter Field:=1, Criteria1:= _
"=Item", Operator:=xlOr, Criteria2:="="
ActiveSheet.Range("$A$1:$A$" & LastRow).Offset(0, 0).SpecialCells _
(xlCellTypeVisible).EntireRow.Delete
Last line edit based on; VBA: How to delete filtered rows in Excel? but offset changed from 1,0 to 0,0
Trying to clean-up your code a little, and to eliminate possible errors, try the code below, it filters Column A for "Item", then you can use one of 2 options:
Option 1: delete all rows, including header row.
Option 2: delete all rows, except header row.
Option Explicit
Sub DeleteFilteredRows()
Dim LastRow As Long
With ThisWorkbook.Worksheets("BatchData")
' remove blank rows based on if column 'B' is blank as a and B are filled when there is risk key
.Columns("B:B").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
' find last row in Column !
LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
' Filter Column A
.Range("A1:A" & LastRow).AutoFilter Field:=1, Criteria1:= _
"=Item", Operator:=xlOr, Criteria2:="="
' option 1: delete also header row
.Range("A1:A" & LastRow).SpecialCells _
(xlCellTypeVisible).EntireRow.Delete
' option 2: if you want to keep the header row
.Range("A1:A" & LastRow).Offset(1, 0).SpecialCells _
(xlCellTypeVisible).EntireRow.Delete
End With
End Sub
I am new to VBA's and have pieced together this macro from various searches on "how to filter between two specific dates in excel." I am trying to have it read the dates from two cells and restrict the shown data to data between those dates. The input data to the workbook is a SQL table that will be growing over time, so I need the left bound of the range to be the last row with a value in it. Each line in my AutoFilter part returns an "Expected Named Parameter" error with the := sign highlighted. From the forums I have read this is due to using VBA reserved words as variable names, but I not think that is the case in this instance.
Here is my code:
Public Sub MyFilter()
.AutoFilterMode = False
Dim datRight, datLeft As Date
Dim lastRow As Long
datLeft = Range("J1").Value
datRight = Range("J2").Value
lastRow = Range("A:A").Find("*", Range("A2"), searchdirection:=xlPrevious).Row
ActiveSheet.Range("F2:F" & lastRow).AutoFilter Field:=7,
Criteria1:=">=" & datLeft, _
Operator:= xlAnd,
Criteria2:="<=" & datRight, VisibleDropDown:=True
End Sub
The first error you will get is at the line .AutoFilterMode = False Notice the DOT before Autofilter. You have to qualify it with the relevant sheet. For example, ThisWorkbook.Sheets("Sheet1").AutoFilterMode = False
When you are trying to find the lastrow using *, Always use the method as mentioned in THIS post else you will get an error if the worksheet is blank.
Your declaration Dim datRight, datLeft As Date. In VBA only the last variable will be declared as Date and the first one will be declared as Variant. Change it to Dim datRight As Date, datLeft As Date Also if the J1 and J2 values are not date values then you will get an error.
Now to your problem. You are getting that error because you the missing the continuation character _
Try this
ActiveSheet.Range("F2:F" & lastRow).AutoFilter Field:=7, _
Criteria1:=">=" & datLeft, _
Operator:=xlAnd, _
Criteria2:="<=" & datRight, VisibleDropDown:=True
I have a table put together as a database. I am trying to write a macro to search a System Size column in my table to find "2500" then search a Standard column to find "Standard" then search a Category column to find "FL" I then want to copy the value from a Select Item column pertaining to the row these values were found in to another sheet. For example, the macro will search Column E (System Size) for all "2500", then it will search Column F (Standard) for all "Standard", then it will search Column G (Category) for all "FL". I then want it to copy the values from Column C (Select Item) for every line that meets these requirements and paste it to another sheet. Following is the code I have so far but I can only get it to search one cell and not the entire column. There is probably a better way to go about it but this is the only way I have found that works.
Sub ImDoingMyBest()
'
' ImDoingMyBest Macro
'
'
If Sheets("Database").Range("E2").Value Like "*2500*" Then
Sheets("Database").Range("C2").Copy
Sheets("Quote Sheet").Select
Range("B26").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
End If
End Sub
The way to search the whole column is to use a for-loop; for instance:
For i = 1 To 10000
If Sheets("Database").Range("E" & i).Value Like "*2500*" Then
Sheets("Database").Range("C" & i).Copy
...
...
End If
Next i
Alternatively (and my preference) use the Cells(row, column) format rather than Range - this avoids having to concatenate the Range reference. This would take
Range("E" & i)
and change to
Cells(i, 5)
which is neater code (IMO).
Following up on Siddarth Rout's comments, the following code uses Autofilter to isolate the rows in the "Database" sheet that meet your criteria, and then copies the corresponding values in column C to a range beginning in cell B26 of the sheet named "Quote Sheet".
Sub FilterAndCopy()
Dim dataWs As Worksheet
Dim copyWs As Worksheet
Dim totRows As Long
Dim lastRow As Long
Set dataWs = Worksheets("Database")
Set copyWs = Worksheets("Quote Sheet")
With dataWs
.AutoFilterMode = False
With .Range("C:G")
.AutoFilter Field:=3, Criteria1:="2500"
.AutoFilter Field:=4, Criteria1:="Standard"
.AutoFilter Field:=5, Criteria1:="FL"
End With
End With
totRows = dataWs.Range("C:C").Rows.count
lastRow = dataWs.Range("C" & totRows).End(xlUp).Row
dataWs.Range("C2:C" & lastRow).Copy
copyWs.Range("B26").PasteSpecial Paste:=xlPasteValues
dataWs.AutoFilterMode = False
End Sub
I have a big database-like sheet, first row contains headers. I would like a subset of rows of this table based on column values. Two issues:
1) VBA-wise I would like to loop through the columns, when the values for all necessary columns all match, copy the entire row into a new sheet.
2) The subset of rows is based on a list. I just read I can use Autofilter with an array. Is it possible to input this array from a column instead of manually entering it in the VBA code? The list I'm using consists of 200 different strings and will be updated periodically.
Where CritList is the list of strings. I still need to figure out how, but now I leave the office, so more tomorrow.
EDIT1 Thanks to #DougGlancy; the autofiltering works now. Here is his beautiful code (I only added the array-filter).
EDIT2 Included a more elaborate array-filter, where NameList is the list I would like to filter for. Now it all works!
Sub FilterAndCopy()
Dim LastRow As Long
Dim vName As Variant
Dim rngName As Range
Set rngName = Sheets("Sheet3").Range("NameList")
vName = rngName.Value
Sheets("Sheet2").UsedRange.Offset(0).ClearContents
With Worksheets("Sheet1")
.Range("A:E").AutoFilter
'Array filter from NameList
.Range("A:J").AutoFilter Field:=3, Criteria1:=Application.Transpose(vName), _
Operator:=xlFilterValues
.Range("A:E").AutoFilter field:=2, Criteria1:="=String1" _
, Operator:=xlOr, Criteria2:="=string2"
.Range("A:E").AutoFilter field:=3, Criteria1:=">0", _
.Range("A:E").AutoFilter field:=5, Criteria1:="Number"
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("A1:A" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Copy _
Destination:=Sheets("Sheet2").Range("A1")
End With
End Sub
Here's a different approach. The heart of it was created by turning on the Macro Recorder and filtering the columns per your specifications. Then there's a bit of code to copy the results. It will run faster than looping through each row and column:
Sub FilterAndCopy()
Dim LastRow As Long
Sheets("Sheet2").UsedRange.Offset(0).ClearContents
With Worksheets("Sheet1")
.Range("$A:$E").AutoFilter
.Range("$A:$E").AutoFilter field:=1, Criteria1:="#N/A"
.Range("$A:$E").AutoFilter field:=2, Criteria1:="=String1", Operator:=xlOr, Criteria2:="=string2"
.Range("$A:$E").AutoFilter field:=3, Criteria1:=">0"
.Range("$A:$E").AutoFilter field:=5, Criteria1:="Number"
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("A1:A" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Copy _
Destination:=Sheets("Sheet2").Range("A1")
End With
End Sub
As a side note, your code has more loops and counter variables than necessary. You wouldn't need to loop through the columns, just through the rows. You'd then check the various cells of interest in that row, much like you did.