moogthemoog
Board Regular
- Joined
- Nov 17, 2004
- Messages
- 51
Hi
I've got VBA to Autofilter records, and to copy the filtered results to another sheet.
When no records are returned, the spreadsheet crashes.
Is there any code to identify if the number of records returned is 0, then go to a specific line of code?
I couldn't find anything completely relevant on the boards, or on the web, for that matter.
Thanks for your help, in advance.
Relevant area of code:
I need something here to stop the routine/goto ErrHandler if the records returned = 0.
Full code, for completeness:
Thanks
Jon
I've got VBA to Autofilter records, and to copy the filtered results to another sheet.
When no records are returned, the spreadsheet crashes.
Is there any code to identify if the number of records returned is 0, then go to a specific line of code?
I couldn't find anything completely relevant on the boards, or on the web, for that matter.
Thanks for your help, in advance.
Relevant area of code:
Code:
Sheets("MCS").Select
ActiveSheet.Range("$A$13:$XFD$13").AutoFilter Field:=5, Criteria1:="CANCELLED"
Range("B13").Offset(rowoffset:=1).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.Offset(columnoffset:=endco - 2)).Copy 'selects to the end of the columns of data
I need something here to stop the routine/goto ErrHandler if the records returned = 0.
Full code, for completeness:
Code:
Sub mcrMoveCancelled()
' Move cancelled contracts from MCS to CANC
'Turn off screen updating
Application.ScreenUpdating = False
'Turn off Events enable
Application.EnableEvents = False
'Turn off Auto Calculate - to speed up application
Application.Calculation = xlCalculationManual
On Error GoTo ErrHandler
'< endco: this picks the last column to be copied over
Sheets("MCS").Select
Range("A13").Select
endco = Range("XFD13").Column
ActiveCell(1, endco).Select
Selection.End(xlToLeft).Select
endco = ActiveCell.Column
'/endco>
Sheets("MCS").Unprotect
Sheets("CANC").Unprotect
If Sheets("MCS").FilterMode = True Then
Sheets("MCS").ShowAllData
End If
If Sheets("CANC").FilterMode = True Then
Sheets("CANC").ShowAllData
End If
Sheets("MCS").Select
ActiveSheet.Range("$A$13:$XFD$13").AutoFilter Field:=5, Criteria1:="CANCELLED"
Range("B13").Offset(rowoffset:=1).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.Offset(columnoffset:=endco - 2)).Copy 'selects to the end of the columns of data
Sheets("CANC").Select
Range("B30163").Select
Selection.End(xlUp).Offset(rowoffset:=1).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Selection.Locked = True 'To protect cells
'< Sort CANC
Range("B13").Select
Range(Selection, Selection.Offset(columnoffset:=(endco - 2))).Select 'titles
Range(Selection, Selection.End(xlDown)).Select
'Sort the data
'to sort the data by Company ID, Cancellation Date, Parent ID, Debtor ID and MCS No (4 keys)
'macro won't do more than 3, apparently, so need to sort in two goes:
'Debtor ID and MCS no first
' then Company ID, Cancellation Date, and Parent ID
Selection.Sort Key1:=Range("S13"), Order1:=xlAscending, Key2:=Range("C13") _
, Order2:=xlAscending, Header:= _
xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, DataOption2:=xlSortNormal
Selection.Sort Key1:=Range("B13"), Order1:=xlAscending, Key2:=Range("K13") _
, Order2:=xlAscending, Key3:=Range("R13") _
, Order3:=xlAscending, Header:= _
xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:=xlSortNormal
'Sort CANC >
Range("B14").Select
ActiveSheet.Protect , DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFiltering:=True
Sheets("MCS").Select
Application.CutCopyMode = False
Selection.EntireRow.Delete 'Delete Cancelled contracts on MCS
Sheets("MCS").ShowAllData
Range("AO14").Select '(Last Sales Invoice Number)
ErrHandler:
Application.CutCopyMode = False
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
Thanks
Jon
Last edited by a moderator: