excel - 尝试更改范围时,RowIndex 的范围不起作用
问题描述
我正在尝试使用此代码选择随机行:
Sub Randomization()
Dim nNumber As Integer, nRowIndex As Integer
'Generate a random number
nNumber = Int(Rnd() * (11 - 2 + 1)) + 2
'Go through the list
For nRowIndex = 2 To 11
If nRowIndex = nNumber Then
MsgBox "The number is " & Cells(nRowIndex, 1).Value
End If
Next nRowIndex
End Sub
但是,当我尝试替换 to 的范围时For nRowIndex
,比如说,31 To 42
宏不起作用。
解决方案
你可以试试这个:
Sub Randomization()
Dim nNumber As Integer, nRowIndex As Integer
'Generate a random number
nNumber = Application.WorksheetFunction.RandBetween(31, 42) 'Change here (Need to be same)
'Go through the list
For nRowIndex = 31 To 42 'Change here (Need to be same)
If nRowIndex = nNumber Then
MsgBox "The number is " & Cells(nRowIndex, 1).Value
End If
Next nRowIndex
End Sub
推荐阅读
- react-native - 使用 react-native-navigation [V2] 时未显示顶部选项卡
- c# - 如何搜索 BindingList 并返回一个对象?
- machine-learning - 多标签分类 Keras 指标
- office-js - 内容加载项实例 ID
- c# - 是否可以根据源中的集合在 dest 上创建集合?
- python - 函数寻找 3 个参数,但只给出了 2 个。不确定问题出在哪里
- python - 如何在 Python 中提取字符串中的前 10 个字符,然后是 2-11,然后是 3-12,依此类推?
- c# - 没有列表权限 Azure Key Vault 'Forbidden' 错误
- java - Azure 中 Glassfish 3.1.2.2 上的远程 EJB
- swift - 无法赋值:“k”是“let”常量