Range Error

CW-NEN

Board Regular
Joined
Jul 18, 2007
Messages
55
I have been using this code in a macro for a while. Today I switched it to a command button. Now I get this error:

"Run time error '1004'
Select Method of Range class failed"

Please Help.



Code:
Private Sub CommandButton4_Click()
'   Add City
    Sheets("Budget Price").Select
    ActiveSheet.Unprotect Password:="password"
-->  Range("A21").Select
    Selection.EntireRow.Insert
    Sheets("City List").Select
    Selection.Copy
    Sheets("Budget Price").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveSheet.Protect DrawingObjects:=True, contents:=True, Scenarios:=True, Password:="password"

End Sub

Thanks
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

dave3009

Well-known Member
Joined
Jun 23, 2006
Messages
7,128
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. Mobile
  3. Web
Did you try putting

Code:
ActiveSheet.

In front of it. Better still reference the sheet or use With.

HTH


Dave
 

dave3009

Well-known Member
Joined
Jun 23, 2006
Messages
7,128
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. Mobile
  3. Web
This is a bit cleaner

Code:
Private Sub CommandButton4_Click()
'   Add City
    Sheets("Budget Price").Select
    ActiveSheet.Unprotect "password"
    ActiveSheet.Range("A21").EntireRow.Insert
    Sheets("City List").Copy
    Sheets("Budget Price").PasteSpecial
    Application.CutCopyMode = False
    ActiveSheet.Protect "password"

End Sub
 

dave3009

Well-known Member
Joined
Jun 23, 2006
Messages
7,128
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. Mobile
  3. Web
I'm glad it works for you.

All the very best

Dave
 

Forum statistics

Threads
1,181,102
Messages
5,928,067
Members
436,586
Latest member
latintxn

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
Top