one script interferes with another

tropdoug

New Member
Joined
Sep 26, 2009
Messages
9
I got this script from this site for colouring cells and it works a treat
Code:
Public OldRng As Range
 
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not OldRng Is Nothing Then
        OldRng.Interior.ColorIndex = xlNone
    End If
    Target.Interior.ColorIndex = 6
    Set OldRng = Target
End Sub

It is placed in the worksheet code. However in a module I also have this script for inserting a new line at the top of my range and then formattiiong it for the data, finally opening up a user form.

Code:
Sub StartNewJob_Click()
Rows("4:4").Select
    Selection.Insert Shift:=xlDown
    Range("A5:M5").Select
    Selection.Copy
    Range("A4").Select
    ActiveSheet.Paste
    Range("A4:H4").Select
    Selection.ClearContents
    Range("J4").Select
    Selection.ClearContents
    Range("L4").Select
    Selection.ClearContents
    Range("A4").Select
 
 
MainRecForm.Show
End Sub

I have found that the second script hangs when it gets to the line " ActiveSheet.Paste" with the error message "Paste method of Worksheet class failed"

each script works when the other is removed.

What do I need to do so as to get them both to work?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Sorry, didn't read the whole message
2nd block of code works for me

Code:
Sub StartNewJob_Click()
Rows("4:4").Insert Shift:=xlDown
    Range("A5:M5").Copy
    Range("A4").PasteSpecial Paste:=xlPasteFormats
    Range("A4").Select
Application.CutCopyMode = False
MainRecForm.Show
End Sub
 
Last edited:
Upvote 0
Just tried it with a form and seems OK too.
 
Upvote 0
Thanks it works well for me too. So that I can learn what was the problem? I am very new to VBA, but I can see you haven't used any 'select' commands, other than the last one and the paste command is cleaner I guess. Can you tell me what the "Activesheet.paste" bit was doing (or not doing rather) and why.

Thanks very much for the help.
 
Upvote 0
Ah just found one issue. If a user uses the add new job macro and then for whatever reason decided to delete the row created, as soon as you try to move cell, this error comes up "Run time error object required"
 
Upvote 0
Yep, I can understand that.
You probably need to rethink your strategy on your worksheet_selection change event.
anytime you record a macro, you can usually delete the "Select.Selection" part and run the lines together.
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,628
Members
452,933
Latest member
patv

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