1

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
  • VANCOUVER, BC

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

So I’m using the following function:

=INDEX(DATA!$E$2:$E$4576,RANDBETWEEN(1,4574),1)

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:

=RIGHT(A1,2)

For the City:

=LEFT(A1,LEN(A1)-4)

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

I can’t select the city using:

=LEFT(INDEX(DATA!$E$2:$E$4576,RANDBETWEEN(1,4574),1),LEN(INDEX(DATA!$E$2:$E$4576,RANDBETWEEN(1,4574),1))-4)

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
A1=INDEX(DATA!$E$2:$E$4576,RANDBETWEEN(1,4574),1)

//pull the province/state form A1
B1=RIGHT(A1,2)

//pull the city name from cell A1
C1=LEFT(A1,LEN(A1)-4)

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!

4

2 回答 2

2

试试这个公式得到一个没有省份的随机城市

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

注意:您有 4575 行数据,因此您需要 RANDBETWEEN 上升到 4575 否则您不考虑最后一行

该公式将逗号替换为 99 个空格,然后取前 99 个字符并删除空格 - 这是一种无需再次使用 RANDBETWEEN 即可获得 City 的方法。

现在假设您的城市都不同,您可以使用 VLOOKUP 使用通配符查找该城市并获取省份,例如在 A2 中使用上述公式在 B2 中使用此公式

=RIGHT(VLOOKUP(A2&",*",DATA!$E$2:$E$4576,1,0),2)

如果城市可能有重复,那么您可以在 B2 中使用此“数组公式”

=RIGHT(INDEX(DATA!$E:$E,SMALL(IF(LEFT(DATA!$E$2:$E$4576,LEN(A2)+1)=A2&",",ROW(DATA!$E$2:$E$4576)),RANDBETWEEN(1,COUNTIF(DATA!$E$2:$E$4576,A2&",*")))),2)

用 CTRL+SHIFT+ENTER CTRL+ SHIFT+确认ENTER

您不能保证第二个公式返回的省份与第一个公式中选择的单元格中的省份相同(除非该城市没有重复项),但这并不重要。如果有重复的城市,它将随机选择一个省份

于 2013-09-25T19:29:06.997 回答
1

这是不可能的,因为公式 RANDBETWEEN () 是在每个单元格中独立计算的。

因此,要获得相同的随机值,请照常进行。

于 2013-09-25T19:23:31.023 回答