Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Strange range behavior (example from Help doesn't work)

Hi Konrad,

Your code will work if the sheet Blad3 is the active sheet. In case it is
not the active sheet, try:

With Worksheets("Blad3")
.Range(.Cells(1, 1), .Cells(3, 2)).Font.Italic = True
End With



---
Regards,
Norman



"Konrad Viltersten" wrote in message
...
I ran the following example from the Help.

Worksheets("Blad3").Range(Cells(1, 1), Cells(3, 2)).Font.Italic = True

To my astonishment i get an error - 1004. The description
is rather vague and i have no idea how to shoot down
this one. Any suggestions?

It should be said that the following code does work as
it's supposed to.

Worksheets("Blad3").Range("A1:B3").Font.Italic = True

Now, for a number of reasons i need to use the syntax
from the first Range-way. How can i do that?

--
Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sence to be lazy
---------------------------------------------------



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 266
Default Strange range behavior (example from Help doesn't work)

"Konrad Viltersten" skrev i en meddelelse
...
I ran the following example from the Help.

Worksheets("Blad3").Range(Cells(1, 1), Cells(3, 2)).Font.Italic = True

To my astonishment i get an error - 1004. The description
is rather vague and i have no idea how to shoot down
this one. Any suggestions?

It should be said that the following code does work as
it's supposed to.

Worksheets("Blad3").Range("A1:B3").Font.Italic = True

Now, for a number of reasons i need to use the syntax
from the first Range-way. How can i do that?

--
Vänligen
Konrad
---------------------------------------------------



Hej Konrad

Worksheets("Blad3") is probably different from the active sheet,
and Cells() (without a full stop) points to the active sheet.
To get it to work, you have to do something like this:

With Worksheets("Blad3")
.Range(.Cells(1,1),.Cells(3,2)).Font.Italic = True
End With


--
Venlig hilsen
Leo Heuser

Followup to newsgroup only please.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default Strange range behavior (example from Help doesn't work)

I ran the following example from the Help.

Worksheets("Blad3").Range(Cells(1, 1), Cells(3, 2)).Font.Italic = True

To my astonishment i get an error - 1004. The description
is rather vague and i have no idea how to shoot down
this one. Any suggestions?

It should be said that the following code does work as
it's supposed to.

Worksheets("Blad3").Range("A1:B3").Font.Italic = True

Now, for a number of reasons i need to use the syntax
from the first Range-way. How can i do that?

--
Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sence to be lazy
---------------------------------------------------

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Strange range behavior (example from Help doesn't work)

Hi Konrad,
]
Where is your code placed?

---
Regards,
Norman



"Konrad Viltersten" wrote in message
...
I ran the following example from the Help.

Worksheets("Blad3").Range(Cells(1, 1), Cells(3, 2)).Font.Italic =
True To my astonishment i get an error - 1004. The description
is rather vague and i have no idea how to shoot down
this one. Any suggestions?


Your code will work if the sheet Blad3 is the active sheet. In case
it is not the active sheet, try:

With Worksheets("Blad3")
.Range(.Cells(1, 1), .Cells(3, 2)).Font.Italic = True
End With



Sorry, no such luck. The worksheet _IS_ active. Just to be
sure i tested the suggested solution but to no avail. The very
same error occured.

Using Cells i can get do the changes i wish to see but i need
to do them in a larger blocks so Range(Cells(,),Cells(,)) would
be very useful.

--
Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sence to be lazy
---------------------------------------------------



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Strange range behavior (example from Help doesn't work)

Hi Konrad,

Providing that I use fully qualified range references, as suggested in both
Leo's response and mine, I am unable to replicate your error.


---
Regards,
Norman



"Konrad Viltersten" wrote in message
...
I ran the following example from the Help.

Worksheets("Blad3").Range(Cells(1, 1), Cells(3, 2)).Font.Italic =
True To my astonishment i get an error - 1004. The description
is rather vague and i have no idea how to shoot down
this one. Any suggestions?


Your code will work if the sheet Blad3 is the active sheet. In case
it is not the active sheet, try:

With Worksheets("Blad3")
.Range(.Cells(1, 1), .Cells(3, 2)).Font.Italic = True
End With



Sorry, no such luck. The worksheet _IS_ active. Just to be
sure i tested the suggested solution but to no avail. The very
same error occured.

Using Cells i can get do the changes i wish to see but i need
to do them in a larger blocks so Range(Cells(,),Cells(,)) would
be very useful.

--
Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sence to be lazy
---------------------------------------------------





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default Strange range behavior (example from Help doesn't work)

I ran the following example from the Help.

Worksheets("Blad3").Range(Cells(1, 1), Cells(3, 2)).Font.Italic =
True To my astonishment i get an error - 1004. The description
is rather vague and i have no idea how to shoot down
this one. Any suggestions?


Your code will work if the sheet Blad3 is the active sheet. In case
it is not the active sheet, try:

With Worksheets("Blad3")
.Range(.Cells(1, 1), .Cells(3, 2)).Font.Italic = True
End With



Sorry, no such luck. The worksheet _IS_ active. Just to be
sure i tested the suggested solution but to no avail. The very
same error occured.

Using Cells i can get do the changes i wish to see but i need
to do them in a larger blocks so Range(Cells(,),Cells(,)) would
be very useful.

--
Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sence to be lazy
---------------------------------------------------

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default Strange range behavior (example from Help doesn't work)

Hi Konrad,
Where is your code placed?



I don't know how to answer this, mostly because of my
ignorance. What i can tell you is how i went about to
put in the code, maybe that will shed some light...

In Excel i go for Tools - Macro - VB Editor. In there
i simply put in those line of code.

Public Sub Test2()
Dim rSelected As Range
Set rSelected = Selection
'rSelected.Cells(1, 1).Font.Italic = True
With Worksheets("Blad3").Range(Cells(1, 1), Cells(3, 2)).Font.Italic = True
End With
'Worksheets("Blad3").Range("A1:B3").Font.Italic = True
End Sub

While the (for now) commented rows do work rather
well, the Range(Cells,Cells) performs badly, resulting
in the error described.

What can be done to kill it?

--
Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sence to be lazy
---------------------------------------------------

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Strange range behavior (example from Help doesn't work)

Hi Konrad,

You are not using the code suggested by Leo and by me.

Use copy / paste to replace your code with:

'=============
Public Sub Test2()
Dim rSelected As Range
Set rSelected = Selection
'rSelected.Cells(1, 1).Font.Italic = True
With Worksheets("Blad3")
.Range(.Cells(1, 1), .Cells(3, 2)).Font.Italic = True
End With
End Sub
'<<=============

In the lines:

With Worksheets("Blad3")
.Range(.Cells(1, 1), .Cells(3, 2)).Font.Italic = True
End With

note the periods (full stops) in front of each instance of Cells. The above
lines are synonymous with:

Worksheets("Blad3").Range(Worksheets("Blad3").Cell s(1, 1), _
Worksheets("Blad3").Cells(3, 2)).Font.Italic = True

In the absence of these periods, each Cells reference will be unqualified.
If your code were resident in a standard code module, such unqualified
references would be implicitly converted by VBA to refer to the active
sheet. In this case, your code snippet:

Worksheets("Blad3").Range(Cells(1, 1), Cells(3, 2))

would be interpreted as:

Worksheets("Blad3").Range(ActiveSheet.Cells(1, 1), _
ActiveSheet.Cells(3, 2))

If the active sheet is not the Blad3 sheet, this will cause the encountered
run-time error, since it is impossible to construct a range on the Blad3
sheet using cells from another sheet.

If, however, the code were resident in a worksheet code module, then VBA
would implicitly convert the unqualified references to refer to the sheet
holding the code. If the sheet holding the code were not the Blad3 sheet,
you would again encounter the 1004 error as VBA would fail in the attempt
to construct the range on Blad3 with constituent cells drawn from the sheet
holding the code.

In either event the answer is to qualify your ranges.

Note that such code should be placed in a standard code module:
Alt- F11 to open the Visual Basic Editor (VBE)
Insert | Module

As a rule of thumb, sheet modules should be reserved for sheet event code.

If you are still experiencing problems, you may send me your workbook:

norman_jones@NOSPAMbtconnectDOTcom
(Delete "NOSPAM" and replace "DOT" with a full stop [period] )


---
Regards,
Norman



"Konrad Viltersten" wrote in message
...
Hi Konrad,
Where is your code placed?



I don't know how to answer this, mostly because of my
ignorance. What i can tell you is how i went about to
put in the code, maybe that will shed some light...

In Excel i go for Tools - Macro - VB Editor. In there
i simply put in those line of code.

Public Sub Test2()
Dim rSelected As Range
Set rSelected = Selection
'rSelected.Cells(1, 1).Font.Italic = True
With Worksheets("Blad3").Range(Cells(1, 1), Cells(3, 2)).Font.Italic =
True
End With
'Worksheets("Blad3").Range("A1:B3").Font.Italic = True
End Sub

While the (for now) commented rows do work rather
well, the Range(Cells,Cells) performs badly, resulting
in the error described.

What can be done to kill it?

--
Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sence to be lazy
---------------------------------------------------



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default Strange range behavior (example from Help doesn't work)

You are not using the code suggested by Leo and by me.
Use copy / paste to replace your code with:
Public Sub Test2()
Dim rSelected As Range
Set rSelected = Selection
'rSelected.Cells(1, 1).Font.Italic = True
With Worksheets("Blad3")
.Range(.Cells(1, 1), .Cells(3, 2)).Font.Italic = True
End With
End Sub

In the lines:
With Worksheets("Blad3")
.Range(.Cells(1, 1), .Cells(3, 2)).Font.Italic = True
End With
note the periods (full stops) in front of each instance of Cells. The
above lines are synonymous with:
Worksheets("Blad3").Range(Worksheets("Blad3").Cell s(1, 1), _
Worksheets("Blad3").Cells(3, 2)).Font.Italic = True


It looks like that was it. I haven't tried that out on the
larger piece of source code but the issue i had has been
resolved. Thank you very much for your time.

--
Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sence to be lazy
---------------------------------------------------

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange behavior Ritchie Excel Discussion (Misc queries) 2 September 26th 06 02:21 AM
VBA - Strange behavior MVM Excel Programming 0 June 12th 06 07:44 PM
Strange TAB behavior m davidson Excel Programming 4 May 28th 06 06:09 PM
Strange behavior. Wiley Coyote Excel Discussion (Misc queries) 7 October 18th 05 04:35 PM
Strange Range.item behavior? Jeremy Gollehon[_2_] Excel Programming 5 April 16th 05 12:53 AM


All times are GMT +1. The time now is 11:47 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"