the default worksheet

P

pt

hi,all:
I face a problem, I have a button control in worksheet 2,
and the code as below:

Private Sub CommandButton1_Click()
Worksheets(1).Activate
Cells(1, 1).Value = "kk"
End Sub

and It seems that worksheet 1 is activated ,but kk is
set at the worksheet 2's cell (1,1) not at the worksheet 1.
It seems that the defaultworksheet is not the active worksheet.
It is so strange. Why it is so?

many thanks,
PanTao
 
D

Dave Peterson

If the code is in a general module, then the unqualified range belongs to the
activesheet.

If the code is behind a worksheet (and it is in your case), then the unqualified
ranges belong to the object owning the code -- the sheet with the commandbutton.

Private Sub CommandButton1_Click()
Worksheets(1).Activate 'you don't even need this line!
worksheets(1).Cells(1, 1).Value = "kk"
End Sub
 
P

pt

Oh,I see, it is reasonable, many thanks ,Dave. And I had search the help of
EXCEL VBA
reference, but did't find it, all the explaination I found is that the
unqualified range belongs to
the activesheet. May you tell me Where can I find it?Many Thanks.

PanTao

--------------------------------------
 
D

Dave Peterson

I've never looked for it.

But you can see it yourself.

Change your code (temporarily) to:

Private Sub CommandButton1_Click()
Worksheets(1).Activate
msgbox Cells(1, 1).parent.name
End Sub

(make sure that the worksheet with the button is not worksheets(1), though.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top