S
stac2410
Good afternoon
I've been using Ron de Bruin's code below to perform an auto filter on on
worksheet and copy/paste the results into another sheet. The auto filter, whe
run, will display a message box in which the user will input the auto filte
criteria. This works perfectly, however, I need to make one small change...
need to provide a combo box with preset (they will not change) options t
choos
from, rather than a blank space to type in the filter criteria. My curren
cod
is below
Option Explici
Sub CreateMarketingElectionReport(
'Note: This macro use the function LastRo
'Important: The DestSh must exis
Dim My_Range As Rang
Dim DestSh As Workshee
Dim CalcMode As Lon
Dim ViewMode As Lon
Dim FilterCriteria As Strin
Dim CCount As Lon
Dim rng As Rang
'Set filter rang
Set My_Range = Worksheets("Marketin
Elections".Range("A4:Z" &
LastRow(Worksheets("Marketin
Elections")
'Set the destination workshee
Set DestSh = Sheets("Marketing Election Report"
If ActiveWorkbook.ProtectStructure = True Or My_Range.Parent.ProtectContents
True The
MsgBox "Sorry, that feature is not available when the workbook i
protected.", vbOKOnly, "Copy to new worksheet"
Exit Su
End I
'Change ScreenUpdating, Calculation, EnableEvents, ...
With Applicatio
CalcMode = .Calculatio
.Calculation = xlCalculationManua
.ScreenUpdating = Fals
.EnableEvents = Fals
End Wit
ViewMode = ActiveWindow.Vie
ActiveWindow.View = xlNormalVie
ActiveSheet.DisplayPageBreaks = Fals
'Firstly, remove the AutoFilte
My_Range.Parent.AutoFilterMode = Fals
'If you want to filter on a Inputbox value use thi
FilterCriteria = InputBox("What type of election do you need inf
for?",
"Enter election type"
If FilterCriteria = "" Then Exit Su
FilterCriteria = Replace(FilterCriteria, "*", ""
FilterCriteria = "*" & FilterCriteria & "*"
My_Range.AutoFilter Field:=22, Criteria1:="=" & FilterCriteri
'Check if there are not more then 8192 areas(limit of areas that Excel ca
copy
CCount =
On Error Resume Nex
CCount
My_Range.Columns(1).SpecialCells(xlCellTypeVisible).Areas(1).Cells.Coun
On Error GoTo
If CCount = 0 The
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 thi
macro.",
vbOKOnly, "Copy to worksheet"
Els
'Copy the visible data and use PasteSpecial to paste to the Dests
With My_Range.Parent.AutoFilter.Rang
On Error Resume Nex
' Set rng to the visible cells in My_Range without the header ro
Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count)
.SpecialCells(xlCellTypeVisible
On Error GoTo
If Not rng Is Nothing The
'Copy and paste the cells into DestSh below the existing dat
rng.Cop
With DestSh.Range("A" & LastRow(DestSh) + 1
' Paste:=8 will copy the columnwidth in Excel 2000 and highe
' Remove this line if you use Excel 9
'.PasteSpecial Paste:=
.PasteSpecial xlPasteValue
' .PasteSpecial xlPasteFormat
Application.CutCopyMode = Fals
End Wit
'Delete the rows in the My_Range.Parent workshee
'rng.EntireRow.Delet
End I
End Wit
End I
'Close AutoFilte
'My_Range.Parent.AutoFilterMode = Fals
My_Range.Parent.ShowAllDat
'Restore ScreenUpdating, Calculation, EnableEvents, ...
With Applicatio
.EnableEvents = Tru
.Calculation = CalcMod
Dim endrange As Lon
End Wit
Call CopyMarketingElectionRepor
End Su
I have created a Userform "frmChooseElection" which provides th
dro
down for the user to select their criteria. However, I'm not sure how to ti
i
my userform into the code above
Any help is tremendously appreciated, I've been working on this for over
wee
without figuring it out yet.
I've been using Ron de Bruin's code below to perform an auto filter on on
worksheet and copy/paste the results into another sheet. The auto filter, whe
run, will display a message box in which the user will input the auto filte
criteria. This works perfectly, however, I need to make one small change...
need to provide a combo box with preset (they will not change) options t
choos
from, rather than a blank space to type in the filter criteria. My curren
cod
is below
Option Explici
Sub CreateMarketingElectionReport(
'Note: This macro use the function LastRo
'Important: The DestSh must exis
Dim My_Range As Rang
Dim DestSh As Workshee
Dim CalcMode As Lon
Dim ViewMode As Lon
Dim FilterCriteria As Strin
Dim CCount As Lon
Dim rng As Rang
'Set filter rang
Set My_Range = Worksheets("Marketin
Elections".Range("A4:Z" &
LastRow(Worksheets("Marketin
Elections")
'Set the destination workshee
Set DestSh = Sheets("Marketing Election Report"
If ActiveWorkbook.ProtectStructure = True Or My_Range.Parent.ProtectContents
True The
MsgBox "Sorry, that feature is not available when the workbook i
protected.", vbOKOnly, "Copy to new worksheet"
Exit Su
End I
'Change ScreenUpdating, Calculation, EnableEvents, ...
With Applicatio
CalcMode = .Calculatio
.Calculation = xlCalculationManua
.ScreenUpdating = Fals
.EnableEvents = Fals
End Wit
ViewMode = ActiveWindow.Vie
ActiveWindow.View = xlNormalVie
ActiveSheet.DisplayPageBreaks = Fals
'Firstly, remove the AutoFilte
My_Range.Parent.AutoFilterMode = Fals
'If you want to filter on a Inputbox value use thi
FilterCriteria = InputBox("What type of election do you need inf
for?",
"Enter election type"
If FilterCriteria = "" Then Exit Su
FilterCriteria = Replace(FilterCriteria, "*", ""
FilterCriteria = "*" & FilterCriteria & "*"
My_Range.AutoFilter Field:=22, Criteria1:="=" & FilterCriteri
'Check if there are not more then 8192 areas(limit of areas that Excel ca
copy
CCount =
On Error Resume Nex
CCount
My_Range.Columns(1).SpecialCells(xlCellTypeVisible).Areas(1).Cells.Coun
On Error GoTo
If CCount = 0 The
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 thi
macro.",
vbOKOnly, "Copy to worksheet"
Els
'Copy the visible data and use PasteSpecial to paste to the Dests
With My_Range.Parent.AutoFilter.Rang
On Error Resume Nex
' Set rng to the visible cells in My_Range without the header ro
Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count)
.SpecialCells(xlCellTypeVisible
On Error GoTo
If Not rng Is Nothing The
'Copy and paste the cells into DestSh below the existing dat
rng.Cop
With DestSh.Range("A" & LastRow(DestSh) + 1
' Paste:=8 will copy the columnwidth in Excel 2000 and highe
' Remove this line if you use Excel 9
'.PasteSpecial Paste:=
.PasteSpecial xlPasteValue
' .PasteSpecial xlPasteFormat
Application.CutCopyMode = Fals
End Wit
'Delete the rows in the My_Range.Parent workshee
'rng.EntireRow.Delet
End I
End Wit
End I
'Close AutoFilte
'My_Range.Parent.AutoFilterMode = Fals
My_Range.Parent.ShowAllDat
'Restore ScreenUpdating, Calculation, EnableEvents, ...
With Applicatio
.EnableEvents = Tru
.Calculation = CalcMod
Dim endrange As Lon
End Wit
Call CopyMarketingElectionRepor
End Su
I have created a Userform "frmChooseElection" which provides th
dro
down for the user to select their criteria. However, I'm not sure how to ti
i
my userform into the code above
Any help is tremendously appreciated, I've been working on this for over
wee
without figuring it out yet.