Excel Filtering and Copying in VBA - excel

I'm working on a VBA script that pulls a range of dates from Access, then filters the data and creates a chart based on the filtered data. The filtered data will be going to a separate sheet where the chart will be pulling its data from . I can get data out of Access with a SQL statement, but my AutoFilter in Excel is erroring out. Here is what I have...
Sheet3.Range("F4:F500").AutoFilter(, "Riveter 01").Copy Destination:=Sheet2.Range("A5")
It gives an app-defined or object-defined error and I can't figure out why. Is this the proper way or is there an easier way?
PS: This filter will happen with 22 unique machines so I was planning on running a loop for each machine. If that is not the fastest or proper way please let me know.

You need to split this into two parts. Filter and then copy/ paste. See below:
With Sheet3
.AutoFilterMode = False
With .Range("F4:F500")
.AutoFilter Field:=1, Criteria1:="Riveter 01"
.SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("A5")
End With
End With
to remove the filter:
On Error Resume Next
Sheet3.ShowAllData
On Error GoTo 0
On Error Resume Next is for when there is no filter present to skip the error. Please note the use of Sheet3 and Sheet2 for those looking for a generic solution.

I think that you have to do this in 2 separate steps:
filter the data
copy the filtered data to another sheet
The answer here has an excellent example of how to do this: Autofilter Macro, then copy visible data ONLY and paste to next available row

Related

VBA Copy visible cells and paste as values - most efficient method

I have an excel tool that uses feed from 11 different raw data files generated from reporting system to do some calculations on that data.
The process is very simple:
- open file
- filter contents
- copy filtered contents
- paste into another file (the summary tool's tab)
As the amounts of data and individual files rose I started getting more issues with memory.Hence my question - what is the most memory and speed efficient way to copy/paste these tables?
(...).SpecialCells(xlCellTypeVisible).Copy Destination:=(...) - I haven't found the way to include XLValues parameter for this (I want to minimise the resources taken up by copying formatting, there are no formulas)
using defined ranges for the copy/paste purpose (name range with xlCellTypeVisible parameter, converting to another range to get the values only and sending the range to destination) - that would require additional variables for the ranges
plain old Columns(...).SpecialCells(xlCellTypeVisible).Copy and Range.("A1").PasteSpecial Paste:=xlValues - this method has both "only visible cells" and "paste values only" bits that I'm looking for, but it uses the clipboard as a middleman and I guess this is eating up my memory
Maybe there is yet another method that I don't know of?
Appreciate any insights!
Fortunately there is a built-in feature for Autofilter to facilitate this. Say we start with:
and apply a filter to it with:
Sub Macro1()
Columns("A:B").AutoFilter
ActiveSheet.Range("$A$1:$B$8").AutoFilter Field:=2, Criteria1:=">50", Operator:=xlAnd
End Sub
==> The Autofilter has a Range property that allows: <==
Sub Kopy()
Dim rng As Range
Set rng = ActiveSheet.AutoFilter.Range
rng.Copy Sheets("Sheet2").Range("A1")
End Sub
The result on Sheet2 has
only the visible data from Sheet1
the header row
is not filtered
NOTE:
Looping is not required.Similar approach can be used for Tables.SpecialCells is not required.

How to copy specific parts of a table from a sheet to another

I'm trying to get some details copied in Excel from Sheet 1 columns 1-5 to Sheet 2 columns 1-4, but only for lines that include text or values on sheet one under a specific column (in this case, Column 2). There are other columns in between, so I need to be able to use exact columns rather than A:D for example.
Example of what I'm trying to achieve:
I have tried using a simple IF function with A:A<>"" so it would include any rows that have any data in them, however this does not seem to copy as I need and occasionally based on my attempts i also get circular reference errors. Additionally, I’m not sure how to make sure this gets pasted at the bottom of a table that will expand with each addition.
I realize a probably easier option would be to simply copy Sheet 1 entirely and use a filter on row 1 to deselect Blanks on A:A, but the sheet has so much more info that it would be a waste, and additionally info is constantly added so I need something scale-able. It also occurred to me now that by doing this i would include info from the "header" and "footer", basically a frozen pane - which I do not need.
Could this be done via a simple function, or would it require a Macro?
Please keep in mind I'm rubbish at programming, just trying to make my life easier and learn as I go. A lot of excel forums help but still I'm no coder. I can understand to a pretty big degree what the code does and can adjust accordingly though :)
As suggested, this cannot be done with formulas. There are different ways to achieve this.. below is one approach:
Sub CopyFilteredRows()
Dim oSourceSheet As Worksheet: Set oSourceSheet = ThisWorkbook.Worksheets("Sheet3") ' Set your source sheet here
Dim oRng As Range: Set oRng = oSourceSheet.Range("A2:E" & oSourceSheet.Range("C" & oSourceSheet.Rows.Count).End(xlUp).Row)
' Set filter on column B
oRng.AutoFilter
oRng.AutoFilter 2, "<>"
' Copy to specified sheet
oRng.SpecialCells(xlCellTypeVisible).EntireRow.Copy ThisWorkbook.Worksheets("Sheet4").Range("A2") ' Change your destination sheet here
' Clear objects
Set oRng = Nothing
Set oSourceSheet = Nothing
End Sub
Paste the above UDF in a Module and then run it whenever you want to perform the copy. I suspect you would have to modify it a bit so that you can cater for your particular scenario but it should give you a start

