Bypass runtime error 1004

oO P2K Oo

New Member
Joined
Aug 12, 2018
Messages
25
Office Version
  1. 365
Platform
  1. Windows
Hi All,

This probably sounds a little silly, due to built in functions and security measures to stop Excel from going mad, but I have the error box (image attached) come up, and I was wondering if i could by pass it by adding some code to my existing VBA.

So my code currently finds a set of matches, and then copy and pastes them onto another sheet, then changes the criteria and repeats this. The issue is that I am about half way through the criteria and the runtime error appears. Is there a way to bypass this error, so if there is nothing to copy and paste, it moves on to the next step and tries again until the end of the code?

Extract of code:

VBA Code:
Sheets("Theatre Data").Select
    Rows("2:2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    Application.CutCopyMode = False
    Selection.ClearContents
    ActiveSheet.Range("$A$1:$XFC$15000").AutoFilter Field:=8
    ActiveWorkbook.Worksheets("Theatre Data").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Theatre Data").AutoFilter.Sort.SortFields.Add2 Key _
        :=Range("H1:H15000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Theatre Data").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    Sheets("Episode Workings").Select
    ActiveSheet.Range("$A$1:$J$75000").AutoFilter Field:=1, Criteria1:="8"
    Rows("2:2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.ClearContents
    ActiveSheet.Range("$A$1:$J$75000").AutoFilter Field:=1
    ActiveWorkbook.Worksheets("Episode Workings").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Episode Workings").AutoFilter.Sort.SortFields.Add2 _
        Key:=Range("A1:A75000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Episode Workings").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
        
    Sheets("Theatre Data").Select
    ActiveSheet.Range("$A$1:$XFC$15000").AutoFilter Field:=8, Criteria1:="Yes"
    Rows("2:2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.Copy
    
    Sheets("Matched Theatre Data").Select
    Range("A40000").Select
    ActiveSheet.Paste
    Cells.Select
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("A1").Select

The error appears on the line "ActiveSheet.Paste", right after "Range("A40000").Select

Many thanks in advance for your help.
 

Attachments

  • Runtime Error 1004.PNG
    Runtime Error 1004.PNG
    26.2 KB · Views: 35

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Geesh, could you add any more '.Select's?

Be careful what you ask for, change:

VBA Code:
ActiveSheet.Paste

to:

VBA Code:
On error resume Next
ActiveSheet.Paste
 
Upvote 0
Solution
Geesh, could you add any more '.Select's?

Be careful what you ask for, change:

VBA Code:
ActiveSheet.Paste

to:

VBA Code:
On error resume Next
ActiveSheet.Paste
Are Select's optional??? Newbie to VBA.

Worked like a charm, many thanks johnnyL.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top