Thread: VB
View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default VB

Bob,

The problem is that you have the code in a Sheet module rather
than a general code module. In a general code module, unqualified
range references "roll up" through ActiveSheet and
ActiveWorkbook. Therefore, your line of code, if it were in a
general module, would be equivalent to

ActiveWorkbook.ActiveSheet.Range("A1").Select

However, in a Sheet module, unqualified range references "roll
up" through that particular sheet, not the ActiveSheet.
Therefore, your code, in the module for Sheet1, for example, is
the same as

ThisWorkbook.Worksheets("Sheet1").Range("A1").Sele ct

But you just made another sheet active, so this line of code
fails when you attempt to select a cell that is not on the active
worksheet.

Change your code to

Sheets("Forecast").Select
Sheets("Forecast").Range("A1").Select


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com




"Bob" wrote in message
...
Why do I get an error message with this code in Excel?
There is something wrong in this line: Range("A1").Select


Private Sub CommandButton1_Click()
Sheets("Forecast").Select
Range("A1").Select
End Sub