VBA help

bcs0262cop

New Member
Joined
May 22, 2023
Messages
8
Office Version
  1. 365
Platform
  1. Windows
I have two sheets in a workbook. Sheet A contains approximately 50 names with about 100 unique numbers associated with each name. Sheet B has a cell with a dropdown which lists each name from sheet A. I would like a VBA code which when a name in sheet B is selected it will randomly select a number from sheet A which is associated with that name.

I currently use a function but with the function it changes every time something changes on the sheet
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
@bcs0262cop You have not shared your data ranges so the below is full of assumption.
Hopefully it is of some value?
Tested on limited data ranges.
AutoTraining.xlsm
ABCDEFGHIJKLMNOP
1Name#1#2#3#4#5#6#7#8#9#10#11#12#13#14
2Name _19991000100110021003100410051006100710081009101010111012
3Name _22324252627282930313233343536
4Name _388899091929394959697
5Name _476777879808182
6
SheetA


AutoTraining.xlsm
ABC
1NameNumber
2Name _225
3Name _388
4Name _229
5Name _390
6Name _234
7Name _397
8Name _482
9Name _11002
10Name _11011
11
12
SheetB
Cells with Data Validation
CellAllowCriteria
A2:A12List=SheetA!$A$2:$A$20


Code for code pane of SheetB
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim wsA As Worksheet
Dim wsB As Worksheet
Dim NEntRng As Range
Dim NListRng As Range
Dim NumRng As Range
Dim NRow As Integer
Dim ROset As Integer
Dim RNum As Integer
Dim lr As Integer
Dim lrand As Integer

'Name list and number data sheet
Set wsA = Sheets("SheetA")       '<<<Edit sheet name to suit

'Valid range for name entry in THIS sheet, SheetB
Set NEntRng = Range("A2:A55")

'Check valid single cell entry
If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, NEntRng) Is Nothing Then Exit Sub
If Target = "" Then Exit Sub

'Otherwise
'last row of
lr = wsA.Range("A" & Rows.Count).End(xlUp).Row

'Range of name list
Set NListRng = wsA.Range("A2:A" & lr)  '<<< Edit range to suit

'Get row of entered name
 ' +1 below assumes row 1 of sheetA is header row and names start row 2  Edit to suit
NRow = WorksheetFunction.Match(Target, NListRng, 0) + 1
lrand = wsA.Cells(NRow, Columns.Count).End(xlToLeft).Column - 1

'Random number for picking  a column for number
NCol = WorksheetFunction.RandBetween(1, lrand) + 1
'get number for entered name
Application.EnableEvents = False
Target.Offset(0, 1) = wsA.Cells(NRow, NCol).Value
Application.EnableEvents = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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