select随机位置的单元格并删除vba

我几个月前才开始编码。 我试图按降序排列随机数字。 在我select的路线中,我试图find一个简单的代码来select一个具有特定值(列中的最小数字)的单元格,而不pipe它在列中的位置。

先谢谢你。

您可以使用Sortfunction按升序或降序对列进行sorting。

下面是以Column A为例的代码:

 Sub OrderColumnDescending() Dim lastrow As Long 'If you change the column change the 1 with the number 'of your column to order lastrow = Cells(Rows.Count, 1).End(xlUp).Row Range("A1:A" & lastrow).Sort key1:=Range("A1:A" & lastrow), _ order1:=xlDescending, Header:=xlNo End Sub 

既然你说你是VBA新手,下面是关于代码的一些解释。

 lastrow = Cells(Rows.Count, 1).End(xlUp).Row 

这一行得到列号1中最后一次使用的行,我将这个值放在lastrowvariables中,以便稍后使用它。 它比固定值更好,比每次写入整个事物更可读。

 Range("A1:A" & lastrow).Sort key1:=Range("A1:A" & lastrow), _ order1:=xlDescending, Header:=xlNo 

我创build一个Range对象,其中包含从A1到A的单元格,其中X是我们之前计算的列A中使用的最后一行。 从这个范围我调用它的Sort方法,并给出它的Key1Order1Header参数。

这里是有关Range.Sort方法的有用链接。

MSDN – Range.Sort方法

堆栈溢出 – VBA Excel按特定列sorting范围