VBA Excel 按特定列对范围进行排序 [英] VBA Excel sort range by specific column

查看:40
本文介绍了VBA Excel 按特定列对范围进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以包含任意行数的表格:

I have a table that can contain any number of rows:

正如我所说,它可以包含 1 或 ∞ 行.

As I said it can contain 1 or ∞ rows.

我想按 B 列中的日期单元格对范围 A3:D∞ 进行排序.
我该怎么做?

I want to sort range A3:D∞ by the Date cell that is in column B.
How can I do it?

问题是我不知道如何从A3选择到最后一行.

The problem is that I don't know how to select from A3 to the last row.

我认为循环到最后一行不是一个正确的方法.

I think that looping to the last row is not a correct method.

到目前为止我已经得到了它,它看起来是正确的,但范围是硬编码的.
我如何摆脱范围的硬编码?

I have got this so far it sorts looks like correct, but the range is hard-coded.
How do I get rid of the hard-coding of the range?

Range("A3:D8").Sort key1:=Range("B3:B8"), _
order1:=xlAscending, Header:=xlNo

推荐答案

试试这个代码:

Dim lastrow As Long
lastrow = Cells(Rows.Count, 2).End(xlUp).Row
Range("A3:D" & lastrow).Sort key1:=Range("B3:B" & lastrow), _
   order1:=xlAscending, Header:=xlNo

这篇关于VBA Excel 按特定列对范围进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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