VBA实现Excel数据排序功能

前言

本节会介绍使用VBA如何实现Excel工作表中数据的排序功能。 本节会通过下表数据内容为例进行实操:


1. Sort 单列排序

语法:Sort key1,Order1
说明:
Key1 :表示需要按照哪列进行排序
Order1 :用来指定是升序xlAscending,还是降序xlDescending
Header:表示是否首行为表头,当等于是xlYes时,则不会对表头行参与到排序中,若为否xlNo,则也会参与排序

示例:

在sheet1中存放着学生的成绩单,先需要将该表数据按照语文成绩进行降序排序,完整的代码如些:

c 复制代码
Sub Sort()
    
    ' 声明工作表变量
    Dim ws As Worksheet
    ' 声明范围数据变量
    Dim Rng As Range
    
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    ' 设置数据范围
    Set Rng = ws.Range("A1:D10")
    
    ' 设置通过语文成绩进行升序排序
    Rng.Sort Key1:="语文", Order1:=xlDescending, Header:=xlYes
    
    
End Sub

运行后截图

相关推荐
wtsolutions3 小时前
Understanding JSON Formats - What JSON to Excel Supports
json·excel
wtsolutions3 小时前
Advanced Features - Unlocking the Power of JSON to Excel Pro
linux·json·excel
fs哆哆5 小时前
VB.NET和VBA教程-如何查找Excel数据区域边界
excel
小矮强5 小时前
Excel中根据年月日提取月日,并按月日进行排序
excel
开开心心_Every5 小时前
图片批量压缩工具:支持有损无损两种模式
python·游戏·微信·django·pdf·excel·语音识别
wtsolutions6 小时前
Real-World Use Cases - How Organizations Use JSON to Excel
json·excel
wtsolutions6 小时前
Introduction to JSON to Excel - The Ultimate Conversion Tool
json·excel
骆驼爱记录7 小时前
Python打包命令全攻略
自动化·word·excel·新人首发
wtsolutions7 小时前
JSON to Excel WPS Add-in - Perfect for WPS Office Users
json·excel·wps
hhzz8 小时前
Springboot项目中使用EasyPOI操作Excel(详细教程系列4/4)
java·spring boot·后端·spring·excel·poi·easypoi