ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Obj Variable or withblock variable not set? (https://www.excelbanter.com/excel-programming/405929-obj-variable-withblock-variable-not-set.html)

davegb[_2_]

Obj Variable or withblock variable not set?
 
This is the code:

Private Sub CommandButton1_Click()
Const PWORD As String = "dave"
Dim wksSummary As Worksheet

wksSummary.Unprotect password:=PWORD <---- OBJ VAR NOT SET
Range("B:C").Select
Selection.ClearContents
Range("A1").Select


End Sub

This worked fine in a previous macro, but not today. Any ideas why
it's not recognizing wksSummary or password? Or is it something else?

Jim Cone

Obj Variable or withblock variable not set?
 

You haven't told Excel what wksSummary refers to...
Set wksSummary = ActiveSheet
-or-
Set wksSummary = Worksheets("Summary").

However, if CommandButton1 is on Summary sheet and the
code is in that sheet's module then this will work...
("Me" is a reference to the sheet containing the module)
'--
Private Sub CommandButton1_Click()
Const PWORD As String = "dave"
Me.Unprotect password:=PWORD
Me.Range("B:C").ClearContents
Me.Range("A1").Select
End Sub
'--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)



"davegb"
wrote in message
This is the code:
Private Sub CommandButton1_Click()
Const PWORD As String = "dave"
Dim wksSummary As Worksheet

wksSummary.Unprotect password:=PWORD <---- OBJ VAR NOT SET
Range("B:C").Select
Selection.ClearContents
Range("A1").Select
End Sub

This worked fine in a previous macro, but not today. Any ideas why
it's not recognizing wksSummary or password? Or is it something else?

davegb[_2_]

Obj Variable or withblock variable not set?
 
On Feb 11, 1:51*pm, "Jim Cone" wrote:
You haven't told Excel what wksSummary refers to...
Set wksSummary = ActiveSheet
-or-
Set wksSummary = Worksheets("Summary").

However, if CommandButton1 is on Summary sheet and the
code is in that sheet's module then this will work...
("Me" is a reference to the sheet containing the module)
'--
Private Sub CommandButton1_Click()
Const PWORD As String = "dave"
Me.Unprotect password:=PWORD
Me.Range("B:C").ClearContents
Me.Range("A1").Select
End Sub
'--
Jim Cone
San Francisco, USAhttp://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)

"davegb"
wrote in message
This is the code:
Private Sub CommandButton1_Click()
Const PWORD As String = "dave"
Dim wksSummary As Worksheet

wksSummary.Unprotect password:=PWORD <---- OBJ VAR NOT SET
Range("B:C").Select
Selection.ClearContents
Range("A1").Select
End Sub

This worked fine in a previous macro, but not today. Any ideas why
it's not recognizing wksSummary or password? Or is it something else?


Doh! Thanks, Jim.


All times are GMT +1. The time now is 01:28 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com