VBA选择范围内的随机行 [英] VBA Select Random Row In Range of Select

查看:302
本文介绍了VBA选择范围内的随机行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想在用户选择范围内选择一个随机行。但是,有时程序将选择特定范围之外的随机行。请参阅下面的代码摘录:

  Dim PopulationSelect As Range 
Set PopulationSelect = Application.InputBox(人口,类型:= 8)


RandSample = Int(PopulationSelect.Rows.Count * Rnd +1)
行(RandSample).EntireRow.Select $ b有没有人知道这是为什么,以及如何解决?


$ b



$ b

提前谢谢。

解决方案

这是因为你的随机样本是从1到行数,你的选择功能正在整个工作表上工作。



所以如果populationSelect是A50:A51你的rand样本将是1或2,它会选择第1行或第2行



尝试

  Sub test()
Dim PopulationSelect As Range
Set PopulationSelect = Application.InputBox (选择群体中的单元格范围,类型:= 8)


RandSample = Int(PopulationSelect.Rows.Count * Rnd + 1)
PopulationSelect.Rows RandSample).EntireRow.Select

End Sub


I am simply trying to select a random row within a user selected range. However, at times the program will select a random row outside of the specific range. See code excerpt below:

Dim PopulationSelect As Range
Set PopulationSelect = Application.InputBox("Select range of cells in the population", Type:=8)


RandSample = Int(PopulationSelect.Rows.Count * Rnd +1)
Rows.(RandSample).EntireRow.Select

Does anyone know why this is, and how to fix it?

Thank you in advance.

解决方案

It's because your random sample is going from 1 to the number of rows and your select function is working on the entire sheet.

So if populationSelect is A50:A51 your rand sample would be 1 or 2 and it would select row 1 or 2

Try

Sub test()
Dim PopulationSelect As Range
Set PopulationSelect = Application.InputBox("Select range of cells in the population", Type:=8)


RandSample = Int(PopulationSelect.Rows.Count * Rnd + 1)
PopulationSelect.Rows(RandSample).EntireRow.Select

End Sub

这篇关于VBA选择范围内的随机行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