Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 高亮显示范围中的随机行_Excel_Vba_Random - Fatal编程技术网

Excel 高亮显示范围中的随机行

Excel 高亮显示范围中的随机行,excel,vba,random,Excel,Vba,Random,我试图制作一个宏,按下一个按钮可以高亮显示选择中的一个随机行。我有以下代码(基于@gtwebb慷慨提供给@Kyle Snell的答案): 现在在工作表上,我有一个占据范围(“A1:L5”)的标题,所以我需要代码从标题下包含数据的任何行中随机选择并高亮显示一行。我该怎么做呢?代码仅选择并高亮显示当前包含数据的最后一行 请尝试下面的代码 Sub SelectRandom() Dim mrg As Worksheet Dim Rand As Long Dim Populatio

我试图制作一个宏,按下一个按钮可以高亮显示选择中的一个随机行。我有以下代码(基于@gtwebb慷慨提供给@Kyle Snell的答案):


现在在工作表上,我有一个占据范围(“A1:L5”)的标题,所以我需要代码从标题下包含数据的任何行中随机选择并高亮显示一行。我该怎么做呢?代码仅选择并高亮显示当前包含数据的最后一行

请尝试下面的代码

Sub SelectRandom()
    Dim mrg As Worksheet
    Dim Rand As Long
    Dim PopulationSelect As Range

    Set mrg = ActiveWorkbook.Sheets("Merged")

    'Define your start row below (instead of 6)
    Rand = Application.WorksheetFunction.RandBetween(6, mrg.Range("A" & Rows.Count).End(xlUp).Row)
    mrg.Rows(Rand).EntireRow.Interior.Color = RGB(255, 255, 153)

End Sub

为什么不使用条件格式?
Sub SelectRandom()
    Dim mrg As Worksheet
    Dim Rand As Long
    Dim PopulationSelect As Range

    Set mrg = ActiveWorkbook.Sheets("Merged")

    'Define your start row below (instead of 6)
    Rand = Application.WorksheetFunction.RandBetween(6, mrg.Range("A" & Rows.Count).End(xlUp).Row)
    mrg.Rows(Rand).EntireRow.Interior.Color = RGB(255, 255, 153)

End Sub