简体   繁体   English

仅引用过滤表中的可见行 - Excel VBA

[英]Referencing only visible rows in a filtered table - Excel VBA

I am working on a project for selecting films.我正在做一个选择电影的项目。 It contains a table of films, with columns for the name of the film and other data, such as IMDB ratings etc. I've written a macro which filters the table according to user input (eg minimum IMDB rating), and now I would like to select a random film from the filtered table.它包含一个电影表,其中包含电影名称和其他数据的列,例如 IMDB 评级等。我编写了一个宏,它根据用户输入(例如,最低 IMDB 评级)过滤表,现在我会喜欢 select 从过滤表中随机出片。

I can work out the number of visible films with:我可以计算出可见电影的数量:

FilmCount = FilmsSheet.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 1

And generate a random number from this with:并由此生成一个随机数:

Randomize
FilmNumber = Int(FilmCount * Rnd) + 1

However, selecting a random film from the filtered table is more difficult;但是,从过滤表中选择随机电影比较困难; in a non-filtered table I could simply use the random number to select a film with:在未过滤的表中,我可以简单地将随机数用于 select 电影:

Film = FilmsSheet.Cells(FilmNumber + 1, 1)

But in a filtered table, the rows don't ascend numerically due to the missing filtered rows, for example 1,3,4,7...但在过滤表中,由于缺少过滤行,行不会按数字上升,例如 1,3,4,7...

How can I use the random number to select the corresponding film in the filtered table?怎么用随机数到select过滤表中对应的片子? Thanks.谢谢。

Maybe something like this?也许是这样的?

Assume that the Film Title (header column) is cell C1, and data is already filtered假设 Film Title(标题列)为单元格 C1,并且数据已被过滤

Sub test()
Set rngFilmTitle = Range("C2", Range("C" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
X = Application.Transpose(rngFilmTitle)
Do
rndNum = Int(Rnd() * UBound(X))
Loop
End Sub

If for example the total Film Title (in filtered data) is 4 film title,例如,如果总电影片名(在过滤数据中)是 4 部电影片名,
If you F8 again and again and again in the loop, the rndNum result will give random number between 0 to 3 (see from the Locals Window in VBA editor).如果您在循环中一次又一次地 F8,则 rndNum 结果将给出 0 到 3 之间的随机数(参见 VBA 编辑器中的 Locals Window)。

If you already sure about that, then you can remove the loop,如果你已经确定了,那么你可以删除循环,
then have然后有

rndNum = Int(Rnd() * UBound(X))
FilmTitle = X(rndNum) '---> this will give a random film title.

That's if I'm not mistaken to get what you mean, because your title question seems that you want to get a random from the hidden rows (unfiltered rows), but in the body of your question it seems that you want to get a random from the visible rows (filtered rows)?那就是如果我没有弄错你的意思,因为你的标题问题似乎你想从隐藏的行(未过滤的行)中随机获取,但在你的问题正文中,你似乎想要随机获取从可见行(过滤行)?

Using @BigBen's commented suggestion, I iterated through all the rows in the table, and if they were visible added the film name to an array, from which a random film could be picked:使用@BigBen 的评论建议,我遍历了表中的所有行,如果它们可见,则将电影名称添加到数组中,从中可以选择随机电影:

'=====Select random film from visible=====
Dim FilmCount As Integer: FilmCount = FilmsSheet.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 1
Dim Films() As Variant
ReDim Films(1 To FilmCount)

x = 1
Dim r As Integer: r = 2
Do Until FilmsSheet.Cells(r, 1) = 0
    If Not FilmsSheet.rows(r).EntireRow.Hidden Then
        Films(x) = FilmsSheet.Cells(r, 1)
        x = x + 1
    End If
    r = r + 1
Loop

Randomize
Dim FilmNumber As Integer: FilmNumber = Int(FilmCount * Rnd) + 1

MsgBox (Films(FilmNumber))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM