Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default It's very strange! Please review and give me a clue

I coded as this below.
When I run DemoUsedRange, no problem!
But when I add CellNumRandum, the error occured.(Application-defined or
object-defined error)
Please give me some comments.

Thanks.


' My Code

Option Explicit

Sub DemoUsedRange()
Dim cel As Range
Dim str As String

For Each cel In ActiveSheet.UsedRange
If cel.Value < 0 Then str = str & cel.Address & ","
Next

ActiveSheet.Range(Left(str, Len(str) - 1)).Select ' Error
Selection.Font.ColorIndex = 3
End Sub

Sub CellNumRandom()
' Range("A1:J24").Select
Dim cel As Range
For Each cel In ActiveSheet.UsedRange

Randomize

Dim random As Long
random = Int((100 - (-100) * Rnd) - 100)
If random < 30 Then random = -random

cel.Value = random
Next cel
DemoUsedRange ' call upper procedure
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default It's very strange! Please review and give me a clue

Your string is probably too long.

Sub DemoUsedRange()
Dim cel As Range
Dim str As String
Dim rng as range
For Each cel In ActiveSheet.UsedRange
If cel.Value < 0 Then
if rng is nothing then
set rng = cel
else
set rng = union(rng,cel)
end if
End if
Next
if not rng is nothing then
rng.ColorIndex = 3
' if you want to select
rng.Select
end if
End Sub

or

Sub DemoUsedRange()
Dim cel As Range
Dim str As String
For Each cel In ActiveSheet.UsedRange
If cel.Value < 0 Then cell.Interior.ColorIndex = 3
Next
End Sub


--
Regards,
Tom Ogilvy


wrote in message
ups.com...
I coded as this below.
When I run DemoUsedRange, no problem!
But when I add CellNumRandum, the error occured.(Application-defined or
object-defined error)
Please give me some comments.

Thanks.


' My Code

Option Explicit

Sub DemoUsedRange()
Dim cel As Range
Dim str As String

For Each cel In ActiveSheet.UsedRange
If cel.Value < 0 Then str = str & cel.Address & ","
Next

ActiveSheet.Range(Left(str, Len(str) - 1)).Select ' Error
Selection.Font.ColorIndex = 3
End Sub

Sub CellNumRandom()
' Range("A1:J24").Select
Dim cel As Range
For Each cel In ActiveSheet.UsedRange

Randomize

Dim random As Long
random = Int((100 - (-100) * Rnd) - 100)
If random < 30 Then random = -random

cel.Value = random
Next cel
DemoUsedRange ' call upper procedure
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default It's very strange! Please review and give me a clue

Many thanks, Ogilvy!




Tom Ogilvy ÀÛ¼º:

Your string is probably too long.

Sub DemoUsedRange()
Dim cel As Range
Dim str As String
Dim rng as range
For Each cel In ActiveSheet.UsedRange
If cel.Value < 0 Then
if rng is nothing then
set rng = cel
else
set rng = union(rng,cel)
end if
End if
Next
if not rng is nothing then
rng.ColorIndex = 3
' if you want to select
rng.Select
end if
End Sub

or

Sub DemoUsedRange()
Dim cel As Range
Dim str As String
For Each cel In ActiveSheet.UsedRange
If cel.Value < 0 Then cell.Interior.ColorIndex = 3
Next
End Sub


--
Regards,
Tom Ogilvy


wrote in message
ups.com...
I coded as this below.
When I run DemoUsedRange, no problem!
But when I add CellNumRandum, the error occured.(Application-defined or
object-defined error)
Please give me some comments.

Thanks.


' My Code

Option Explicit

Sub DemoUsedRange()
Dim cel As Range
Dim str As String

For Each cel In ActiveSheet.UsedRange
If cel.Value < 0 Then str = str & cel.Address & ","
Next

ActiveSheet.Range(Left(str, Len(str) - 1)).Select ' Error
Selection.Font.ColorIndex = 3
End Sub

Sub CellNumRandom()
' Range("A1:J24").Select
Dim cel As Range
For Each cel In ActiveSheet.UsedRange

Randomize

Dim random As Long
random = Int((100 - (-100) * Rnd) - 100)
If random < 30 Then random = -random

cel.Value = random
Next cel
DemoUsedRange ' call upper procedure
End Sub


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
Haven't got a clue!!! DarrenG[_4_] Excel Programming 2 July 24th 06 12:59 PM
Have not a clue how to do this DTTODGG Excel Programming 1 July 17th 06 09:39 PM
Give RELEVANT responses to questions. DO NOT give usless list pmartin Excel Worksheet Functions 2 July 6th 06 06:08 PM
No Clue... tahir Excel Programming 3 March 22nd 06 12:44 PM
entering a26-02 into a cell give strange result Anthony Excel Discussion (Misc queries) 1 January 27th 05 10:34 PM


All times are GMT +1. The time now is 07:16 PM.

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

About Us

"It's about Microsoft Excel"