Hi
I am learning vba , and my very 1st time using combo box. So I apologise if any of my below explanations doesn't make sense.
I want the code below to auto filter the data based on the selected value of the combo boxes which I set up. Then copy the selecte data into a a new worksheet ("emails to cardholders"). I set two combo boxes: "ComboTestName"& "ComboTesting_Period". They are ActiveX control boxes.
I find the below code from Ron's website, and varied a little to suit my purpose.
But I am keeping receive a complile error:-Invalid qualifer. Can anyone give some hints, is this due to I haven't declare the varible correctly? Any help would be much appreicated.
Sub Copy_With_AutoFilter1()
'Note: This macro use the function LastRow
Dim My_Range As Range
Dim CalcMode As Long
Dim ViewMode As Long
Dim FilterCriteria As String
Dim CCount As Long
Dim WSNew As Worksheet
Dim sheetName As String
Dim Rng As Range
Dim DestSh As Worksheet
Dim iTestName As String
Dim iTesting_Period As String
Dim ComboTestname As String
Dim ComboTesting_period As String
Dim ctl As MSForms.CheckBox
Dim ole As OLEObject
Dim Iline As Long
Dim oCodeModule As Object
Names.Add Name:="TestName", RefersTo:="=sheetList!$I$2:$I$17"
Names.Add Name:="Testing_Period", RefersTo:="=sheetList!$D:$D"
Set My_Range = Worksheets("Database_2016-17").Range("A1:AD" & lastrow(Worksheets("Database_2016-17")))
My_Range.Parent.Select
Set DestSh = Sheets("emails to cardholders")
If ActiveWorkbook.ProtectStructure = True Or _
My_Range.Parent.ProtectContents = True Then
MsgBox "Sorry, not working when the workbook or worksheet is protected", _
vbOKOnly, "Copy to new worksheet"
Exit Sub
End If
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
ActiveSheet.DisplayPageBreaks = False
My_Range.Parent.AutoFilterMode = False
ole.Name = "combo"
iTestName = ComboTestname
iTesting_Period = ComboTesting_period
With My_Range
.AutoFilter Field:=23, Criteria1:=ComboTesting_period.Value, visibledropdown:=False, Operator:=xlAnd
.AutoFilter Field:=24, Criteria1:=ComboTestname.Value, visibledropdown:=False
End With
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 = .SpecialCells(xlCellTypeVisible)
On Error GoTo 0
End With
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)
.PasteSpecial Paste:=8
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
.Columns("W:AW").EntireColumn.Delete
Application.CutCopyMode = False
End With
'Delete the rows in the My_Range.Parent worksheet
'rng.EntireRow.Delete
End If
End If
'Close AutoFilter
My_Range.Parent.AutoFilterMode = False
'Restore ScreenUpdating, Calculation, EnableEvents, ....
ActiveWindow.View = ViewMode
Application.Goto DestSh.Range("A1")
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub