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

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

问题描述

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





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



我想通过列B中的Date单元格对A3范围进行排序:D∞。我该怎么做?



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



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


$ b $我已经得到了这个到目前为止它的排序看起来是正确的,但范围是硬编码。如何摆脱范围的硬编码?

 范围(A3:D8)排序key1:=范围(B3:B8),_ 
order1:= xlAscending,标题:= 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


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

As i said it can contain 1 or ∞ rows.

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

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 hardcoded. How do I get rid of the hardcoding of the range?

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

解决方案

Try this code:

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天全站免登陆