|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
麻烦验证一下效率
- Public Sub 数组方法()
- ''关闭警示弹窗
- Dim dStart, dCount
- Application.DisplayAlerts = False
- Dim wb As Workbook, sht As Worksheet, psht As Worksheet
- Set dStart = CreateObject("Scripting.Dictionary")
- Set dCount = CreateObject("Scripting.Dictionary")
- Set wb = Application.ThisWorkbook
- Set sht = wb.Worksheets(1)
- With sht
- eRow = .Cells(.Rows.Count, 1).End(xlUp).Row
- Set rng = .Range("A1").CurrentRegion
- arr = rng.Value
- Dim br(1 To 10000, 1 To 6)
- r = 0
- For i = LBound(arr) To UBound(arr)
- If IsDate(arr(i, 1)) Then
- dat = Format(arr(i, 1), "yyyy/mm/dd")
- Else
- '''存入数据行
- For j = 4 To UBound(arr, 2) Step 2
- If arr(i, j) <> "" Then
- r = r + 1
- br(r, 1) = arr(i, 1)
- br(r, 2) = arr(i, 2)
- br(r, 3) = arr(i, 3)
- br(r, 4) = arr(i, j)
- br(r, 5) = arr(i, j + 1)
- br(r, 6) = dat
- If Not dStart.Exists(br(r, 1) & br(r, 6)) Then dStart(br(r, 1) & br(r, 6)) = "A" & r
- If Not dStart.Exists(br(r, 1) & br(r, 2) & br(r, 6)) Then dStart(br(r, 1) & br(r, 2) & br(r, 6)) = "B" & r
- dCount(br(r, 1) & br(r, 6)) = dCount(br(r, 1) & br(r, 6)) + 1
- dCount(br(r, 1) & br(r, 2) & br(r, 6)) = dCount(br(r, 1) & br(r, 2) & br(r, 6)) + 1
- End If
- Next j
- End If
- Next i
- End With
- Set sht = wb.Worksheets(2)
- With sht
- .Cells.Clear
- .Range("a1").Resize(UBound(br), UBound(br, 2)).Value = br
- '边框居中
- .UsedRange.Borders.LineStyle = 1
- .UsedRange.HorizontalAlignment = 3
- .Sort.SortFields.Clear
- .Sort.SetRange .UsedRange
- .Sort.SortFields.Add Key:=.UsedRange.Columns(6), _
- SortOn:=xlSortOnValues, _
- Order:=xlAscending, _
- DataOption:=xlSortNormal
- .Sort.SortFields.Add Key:=.UsedRange.Columns(1), _
- SortOn:=xlSortOnValues, _
- Order:=xlAscending, _
- DataOption:=xlSortNormal
- .Sort.SortFields.Add Key:=.UsedRange.Columns(2), _
- SortOn:=xlSortOnValues, _
- Order:=xlAscending, _
- DataOption:=xlSortNormal
- .Sort.Apply
- For Each k In dStart
- .Range(dStart(k)).Resize(dCount(k), 1).Merge
- Next k
- End With
- ''恢复警示弹窗
- Application.DisplayAlerts = True
- End Sub
- ''代码功能:对目标单元格区域进行按行排序
- '''参数【sht】: 目标工作表
- '''参数【rng】: 目标单元格区域
- '''参数【cols 】: 排序的列号与升降序的数组 如Array(array(1,True)) True为升序
- '''参数【header】: 目标单元格区域是否包含表头
- '''调用示范:sort2007 sht,rng,array(array(1,true))
- Sub Sort2007(ByVal sht As Worksheet, ByVal rng As Range, ByVal cols As Variant, Optional header = True)
- With sht
- '''操作Sort对象,升序xlAscending, 降序xlDescending
- With .Sort
- '''清除排序字段
- .SortFields.Clear
- '''添加排序字段,Rng.Columns(1)为目标单元格区域的第1列,按需修改
- For Each cs In cols
- .SortFields.Add Key:=rng.Columns(cs(0)), _
- SortOn:=xlSortOnValues, _
- Order:=IIf(cs(1), xlAscending, xlDescending), _
- DataOption:=xlSortNormal
- Next
- '''设置目标单元格区域
- .SetRange rng
- '''目标单元格区域是否包含表头 xlYes, xlNo
- If header Then
- .header = xlYes
- Else
- .header = xlNo
- End If
- .MatchCase = False
- .Orientation = xlTopToBottom
- .SortMethod = xlPinYin
- .Apply
- End With
- End Sub
复制代码 |
|