Excel VBA - Select first cell in filtered listobject data

I want my macro to select or goto the first row of filtered data in a listobject. I am trying this code but I get an error.
activesheet.AutoFilter.Range.offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 2).select
I would like to do this because if the user filters the data, whilst he has scrolled mid-way through the sheet, then he might not see all of the filtered data because of the freezed panes.
ok I figured this out:
Application.Goto lo_f.DataBodyRange.SpecialCells(xlCellTypeVisible).Cells(1), True
Need to use Goto in order to bring the screen to the filtered cell.

VBA clear macro cleans more than the defined range

I have data in Excel which is in table format, half of which (columns) contains data and the rest contains some formulas. I am writing some results on the data part, so I want to delete it before each update.
I tried to delete data using a VBA code, which contains similar piece of codes as below:
Range("A2:K2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
However, it deletes the whole table. I tried various version of this code, but did not work. Also, I realized that, when the data is not in table format the macro works correctly. However, I need it to be a table to update the formulas in a dynamic range.
Does this error is related to table format & any ideas how can I fix it?
You need to use the .DataBodyRange property of the ListObjects object (this is what tables are called in VBA). The code below will clear the non formula elements of a table that is on the active sheet.
Sub ClearTableButLeaveFormulas()
With ActiveSheet.ListObjects(1)
.DataBodyRange.SpecialCells(xlCellTypeConstants).ClearContents
End With
End Sub

Paste special with autofilter and unfilter after autofilter and paste is complete

I am using the solution from this post to autofilter a table and paste the selected columns to another sheet.
It is doing exactly what I want it to do with one exception. I am copying from a table that uses filtered drop down menus. As a result, the following message pops up while copying: The name already exists
The solution offered in this blog is not an option as the source table is dependent on the named values in it.
Is there an additional statement I can add to the below code that will make it paste special?
copyRange4.SpecialCells(xlCellTypeVisible).Copy tgt.Range("E10")
If not, can something be added to select "Yes" without the pop up appearing at all?
Finally, after it pastes to my target, can an additional line be added to unfilter the source?
Disable Alert to by-pass the alert,
Application.DisplayAlerts = False
copyRange4.SpecialCells(xlCellTypeVisible).Copy tgt.Range("E10")
Application.DisplayAlerts = True
To unfilter,
If (Range).AutoFilterMode Then
(Range).ShowAllData
End If
Where (Range) is your range with filter.

Resources