Conditionally adding names within a cell range using button.

wokyen

New Member
Joined
Mar 14, 2016
Messages
2
Hello!

I'm attempting to add names within a small cell range (6 cells) by using a button attached to a particular name (21 buttons attached to unique names). Using the below code, I'm able to add the names within that range, but a name is not always added with a single button press (because of the Rnd function maybe?). In other words, sometimes I have to hit the button several times for the name to show up.

Code:
Sub add_1()

Dim rCells As Range
Set rCells = Range("N2:S2")

Dim rRandom As Long
    rRandom = Int(Rnd * rCells.Cells.Count) + 1

With rCells.Cells(rRandom)
    If IsEmpty(rCells.Cells(rRandom)) Then
        .Value = "Bastion"
    End If
End With

End Sub

I'd like to fix the issue of the button not ALWAYS adding a name (unless the cell has existing data), but it would be nice if the names were added sequentially within the defined range (i.e. clicking the button starts with N2 and moves to the next if there is data already present).

Either way, thanks for the help, and sorry if this was brought up elsewhere.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Re-upping in case this is not one of these situations:

pcl1e8N.png
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,873
Members
449,056
Latest member
ruhulaminappu

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