Autofilter copy paste not all data - vba

I’m trying to have my macro sort column L for DE42, and then copy the visible cells into column AL minus the header Undefined 3, to the last row of the table. I get it to sort but it copies the header and only pastes like half the data. Here’s what I have so far:
Sub DE42 ()
ActiveWorkbook.Sheets(“Alerted Deduped”)
Range (“L1”).AutoFilter Field:=12 Criteria1:=“DE42”
Range(“AL2”).SpecialCell xlCellTypeVisible.Copy
Range.(“F2”).PasteSpecial xlPasteValues
End Sub()

Not sure what you mean by into a last row of a table?
This will simply filter and the copy the visible filter range to AL2 but if the filter is still on then some data may be hidden.
Sub DE42()
With ActiveWorkbook.Sheets("Alerted Deduped")
.Range("L1").AutoFilter Field:=1, Criteria1:="DE42"
.AutoFilter.Range.Offset(1).Resize(.AutoFilter.Range.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Copy .Range("AL2")
End With
End Sub
Your original filter specified Field 12 so I am wondering if you actually meant to filter A1:L1 on field 12? I will update answer accordingly if so.
Version 2 Assuming data starts column A
Option Explicit
Sub DE42()
With ActiveWorkbook.Sheets("Alerted Deduped")
.Range("A1").AutoFilter
.Range("A1").AutoFilter Field:=12, Criteria1:="DE42"
.AutoFilter.Range.Offset(1, 37).Resize(.AutoFilter.Range.Rows.Count - 1, 1).SpecialCells(xlCellTypeVisible).Copy .Range("F2")
End With
End Sub

Related

Excel VB Advanced Filter Copy with Condition

I am trying to put a condition on each row copied. I want all uniques but only if they also have a specific value in another field.
This is what I have to grab all uniques (and it works) but I can't figure out how to get only the rows with a specific value in column J.
r1.Columns(20).AdvancedFilter xlFilterCopy, , Sheet11.Range("A1"), unique:=True
I have tried doing a CriteriaRange but I can't seem to get the syntax correct for it. Additionally I thought about an If statement but logically in my head it means it would fire off the whole list every time it has a true statement, not on a per row basis.
Here is how I thought it might work. But I get a type mismatch error.
r1.Columns(20).AdvancedFilter xlFilterCopy, r1.Columns(10).Value = "November", Sheet11.Range("A1"), unique:=True
Thoughts?
First of all, your Criteria Range should be just that - a Range with the header corresponding to the column to be filtered, and criteria underneath. For example, D1:D2 in this snapshot:
Secondly, you won't be able to copy just a single column (20) while filtering another column (10) in the same step.
You can tweak the Advanced Filter to
First filter the entire list in place based on the criterion provided
And then copy the visible cells in the column in question
Something like this (change Sheet and Range references as needed):
Sub MyFilter()
Dim lastRow As Long
With Sheet1
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range("A1:B" & lastRow).AdvancedFilter _
Action:=xlFilterInPlace, CriteriaRange:=.Range("D1:D2"), Unique:=True
With .Range("B1:B" & lastRow).SpecialCells(xlCellTypeVisible)
.Copy Sheet2.Range("A1")
End With
.ShowAllData
End With
End Sub
To be able to keep the other parts of the code that worked perfectly. I added a hidden sheet and wrote a macro to copy the filtered results out to the new hidden sheet. Then I ran my original code against the filtered data on that hidden sheet.
Sub FilterLiveToDataSheet()
' Unhide Required Sheets
Sheets("Original-Data").Visible = True
Sheets("Filtered-Data").Visible = True
' Delete Old Data
Sheets("Filtered-Data").Select
Cells.Select
Selection.ClearContents
' Copy Filtered Data
Sheets("Original-Data").Select
Range("TBL_ATTR_Spend[[#Headers],[HeaderName]]").Select
Selection.AutoFilter
ActiveSheet.ListObjects("TBL_ATTR_Spend").Range.AutoFilter Field:=10, _
Criteria1:="Delta"
Cells.Select
Selection.Copy
' Paste to Data Sheet
Sheets("Filtered-Data").Select
Cells.Select
ActiveSheet.Paste
' Unfilter Original Data Page
Sheets("Original-Data").Select
Range("TBL_ATTR_Spend[[#Headers],[HeaderName]]").Select
Selection.AutoFilter
' Hide Required Sheets
Sheets("Original-Data").Visible = False
Sheets("Filtered-Data").Visible = False
' Go to Results Sheet
Sheets("Results").Select

vba: delete filtered rows but not first one (or store it and paste after deletion)

Working on Microsoft Visual Basic Application Edition 7.1 in Excel 2013
Data are on columns from A to D, rows' number varies from time to time. I would like to delete all rows for which column B's value doesn't start with LCR (and also I would like not to bore with a for...next loop).
Something like:
Columns("B:B").AutoFilter Field:=1, Criteria1:="<>LCR*"
Selection.Delete
Unfortunately, this code deletes heading row (row number 1) and I don't want.
I tried to store row number 1 elsewhere in a range variable, but it doesn't work (run-time error '424')
Set r1 = Range("A1:D1")
r1.Copy
Columns("B:B").AutoFilter Field:=1, Criteria1:="<>LCR*"
Selection.Delete
With Range("A1:D1")
.Insert Shift:=xlDown
.Select
.Value = r1.Value
End With
How can I tell the filter to start from row number two (or how can I correctly store content of row number one so to paste it after deletion by filter)?
Thanks in advance for your help
Define your range for deleting as Range(Cells(2,2),Cells(ActiveSheet.UsedRange.Rows.Count,2)) (To replace the Selection call). This will delete everything except for the first cell in the column
Edit to avoid excel prompt: Range(Cells(2,2),Cells(ActiveSheet.UsedRange.Rows.Count,2)).EntireRow
in a more complete way you could go like this
Sub main()
With Worksheets("MyWantedSheet") '<--| always specify full worksheet reference (change "MyWantedSheet" with your actual sheet name)
With .Columns("B:B") '.Resize(.Cells(.Rows.Count, "B").End(xlUp).Row) '<--| refer to wanted column range down to its last non empty cell
.AutoFilter '<--| remove possible preeeding autofilter filtering
.AutoFilter Field:=1, Criteria1:="<>LCR*" '<--| apply current filtering
If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then '<--| if there are visible cells other than the "header" one
.Resize(.Parent.Cells(.Parent.Rows.Count, "B").End(xlUp).Row - 1).Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete '<--|delete visible rows other than the first ("headers") one
End If
End With
.AutoFilterMode = False '<--| remove drop-down arrows
End With
End Sub

Excel: Copy and insert rows on another sheet based on cell

I'm trying to make a code that checks for numbers in a master sheet called All in column D (ex. 780101) and if it meets the criteria, it copies the whole row and inserts (not paste) it to another sheet with the name of the criteria (ex. 780101), starting on row 6.
The code I have doesn't work like I want it to. It doesn't copy all the rows that meet the criteria and sometimes it inserts blank rows.
Sub Insert()
For Each Cell In Sheets("All").Range("D:D")
If Cell.Value = "780101" Then
matchRow = Cell.Row
Rows(matchRow & ":" & matchRow + 1).Select
Selection.Copy
Sheets("780101").Select
Rows("6:6").Select
Selection.Insert Shift:=xlDown
End If
Next
End Sub
I'm just starting to learn VBA, so if it could be possible the names of the sheets would be the criteria of the cell values (the code is made for only one sheet - 780101, but there are 20 of sheets with different names).
It's tough to make recommendations without seeing sample data and what could potentially be causing the problems you are having but you can run this rehash of your existing code.
Sub Insert()
Dim dc As Range
With Sheets("All")
For Each dc In Intersect(.Range("D:D"), .UsedRange)
If dc.Value2 = 780101 Then
dc.Resize(2, 1).EntireRow.Copy
Sheets("780101").Rows(6).Insert Shift:=xlDown
End If
Next
End With
End Sub
The nature of running that from top to bottom means that the results will be reversed. You may wish to consider running the main loop from bottom to top to maintain the order.

Excel VBA filtering and copy pasting data

Given a data set, that is lets say 10 columns. In column A i have date and in column B i have %. I want to filter for column A only 2014 data and for column B <70% and copy paste the filtered data into another worksheet. How do i go about writing the code for the next line to get to the first row of filtered data?
ActiveSheet.Range("$A$1:$AR$1617").AutoFilter Field:=5, Operator:= _
xlFilterValues, Criteria2:=Array(0, "12/28/2014")
ActiveSheet.Range("$A$1:$AR$1617").AutoFilter Field:=14, Criteria1:="<0.7" _
, Operator:=xlAnd
Use Offset method like this:
ActiveSheet.Range("A1:AR1617").Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy
Above code copies the filtered data excluding the header.
Is this what you're trying?
ActiveSheet.Range("$A$1:$AR$1617").specialcells(xlcelltypevisible).copy sheets("Sheet2").range("A1")
Copies only the visible/filtered cells to a new worksheet as the first part of your question requests.
To get the first line of data, presuming there is data and you have headers in line 1, and paste that to a new sheet's next available row.
Activesheet.range("A1").end(xldown).entirerow.copy sheets("Sheet2").range("A65536").end(xlup).offset(1,0)
And a loop solution, that will copy data from sheet1 to sheet2, providing that column A always contains data (will be a bit slow on very large data sets):
Sub copyFilteredData(ByVal iYear As Integer, ByVal dPercent As Double)
Dim rCell As Range
For Each rCell In Sheet1.Range("A1:A65536").SpecialCells(xlCellTypeConstants)
If Year(rCell.Offset(0, 4).Value) = iYear And rCell.Offset(0, 13).Value < dPercent Then
rCell.EntireRow.Copy Sheet2.Range("A65536").End(xlUp).Offset(1, 0)
End If
Next rCell
End Sub
Call using:
copyfilteredData 2014,0.7

Excel VBA Conditional Filter

simple VBA question. I need a VBA subroutine that will filter my data based on a condition (in this case, if what's in column C equals 11-Jun-12 [41071 in numeric form]) without looping through the whole data set. I've looked a bit at filtering on the Internet but nothing seems to be quite what I want (or I just don't really understand what's going on). To be clear, here's an example of what I want:
I want http://imgur.com/qebVv
to go to http://imgur.com/zDncq.
Thanks!
Assuming that the spreadsheet is set up as it appears in your screenshot here is what you can do
Sub DateFilter()
'hide dialogs
Application.ScreenUpdating = False
'filter for records that have June 11, 2012 in column 3
ActiveSheet.Cells.AutoFilter Field:=3, Criteria1:="6/11/2012"
'deletes all matching rows
ActiveSheet.AutoFilter.Range.Delete
Application.ScreenUpdating = True
End Sub
Jack Radcliffe,
Do you mean a simple autofilter, for example:
Sub SimpleColumnDateFilter1()
' Quick Recorded Macro
' Select a Column
' Activate Autofilter
' For a range C1 through the rest of C
' Autofilter such that the column will display dates not equal to 11/15/2012
Columns("C:C").Select
Selection.AutoFilter
ActiveSheet.Range("C:C").AutoFilter Field:=1, Criteria1:= _
"<>11/15/2012", Operator:=xlAnd
End Sub