Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Autofiltering & copying results to new worksheet - custom message box

Good afternoon!

I've been using Ron de Bruin's code below to perform an auto filter on one
worksheet and copy/paste the results into another sheet. The auto filter, when
run, will display a message box in which the user will input the auto filter
criteria. This works perfectly, however, I need to make one small change...I
need to provide a combo box with preset (they will not change) options to
choose
from, rather than a blank space to type in the filter criteria. My current
code
is below.


Option Explicit

Sub CreateMarketingElectionReport()
'Note: This macro use the function LastRow
'Important: The DestSh must exist
Dim My_Range As Range
Dim DestSh As Worksheet
Dim CalcMode As Long
Dim ViewMode As Long
Dim FilterCriteria As String
Dim CCount As Long
Dim rng As Range

'Set filter range
Set My_Range = Worksheets("Marketing
Elections").Range("A4:Z" &
LastRow(Worksheets("Marketing
Elections")))

'Set the destination worksheet
Set DestSh = Sheets("Marketing Election Report")

If ActiveWorkbook.ProtectStructure = True Or My_Range.Parent.ProtectContents =
True Then
MsgBox "Sorry, that feature is not available when the workbook is
protected.", vbOKOnly, "Copy to new worksheet"
Exit Sub
End If

'Change ScreenUpdating, Calculation, EnableEvents, ....
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
ActiveSheet.DisplayPageBreaks = False

'Firstly, remove the AutoFilter
My_Range.Parent.AutoFilterMode = False

'If you want to filter on a Inputbox value use this
FilterCriteria = InputBox("What type of election do you need info
for?", _
"Enter election type")
If FilterCriteria = "" Then Exit Sub
FilterCriteria = Replace(FilterCriteria, "*", "")
FilterCriteria = "*" & FilterCriteria & "*"
My_Range.AutoFilter Field:=22, Criteria1:="=" & FilterCriteria


'Check if there are not more then 8192 areas(limit of areas that Excel can
copy)
CCount = 0
On Error Resume Next
CCount =
My_Range.Columns(1).SpecialCells(xlCellTypeVisible ).Areas(1).Cells.Count
On Error GoTo 0
If CCount = 0 Then
MsgBox "There are more than 8192 areas:" _
& vbNewLine & "It is not possible to copy the visible data."
_
& vbNewLine & "Tip: Sort your data before you use this
macro.", _
vbOKOnly, "Copy to worksheet"
Else
'Copy the visible data and use PasteSpecial to paste to the Destsh
With My_Range.Parent.AutoFilter.Range
On Error Resume Next
' Set rng to the visible cells in My_Range without the header row
Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count) _
.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not rng Is Nothing Then
'Copy and paste the cells into DestSh below the existing data
rng.Copy
With DestSh.Range("A" & LastRow(DestSh) + 1)
' Paste:=8 will copy the columnwidth in Excel 2000 and higher
' Remove this line if you use Excel 97
'.PasteSpecial Paste:=8
.PasteSpecial xlPasteValues
' .PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
'Delete the rows in the My_Range.Parent worksheet
'rng.EntireRow.Delete
End If
End With
End If

'Close AutoFilter
'My_Range.Parent.AutoFilterMode = False
My_Range.Parent.ShowAllData

'Restore ScreenUpdating, Calculation, EnableEvents, ....

With Application
.EnableEvents = True
.Calculation = CalcMode
Dim endrange As Long
End With
Call CopyMarketingElectionReport

End Sub
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
subtotals with custom autofiltering teh_chucksta Excel Discussion (Misc queries) 1 May 30th 09 08:38 AM
Autofiltering and copying selection to anotherworkbook Albert Excel Programming 9 January 30th 08 06:17 AM
Searching a workbook and copying results to new worksheet jhyatt Excel Discussion (Misc queries) 0 October 4th 07 05:09 PM
Copying a region in excel results in the entire worksheet being pa THT Excel Worksheet Functions 0 October 4th 06 05:15 PM
Copying RESULTS of a FORMULA to another worksheet Scott Howell Excel Programming 2 November 21st 03 10:46 PM


All times are GMT +1. The time now is 03:45 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"