以下是引用mou250在2007-12-18 10:06:11的发言:看了看lenghonghai的代码,做了局部的修改,但还是有问题不懂 '下面是sheet1的代码 Private Sub CommandButton1_Click() Application.ScreenUpdating = False Dim sh As Worksheet, bo1 As Workbook, bo2 As Workbook, bo3 As Workbook Dim sh1 As Worksheet, arr, j%, m%, n% Dim d As Object, x%, i% m = 2: n = 1 Set d = CreateObject("scripting.dictionary") For Each sh In Worksheets x = sh.Range("b65536").End(xlUp).Row For i = 2 To x If Not d.exists(sh.Range("b" & i).Value) Then d.Add sh.Range("b" & i).Value, "" End If Next i Next sh arr = d.keys For i = 0 To UBound(arr) Set bo1 = Workbooks.Add: Workbooks("汇总.xls").Activate '将原有的“汇总”改为"汇总.xls"就不会有下标越界的情况了 For Each sh In Worksheets x = sh.Range("a65536").End(xlUp).Row Set sh1 = bo1.Worksheets(n) sh1.Name = sh.Name sh.Rows(1).Copy sh1.Range("a" & 1) '添加上面一句可以满足生成的每个表都有表头 For j = 2 To x If sh.Range("b" & j).Value = arr(i) Then sh.Rows(j).Copy sh1.Range("a" & m) m = m + 1 End If Next j m = 2: n = n + 1 Next sh bo1.SaveAs ThisWorkbook.Path & "\" & arr(i) & ".xls" bo1.Close True: n = 1 Next i Application.ScreenUpdating = True End Sub leng的思路特别好,我看了半天才弄明白。 因为刚开始学习VBA,许多语法都不熟。 第一,上面的程序段中最后由一个close true,这个方法没有在帮助上查到,只查到了close 第二,完全看不明白leng在sheet2中的程序了,估计应该是按照同样的规则向sheet1生成的books中添加数据的意思。 第三,yijiboo提出的去掉工作单位一列和添加汇总的程序,应该是添加在sheet1中就足够了吧 close true是关闭这个工作簿,并进行保存。 |