Copy last formatted cell into another cell in the sheet

MikeyL75

New Member
Joined
Jul 29, 2014
Messages
10
Hi,

This sheet is to perform a raffle, with people's names assigned 1 to 100 in the range TargetRg. It colors random cells orange as a build up, then selects a last random cell and colors it green.

The code I'm currently using is below. It works exactly how I want it to, until the last part. I want the last random cell that the code colors to then be copied to D17. I've tried various methods online and nothing seems to be working.

Help please! :)


Code:
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Function RandCell(Rg As Range) As Range
    Set RandCell = Rg.Cells(Int(Rnd * Rg.Cells.Count) + 1)
End Function


Sub RandCellTest()
Dim Counter As Long
Dim TargetRg As Range, Cell As Range

Set TargetRg = Range("B5:K14")

    TargetRg.Interior.Color = RGB(255, 255, 255)

For Counter = 1 To 50
    Set Cell = RandCell(TargetRg)
    Cell.Interior.Color = RGB(255, 165, 0)
    'Cell.Interior.Color = RGB(Int((255 * Rnd)), Int((255 * Rnd)), Int((255 * Rnd)))   'if I want a random color
    Sleep (250)
    Cell.Interior.Color = xlColorIndexNone
Next
    Set Cell = RandCell(TargetRg)
    Cell.Interior.Color = RGB(34, 139, 34)
        
    Worksheets("Generator").Range("D17").Value = ActiveCell.Value   'need this to copy the cell colored above, and paste it to D17.
        
End Sub
 
Last edited:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi there. With your code as it is, the activecell is not the one youve just coloured. Try modifying the last line to read:
Code:
    Worksheets("Generator").Range("D17").Value = Cell.Value   'need this to copy the cell colored above, and paste it to D17.
 
Upvote 0
That worked perfectly, thanks both.

Another issue I appear to be running into, it seems the cell that is selected at the end of the counter always ends up the same. Is there a way to further randomise it?

Thanks in advance
 
Upvote 0
I'm not sure why - in my copy of the code it returns a random one each time.
 
Upvote 0
Sorry, I should clarify. It does return a random one each time, however it always follows the same pattern after I run it from opening the sheet. Cell 93 first, then Cell 75, 79 etc.
 
Upvote 0
OK, sorry. Put a Randomize statement in your rand routine like this:
Code:
Function RandCell(Rg As Range) As Range
Randomize
Set RandCell = Rg.Cells(Int(Rnd * Rg.Cells.Count) + 1)
End Function
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,734
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top