Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default do until loop

I am using excel 2007.

I have a range of cells in sheet2. The range is A1:E300. The cells
contain numbers. Some cells have the same numbers in them.

I have 3 buttons on a sheet.

The first button put a value from a random cell on sheet2 into cell A9
on sheet 1.
Here is the code for button 1

Private Sub Number1_Click()

Sheet1.Range("A9").Value = Sheet2.Cells(Int(Rnd * 300) + 1, Int(Rnd * 5)
+ 1)

End Sub


The second button puts a value from a random cell on sheet2 in B9 on
sheet 1 but I do not want A9 to equal B9. This seems to work.

Here is my code

Private Sub Number2_Click()

Do

Sheet1.Range("B9").Value = Sheet2.Cells(Int(Rnd * 300) + 1, Int(Rnd * 5)
+ 1)

Exit Do

Loop Until Sheet1.Range("A9").Value < Sheet1.Range("B9").Value

End Sub


The third button puts a value from a random cell on sheet2 in C9 on
sheet 1. But I do not want C9 to equal A9 and B9. This part does not work.

Here is my code

Do

Sheet1.Range("C9").Value = Sheet2.Cells(Int(Rnd * 300) + 1, Int(Rnd * 5)
+ 1)

Exit Do

Loop Until Sheet1.Range("C9").Value < Sheet1.Range("B9").Value And
Sheet1.Range("C9").Value < Sheet1.Range("A9").Value

End Sub


Any help would be greatly appreciated

sc


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default do until loop

Steve,

You have the right idea with Do Loops but the Exit Do is stopping the code
looping prematurely. Remove the line

Exit Do

from buttons 2 & 3 and they will work fine.

Mike

"Steve" wrote:

I am using excel 2007.

I have a range of cells in sheet2. The range is A1:E300. The cells
contain numbers. Some cells have the same numbers in them.

I have 3 buttons on a sheet.

The first button put a value from a random cell on sheet2 into cell A9
on sheet 1.
Here is the code for button 1

Private Sub Number1_Click()

Sheet1.Range("A9").Value = Sheet2.Cells(Int(Rnd * 300) + 1, Int(Rnd * 5)
+ 1)

End Sub


The second button puts a value from a random cell on sheet2 in B9 on
sheet 1 but I do not want A9 to equal B9. This seems to work.

Here is my code

Private Sub Number2_Click()

Do

Sheet1.Range("B9").Value = Sheet2.Cells(Int(Rnd * 300) + 1, Int(Rnd * 5)
+ 1)

Exit Do

Loop Until Sheet1.Range("A9").Value < Sheet1.Range("B9").Value

End Sub


The third button puts a value from a random cell on sheet2 in C9 on
sheet 1. But I do not want C9 to equal A9 and B9. This part does not work.

Here is my code

Do

Sheet1.Range("C9").Value = Sheet2.Cells(Int(Rnd * 300) + 1, Int(Rnd * 5)
+ 1)

Exit Do

Loop Until Sheet1.Range("C9").Value < Sheet1.Range("B9").Value And
Sheet1.Range("C9").Value < Sheet1.Range("A9").Value

End Sub


Any help would be greatly appreciated

sc



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default do until loop

Mike H wrote:
Steve,

You have the right idea with Do Loops but the Exit Do is stopping the code
looping prematurely. Remove the line

Exit Do

from buttons 2 & 3 and they will work fine.

Mike

"Steve" wrote:

I am using excel 2007.

I have a range of cells in sheet2. The range is A1:E300. The cells
contain numbers. Some cells have the same numbers in them.

I have 3 buttons on a sheet.

The first button put a value from a random cell on sheet2 into cell A9
on sheet 1.
Here is the code for button 1

Private Sub Number1_Click()

Sheet1.Range("A9").Value = Sheet2.Cells(Int(Rnd * 300) + 1, Int(Rnd * 5)
+ 1)

End Sub


The second button puts a value from a random cell on sheet2 in B9 on
sheet 1 but I do not want A9 to equal B9. This seems to work.

Here is my code

Private Sub Number2_Click()

Do

Sheet1.Range("B9").Value = Sheet2.Cells(Int(Rnd * 300) + 1, Int(Rnd * 5)
+ 1)

Exit Do

Loop Until Sheet1.Range("A9").Value < Sheet1.Range("B9").Value

End Sub


The third button puts a value from a random cell on sheet2 in C9 on
sheet 1. But I do not want C9 to equal A9 and B9. This part does not work.

Here is my code

Do

Sheet1.Range("C9").Value = Sheet2.Cells(Int(Rnd * 300) + 1, Int(Rnd * 5)
+ 1)

Exit Do

Loop Until Sheet1.Range("C9").Value < Sheet1.Range("B9").Value And
Sheet1.Range("C9").Value < Sheet1.Range("A9").Value

End Sub


Any help would be greatly appreciated

sc



Thanks. That did the trick.
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
returning back to loop check condition without completing the loop ashish128 Excel Programming 13 April 3rd 08 12:53 PM
Loop to Filter, Name Sheets. If Blank, Exit Loop ryguy7272 Excel Programming 3 February 5th 08 03:41 PM
Naming Worksheets - Loop within a loop issue klysell Excel Programming 0 March 27th 07 11:17 PM
(Complex) Loop within loop to create worksheets klysell Excel Programming 1 March 20th 07 12:03 AM
Advancing outer Loop Based on criteria of inner loop ExcelMonkey Excel Programming 1 August 15th 05 05:23 PM


All times are GMT +1. The time now is 09:27 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"