【问题标题】:select cell in random location and delete vba在随机位置选择单元格并删除 vba
【发布时间】:2020-01-23 20:28:11
【问题描述】:

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

提前谢谢你。

【问题讨论】:

  • 查看 SMALL 函数。
  • I am trying to arrange random numbers in a column in descending order 查看.Sort。要查找范围内的最小数字并将其删除,请使用.Find
  • 到目前为止你尝试了什么?
  • 请阅读> How to Ask
  • @SamanthaTonog 如果我的回答如您所愿,请点击赞成/反对票下方的按钮接受它!谢谢你:)

标签: excel vba


【解决方案1】:

您可以使用Sort 函数按升序或降序对列进行排序。

以下是订购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 列中最后使用的行,我将值放在 lastrow 变量中,以便以后使用。它比固定值更好,并且比每次都写整个东西更具可读性。

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

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

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

MSDN - Range.Sort Method

Stack Overflow - VBA Excel sort range by specific column

【讨论】:

    猜你喜欢
    • 2018-01-25
    • 2020-01-27
    • 2019-12-14
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多