Randomization Macro

DixitCorvus

New Member
Joined
Jun 1, 2018
Messages
3
I am trying to build a macro that does a couple different random selections. The first function would be to randomly select a name from a table on another tab; this portion is working perfectly fine. The second portion is a randomly generated Yes/No. Here's what I've got so far:

Sub NewCast()
Dim NPC_Row As Double
Dim NPC_Shapeshift As Double
Dim NPC_Shapeshift_Result As String


Randomize
NPC_Row = Int((Rnd() * (Sheets("NPCs").UsedRange.Rows.Count - 1)) + 1)
NPC_Name = Sheets("NPCs").Range("A" & NPC_Row)
Lastrow_NPC = Sheets("NPCs").Range("K1").Value

NPC_Shapeshift = Rnd()
If NPC_Shapeshift > 0.5 Then
NPC_Shapeshift_Result = "Yes"
Else
NPC_Shapeshift_Result = "No"
End If

Sheets("Cast").Range("A" & Lastrow_NPC).Value = NPC_Name
Sheets("Cast").Range("C" & Lastrow_NPC).Value = NPC_Shapeshift_Result


End Sub

Column A of the "Cast" Sheet is the Randomly selected name. Column C is the randomly selected Yes/No value.
LastRow_NPC determines the number of entries already on the "Cast" Sheet telling this macro where to enter the new entry.

Everything looks good to me, but I'm getting an "Application-defined or Object-defined Error" which seems to be associated with the last line:
Sheets("Cast").Range("C" & Lastrow_NPC).Value = NPC_Shapeshift_Result

Any ideas?
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
did you try debugging it? set a breakpoint on this line that errors and make sure all those variables you are using are holding the values you anticipated
 
Upvote 0
I have. Didn't really give me much, though. The Name randomization was holding values, the Yes/No randomization didn't give me any values, so I presume that's not correct. The error doesn't show up until I get to that last line though before End Sub.
 
Upvote 0
Works ok for me, does the Name get inserted in col A on the Cast sheet?
 
Upvote 0
It did before I started trying to add the Yes/No randomization. Now I just get an error box that just says "400".
The issue was that the Yes/No never populated in Column "C".
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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