VBA - how to access distinct elements in a named range

bubba2413

New Member
Joined
Nov 8, 2010
Messages
25
ok..

I'm trying to get the value of a specific elements of a named range in VBA.
thinking something like [RangeName](element) or [RangeName].element, but can't seem to quite get the syntax right. The element must be a random integer between 1 and [RangeName].count. I already have a vba equivalent for the RandBetween worksheet function.

background: I'm working on some data randomization routines to generate test data for a project. I have several lists of valid elements: First Name, Last Name, location, element type, etc.

Lets talk about names as an example. Given a dynamic named range that points to Last Names [LU_LName] with (currently) 5000+ last names.
There is a design requirement is that LookUp (LU) ranges be dynamic to allow for adding/deleting elements without breaking things...

In a spreadsheet view, I can simply enter a formula:
"=index([LU_LName],int(randbetween(1,counta([LU_LName])))"
Which gives me a random element from the LU_LName list.

This works great, but is slow, especially with large data set generation, and changes/recalculates everytime any element on the sheet updates.

Consequently, I'm trying to convert the process to VBA. Today, I can select the number of rows to meet the desired data-set size, and populate the cells with the proper formula. Much faster, especially when I wrap it with screenupdating=off/on and completing with copy used range, paste values.. but still slow with larger data sets (desired data sets can range from 1 to 10's of thousands...)
 

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)
If the named range is one dimensional try like this to get the second element

Code:
x = Range("LU_LName")(2)
 
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,367
Members
449,080
Latest member
Armadillos

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