I've constructed a Code that is fired off from the click of a Command Button. The Button with the associated code is on the "front sheet" of my workbook and runs a succession of copy / paste activities on another worksheet (Sheet 12 - "Clash Check").
If I have the sheet "Clash Check" active and run the code from the open VBA code editor by clicking the Run button on the ribbon, the code runs perfectly. However, if I then close the VAB editor, go to the front sheet and click on the Command Button the code fails at "Sheets("Clash Check").Range("V2:W2").Select" with 1004 "Method of range class failed".
Why should this run with the Clash Sheet active, but not when selected from the Command Button?
Private Sub Run_Clash_Check_Click()
Application.ScreenUpdating = False
Dim LastRow As Long
Dim destRng As Range
Application.ScreenUpdating = False
Sheets("Clash Report").Range("A2:B600").Clear
Sheets("Clash Check").Range("V2:W600").Clear
Sheets("Clash Check").Range("O2:P600").Copy
Sheets("Clash Check").Range("V2:W600").PasteSpecial xlPasteValues
Sheets("Clash Check").Range("V2:W600").RemoveDuplicates Columns:=1, Header:=x1No
Sheets("Clash Check").Range("V2:W2").Select
Selection.ClearContents
Selection.Delete Shift:=xlUp
Sheets("Clash Check").Range("S2:T600").Copy
Sheets("Clash Check").Range("Y1:Z600").PasteSpecial xlPasteValues
Sheets("Clash Check").Range("Y1:Z600").RemoveDuplicates Columns:=1, Header:=x1No
Sheets("Clash Check").Range("Y1:Z1").Select
Selection.ClearContents
Selection.Delete Shift:=xlUp
With Sheets("Clash Check")
Set destRng = .Range("V" & .Cells(.Rows.Count, "V").End(xlUp).Row + 1)
LastRow = Sheets("Clash Check").Range("Y1:Z" & Rows.Count).End(xlUp).Row
Sheets("Clash Check").Range("Y1:Z1" & LastRow).Copy Destination:=destRng
.Columns("V:W").AutoFit
End With
Sheets("Clash Check").Range("V2:W600").Copy
Sheets("Clash Report").Range("A2:B600").PasteSpecial xlPasteValues
Sheets("Clash Report").Activate
Sheets("Clash Report").Range("A1").Select
Application.ScreenUpdating = True
End Sub
This bit of code appears to be necessary as the remove duplicates leaves a blank cell in the first row (row 2) of the paste area. (I guess that's because there are several blanks in the original list and it leaves one and removes the rest as being duplicates). Having a blank in the top row means that finding the last row for the next lot of paste items doesn't work. However, if I remove the blank all's good for the next paste.
PS. I appreciate the Code is a bit messy, but it's been built over time with trial and error!!
Thanks
Frankie
Bookmarks