|
我想根据小渔镇工作簿中“农户基础信息明细表”的H列中的村名作为新工作簿名并在此工作簿根据组别分组建工作表,再把“农户基础信息表”中的内容分村分组复制到这些村组工作簿、工作表,请教各位高手我用代码怎样实现?还有工作簿有没有name属性?怎样根据需要动态获取工作簿名?
终于搞定了,运行时间有点长
- Sub 分解()
- Dim crr(1 To 1500, 1 To 8)
- Set yyy = CreateObject("Scripting.FileSystemObject")
-
- If yyy.FolderExists(ThisWorkbook.Path & "\各村") = False Then
- Set aaa = CreateObject("Scripting.FileSystemObject")
- aaa.CreateFolder ThisWorkbook.Path & "\各村"
- End If
- Set d = CreateObject("scripting.dictionary")
- arr = ThisWorkbook.Sheets("农户基础信息明细表").Range("a2:h" & ThisWorkbook.Sheets("农户基础信息明细表").[h65536].End(3).Row)
- For i = 1 To UBound(arr)
- d(Left(arr(i, 8), Len(arr(i, 8)) - 2)) = ""
- Next
- arrcm = d.keys
- d.RemoveAll
- For i = 0 To UBound(arrcm)
- Set wb = Workbooks.Add
- wb.SaveAs Filename:=ThisWorkbook.Path & "\各村" & Right(arrcm(i), Len(arrcm(i)) - 2), FileFormat:=xlExcel8
- 50:
- j = j + 1
- If j = UBound(arr) Then
- If Left(arr(j, 8), Len(arr(j, 8)) - 2) = arrcm(i) Then
- c = c + 1
- For k = 1 To 8
- crr(c, k) = arr(j, k)
- Next
- End If
- Sheets.Add after:=Sheets(Sheets.Count)
- ActiveSheet.Name = Right(arr(j, 8), 2)
- ThisWorkbook.Sheets(1).Range("a1:h1").Copy
- wb.Sheets(Sheets.Count).Range("a1").PasteSpecial Paste:=xlPasteAll
- wb.Sheets(Sheets.Count).[a1].PasteSpecial Paste:=xlPasteColumnWidths
- wb.Sheets(Sheets.Count).[a2].Resize(c, 8) = crr
- c = 0
- Erase crr
- GoTo 100
- End If
-
-
- If (j <> 1 And Right(arr(j, 8), 2) <> Right(arr(j + 1, 8), 2)) Then
- If Left(arr(j, 8), Len(arr(j, 8)) - 2) = arrcm(i) Then
- c = c + 1
- For k = 1 To 8
- crr(c, k) = arr(j, k)
- Next
- End If
- Sheets.Add after:=Sheets(Sheets.Count)
- ActiveSheet.Name = Right(arr(j, 8), 2)
- ThisWorkbook.Sheets(1).Range("a1:h1").Copy
- wb.Sheets(Sheets.Count).Range("a1").PasteSpecial Paste:=xlPasteAll
- wb.Sheets(Sheets.Count).[a1].PasteSpecial Paste:=xlPasteColumnWidths
- wb.Sheets(Sheets.Count).[a2].Resize(c, 8) = crr
- c = 0
- Erase crr
- If Left(arr(j + 1, 8), Len(arr(j + 1, 8)) - 2) <> arrcm(i) Then GoTo 100
- Else
- If Left(arr(j, 8), Len(arr(j, 8)) - 2) = arrcm(i) Then
- c = c + 1
- For k = 1 To 8
- crr(c, k) = arr(j, k)
- Next
- End If
- End If
- If j = UBound(arr) Then
- wb.Close True
- Else
- GoTo 50
- End If
- 100:
- wb.Close True
- Next
- End Sub
复制代码
小渔镇.rar
(247.67 KB, 下载次数: 72)
|
|