Home » excel » excel – Is it possible to do a LEFT or RIGHT function in conjunction with a LEN on a randomly populated cell of varying length?

excel – Is it possible to do a LEFT or RIGHT function in conjunction with a LEN on a randomly populated cell of varying length?

Posted by: admin May 14, 2020 Leave a comment


Preamble: I have a problem and I’ve got a very simple solution that I’m using. However, I was wondering, for the sake of learning, if any Excel Guru’s out there had an alternate solution to my problem.

I have an excel table with a column that contains about 4500 U.S. and Canadian cities.
Here’s a sample of the way the data in the column is formatted:

  • Toronto, ON
  • Jacksonville, FL

On another sheet in the workbook, I randomly select a City.

So I’m using the following function:


This function produces a random selection from the 4500 cities; the problem is that I want to populate two cells, one that has the City and one that has the Province/State.

Typically, extracting the City/Province from a cell formatted ‘City, Province’ is pretty straight forward.

I’d do something like this:

For the State or Province:


For the City:


My problem lies in the random selection of the city/state.

I can’t select the city using:


Because when I’m calculating the LEN function, it randomly selects another city and uses that length for the calculation. So depending on the combination of random city selections in the one function, some will be short, some will be long (and some will be accurate).

An extension of the same problem is when I do the RIGHT string function to get the state, that’ll be from another randomly selected value, so it won’t (necessarily) match the city.

The simplest thing I could think of, and it’s what I’ve done, is to make the random selection in one cell, then perform the RIGHT and LEFT functions on that cell.

Like this:

//populate cell A1 randomly

//pull the province/state form A1

//pull the city name from cell A1

I’m just curios if there’s another way of doing the LEFT/LEN function on the RANDOM selection without having to play off of another cell. I’d like to avoid VBA if possible. It’s possible there isn’t a solution, but I’m hoping someone who knows more than I do will have some ideas!

How to&Answers:

Try this formula to get a random city without the province

=TRIM(LEFT(SUBSTITUTE(INDEX(DATA!$E$2:$E$4576,RANDBETWEEN(1,4575)),",",REPT(" ",99)),99))

Note: you have 4575 rows of data so you need the RANDBETWEEN to go up to 4575 otherwise you aren’t considering the last row

That formula replaces the comma with 99 spaces then takes the first 99 characters and trims off the spaces – that is one way to get the City only without having to use RANDBETWEEN again.

Now assuming your cities are all different you can use VLOOKUP to lookup that City with a wildcard and get the province, e.g. with the above formula in A2 use this formula in B2


If cities might have duplicates then you can use this “array formula” in B2 instead



You can’t guarantee that the province returned by the second formula was the same as the one from the cell chosen in the first formula (unless the city has no duplicate) but that won’t matter. If there are duplicate cities it will pick one of the provinces at random


It is not possible, because the formula RANDBETWEEN () is calculated independently in each cell.

Thus, to get the same random value, proceed as already doing.