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
Related
Filtered data for column M is not filling down visible with my input change to first visible row of data in column M. It is filling down for first row in the first cell beyond the header row for column M not my input into the first visible cell in M. It is not validating data is returned and filling down the first row down. Not skipping because there was no data to fill down.
I have tried many different pieces of code I have found on visible and selection and filling down to no avail.
Sub DataData ()
'Change Active Worksheet to Raw Data
Worksheets("Raw Data").Activate
Application.ScreenUpdating = False
ActiveSheet.Range("$A$1:$Z$" & Rows.Count).End(xlUp).AutoFilter Field:=4, Criteria1:= _
"*Data*"
With Worksheets("Raw Data").AutoFilter.Range
Range("M" & .Offset(1, 0).SpecialCells(xlCellTypeVisible)(1).Row).Select
End With
ActiveCell.FormulaR1C1 = " DataData "
With Worksheets("Raw Data").AutoFilter.Range
Range("M" & .Offset(1, 0).SpecialCells(xlCellTypeVisible)(1).Row).Select
End With
With ActiveSheet.UsedRange.Select
se
.Resize(.Rows.Count - 1).Offset(1).Columns("M"). _
SpecialCells(xlCellTypeVisible).FillDown
End With
ActiveSheet.ShowAllData
I am trying to filter Data on the 4th column, then in row M on first visible cell for that column, input DataData (if there was anything found in the filter), then fill down only visible data from first cell visible in column M.
Going to try and fix what I think you're doing and wanting:
option explicit 'put this at the top of your module
Sub DataData()
Dim lr as long
With Sheets("Raw Data")
lr = .cells(.rows.count,1).end(xlup).row
.Range(.Cells(2,"M"),.Cells(lr,"M")).SpecialCells(xlCellTypeVisible).value = " DataData "
.ShowAllData
End With
End Sub
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.
I need help with removing duplicates and adding their corresponding values - dependent on whether or not they are duplicates from another column. In this case, if the part number is duplicate under the same work order, then remove duplicate - however, add all values. I will need avoid removing duplicates from another work order number.
Try the following, it will add a column to the right, use formula to SumIF, then paste the results as values and delete column C to bring the data to the correct column, then remove duplicates to give you the summary expected:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data from Column A
Range("D2").FormulaR1C1 = "=SUMIF(C[-2],RC[-2],C[-1])"
'enter a sumif formula to add all with same item number
Range("D2:D" & LastRow).FillDown 'fill the formula to the last row
Range("D2:D" & LastRow).Copy 'copy
Range("D2:D" & LastRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'paste as values
Range("C2:C" & LastRow).Delete Shift:=xlToLeft
'delete column C and bring to the left the values calculated by formula
Range("$A$1:$D$" & LastRow).RemoveDuplicates Columns:=Array(1, 2, 3, 4), Header:=xlYes
'remove duplicates
End Sub
I have a spreadsheet with ~50,000 rows of data. This value will fluctuate because I have a function to bring in more data.
What I am hoping to do is be able to use my column filters (Row 2) to display the items I do not want. Then the macro will delete all the visible values, turn off the filters, delete all the empty rows, and finally turn the filters in row 2 back on. The following code is designed to do this but leaves empty rows throughout the data matrix. Thank you for your help!
Sub DeleteVisible_Empty_BlankRows()
Application.ScreenUpdating = False
Range("C3:V" & rows.count).ClearContents
Worksheets("MyDataPull").AutoFilterMode = False
'Ungroup & regroup necessary columns
Columns("F:K").Columns.Ungroup
Dim r As Range, emrows As Long, i As Long
LRI = Mydatapull.Cells(Mydatapull.rows.count, "C").End(xlUp).Row
'Filter column C from A to Z so that blanks are put at the bottom
Range("C3:V" & LRI).Sort Key1:=Range("C3"), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
'Filter to only show blanks
Range("C3:V" & LRI).AutoFilter Field:=1, Criteria1:="=", Operator:=xlOr, Criteria2:="=" & ""
'Delete blanks
Range("C3:V" & LRI).EntireRow.Delete
'Turn filters back on
Worksheets("MyDataPull").AutoFilterMode = False
'Delete Extra rows
LRI = Mydatapull.Cells(Mydatapull.rows.count, "C").End(xlUp).Row
Range("C" & LRI + 1 & ":V" & rows.count).EntireRow.Delete
Mydatapull.Range("C2:V2").AutoFilter
Columns("F:K").Columns.Group
End Sub
Have you tried first unfiltering the data, then deleting the empty rows? I've personally had more success working with content when sheets are unfiltered.
Try adding:
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.ShowAllData
before you delete the empty rows.
Let me know if that works for you.
I figured this out. See the updated code above.
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.