excel - 在 VBA 中查找列并排序
问题描述
您好,我正在尝试创建一个宏,以便更轻松地对从机器人导出的数据进行排序
问题在于,由于机器人进行测试的方式,柱子可能会改变位置
许多列都是无用的,所以我制作了一个宏来隐藏未使用的列,现在我想添加一个宏,以升序对剩余的 4 列进行排序,但我无法破解它
到目前为止我有
Dim c As Range
For Each c In Range("A1:BR1").Cells
If c.Value = "Plate Name (barcode)" Or c.Value = "Measurement Date" Or c.Value = "Measurement profile" Or c.Value = "pH" Or c.Value = "Count" Or c.Value <= 30 Then
c.EntireColumn.Hidden = False
Else: c.EntireColumn.Hidden = True
End If
Next c
End Sub
它隐藏了除命名列之外的每一列,但我无法让它在此之后对数据进行排序
我试图找到列/选择列并排序,但由于某种原因,宏似乎运行而不是实际排序
还尝试录制宏,但是随着列的移动,代码不断定义要排序的列,例如“D:D”或“AB:AB”,但它可能并不总是在这些列中,所以我需要搜索标题然后排序
任何帮助,将不胜感激!
解决方案
看看这样的东西是否适合你......
该代码假定数据集之间没有空白列。
Sub SortColumns()
Dim i As Long, j As Long, lc As Long
Dim vKeys()
lc = ActiveSheet.UsedRange.Columns.Count
For i = lc To 1 Step -1
If Columns(i).Hidden = False Then
j = j + 1
ReDim Preserve vKeys(1 To j)
vKeys(j) = Cells(2, i).Address
End If
Next i
'vKeys(4) --> First visible column from left
'vKeys(3) --> Second visible column from left
'vKeys(2) --> Third visible column from left
'vKeys(1) --> Fourth visible column from left
Range("A1").CurrentRegion.Sort key1:=Range(vKeys(4)), order1:=xlAscending, key2:=Range(vKeys(3)), order2:=xlAscending, key3:=Range(vKeys(2)), order3:=xlAscending, Header:=xlYes
Range("A1").CurrentRegion.Sort key1:=Range(vKeys(1)), order1:=xlAscending, Header:=xlYes
End Sub
推荐阅读
- spring-data-jpa - Spring with Querydsl:空指针异常
- sql - Sql 删除字符串中的子字符串(oracle 11g)
- compilation - 是否有工具可用于将 WebAssembly “组装”为 x86-64 本机代码?
- java - 解释用JAVA写的这一行
- r - grep multiple patterns Or condition on grouping data
- ios - How to set up UIScrollView programatically to scroll horizontally using autoLayout
- php - Laravel中效用函数的含义是什么
- angular - 角度 POS 打印问题
- excel - Delete Certain Words from an Excel Cell based on a list
- python - Creating a dictionary of categoricals in SQL and aggregating them in Python