What would be the best way to do this?

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,375
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am making a spreadsheet that allows you to enter an activity, press a button, then it stores the activity. A list of activities are compiled over time and there is another button that selects a random activity from the list. You can't use variables for this as they only last until the spreadsheet shuts.

What would be the best way to do this, maybe a list box?
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi dpaton05,

Let me know how this goes:

Code:
Option Explicit
Sub Macro2()

    Dim ws As Worksheet
    Dim strSourceCol As String
    Dim lngLowerBound As Long
    Dim lngUpperBound As Long
    Dim lngRandRow As Long
    
    Application.ScreenUpdating = False
    
    Set ws = ThisWorkbook.Sheets("Sheet1") 'Sheet name containing the activity list. Change to suit if necessary.
    strSourceCol = "A" 'Column letter contianing the activity list. Change to suit if necessary.
    lngLowerBound = 2 'Starting row number for the activity list. Change to suit if necessary.
    lngUpperBound = ws.Cells(Rows.Count, strSourceCol).End(xlUp).Row
    
    'The following was adpted from here: _
    https://www.techonthenet.com/excel/formulas/rnd.php
    lngRandRow = CLng((lngUpperBound - lngLowerBound + 1) * Rnd + lngLowerBound)
    
    Application.ScreenUpdating = True
    
    MsgBox ws.Range(strSourceCol & lngRandRow) 'Display the result
   
End Sub

Regards,

Robert
 
Upvote 0
Thanks for the reply Robert, I wanted to make it into an app so instead of randomly selecting an activity from a range, I was thinking of a stand alone way to store the list of activities. What do you think would be the best way to do this?

Dave
 
Upvote 0
I am making a spreadsheet that allows you to enter an activity, press a button, then it stores the activity.

So my code randomly selects a row from the list (once you change the variables to suit) and displays it in a message box. Isn't that what you were after?
 
Upvote 0
You're welcome. I'm glad we were able to provide you with a solution ;)
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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