|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
原帖由 puresway 于 2011-3-3 12:00 发表
赵老师,如果区域的一列不在第一列而在最后一列,那么这个代码该怎么改呢,小弟很想学习一下,这个感觉对我用处很大 谢谢您!
Sub Macro2()
Dim wb As Workbook, arr, rng As Range, d As Object, k, t, sh As Worksheet, i&, lr&, lc%
With [a1].CurrentRegion
lr = .Rows.Count '最大行号
lc = .Columns.Count '最大列号
End With
Set rng = [a1].Resize(2, lc)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
arr = Cells(1, lc).Resize(lr)
Set d = CreateObject("scripting.dictionary")
For i = 3 To UBound(arr)
If Not d.Exists(arr(i, 1)) Then
Set d(arr(i, 1)) = Cells(i, 1).Resize(1, lc)
Else
Set d(arr(i, 1)) = Union(d(arr(i, 1)), Cells(i, 1).Resize(1, lc))
End If
Next
k = d.Keys
t = d.Items
For i = 0 To d.Count - 1
Set wb = Workbooks.Add(xlWBATWorksheet)
With wb.Sheets(1)
rng.Copy .[a1]
t(i).Copy .[A3]
End With
wb.SaveAs Filename:=ThisWorkbook.Path & "\考核结果\" & k(i) & ".xls"
wb.Close
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "完毕"
End Sub |
|