VBA - 对引用 MergeArea 的单元格进行排序
VBA - Sorting cells referencing MergeArea
我 运行 我的排序代码有问题。我的目标是按地址类型对区域进行排序。每个人都有多个帐户,只要有帐户,名字就会出现在一个合并区域中。所以从"B3:B6"合并为第一个。
但是,有时这些人在每个帐户下都有不同的地址。所以,我想对每个区域进行排序,在此示例中 "C3:H6" 按 E 列中的值进行排序。但是,当我 运行 逐行排序时,它不会执行它。
代码:
With NeedMail
rwCnt = .Cells(Rows.Count, 1).End(xlUp).Row
For y = 3 To rwCnt
If .Cells(y, 2).MergeCells Then
Set mrg = .Range(.Cells(y, 2).MergeArea.Address)
Set srt = .Range(mrg.Offset(0, 1).Address & ":" & mrg.Offset(0, 6).Address)
Set keyRng = .Range(mrg.Offset(0, 3).Address)
cnt = .Cells(y, 2).MergeArea.Rows.Count
Z = y + cnt - 1
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=keyRng, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .Sort
.SetRange srt
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
mrg.UnMerge
'More code to execute here
End If
Next y
End With
示例数据:
先谢谢你了,我绞尽脑汁想弄清楚哪里出了问题?
当您偏移 mrg
时,例如Set srt = .Range(mrg.Offset(0, 1)...
,您的新偏移范围只有 1 行高。所以Resize
行数使用cnt
.
此外,一旦您拥有 srt
,您就可以使用 srt.Sort
。
这是显示简化排序的修订代码。
Sub SortWhenMerged()
Dim needMail As Worksheet
Dim rwCnt As Long, y As Long, cnt As Long
Dim mrg As Range, srt As Range, keyRng As Range
Set needMail = ThisWorkbook.Worksheets("NeedMail")
With needMail
rwCnt = .Cells(.Rows.Count, 1).End(xlUp).row
For y = 3 To rwCnt
If .Cells(y, 2).MergeCells Then
Set mrg = .Cells(y, 2).MergeArea
cnt = mrg.Rows.Count
Set srt = mrg.Offset(, 1).Resize(cnt, 6)
Set keyRng = mrg.Offset(, 3).Resize(cnt)
srt.Sort Key1:=keyRng, Order1:=xlAscending, Header:=xlNo, Orientation:=xlTopToBottom, SortMethod:=xlPinYin
mrg.UnMerge
End If
Next y
End With
End Sub
我 运行 我的排序代码有问题。我的目标是按地址类型对区域进行排序。每个人都有多个帐户,只要有帐户,名字就会出现在一个合并区域中。所以从"B3:B6"合并为第一个。
但是,有时这些人在每个帐户下都有不同的地址。所以,我想对每个区域进行排序,在此示例中 "C3:H6" 按 E 列中的值进行排序。但是,当我 运行 逐行排序时,它不会执行它。
代码:
With NeedMail
rwCnt = .Cells(Rows.Count, 1).End(xlUp).Row
For y = 3 To rwCnt
If .Cells(y, 2).MergeCells Then
Set mrg = .Range(.Cells(y, 2).MergeArea.Address)
Set srt = .Range(mrg.Offset(0, 1).Address & ":" & mrg.Offset(0, 6).Address)
Set keyRng = .Range(mrg.Offset(0, 3).Address)
cnt = .Cells(y, 2).MergeArea.Rows.Count
Z = y + cnt - 1
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=keyRng, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .Sort
.SetRange srt
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
mrg.UnMerge
'More code to execute here
End If
Next y
End With
示例数据:
先谢谢你了,我绞尽脑汁想弄清楚哪里出了问题?
当您偏移 mrg
时,例如Set srt = .Range(mrg.Offset(0, 1)...
,您的新偏移范围只有 1 行高。所以Resize
行数使用cnt
.
此外,一旦您拥有 srt
,您就可以使用 srt.Sort
。
这是显示简化排序的修订代码。
Sub SortWhenMerged()
Dim needMail As Worksheet
Dim rwCnt As Long, y As Long, cnt As Long
Dim mrg As Range, srt As Range, keyRng As Range
Set needMail = ThisWorkbook.Worksheets("NeedMail")
With needMail
rwCnt = .Cells(.Rows.Count, 1).End(xlUp).row
For y = 3 To rwCnt
If .Cells(y, 2).MergeCells Then
Set mrg = .Cells(y, 2).MergeArea
cnt = mrg.Rows.Count
Set srt = mrg.Offset(, 1).Resize(cnt, 6)
Set keyRng = mrg.Offset(, 3).Resize(cnt)
srt.Sort Key1:=keyRng, Order1:=xlAscending, Header:=xlNo, Orientation:=xlTopToBottom, SortMethod:=xlPinYin
mrg.UnMerge
End If
Next y
End With
End Sub