|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 ping200030 于 2022-12-1 16:13 编辑
借用其他版主工作簿,增加一个功能
本工作簿有两个代码,一个是由总表数据生成明细表,另一个是自动生成目录+链接
现在的情况总说下标越界,删除生成目录的代码,只剩下生成明细表就没问题,但是想增加目录生成功能
1、总表生成明细表,会冲掉自制的“目录”工作表,如何修改下,不删除想保留的的工作表“目录”,
2、且生成的明细表名称按照拼音顺序排序- Private Sub CommandButton1_Click()
- tms = Timer
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- For Each sht In Sheets
- If sht.Name <> ActiveSheet.Name Then sht.Delete
- Next
- Application.DisplayAlerts = True
- Set d = CreateObject("scripting.dictionary")
- arr = Range("a1").CurrentRegion
- For i = 1 To UBound(arr)
- If Not d.exists(arr(i, 27)) Then
- Set d(arr(i, 27)) = Range("a" & i).Resize(1, 66)
- Else
- Set d(arr(i, 27)) = Union(d(arr(i, 27)), Range("a" & i).Resize(1, 66))
- End If
- Next
- x = d.keys
- For k = 1 To UBound(x)
- Set Sh = ActiveWorkbook.Sheets.Add(, after:=ActiveSheet)
- Sh.Name = x(k)
- d.items()(k).Copy Sh.Range("a" & 2)
- Rows("1:1").Copy Sh.Range("a1")
- Next
- Application.ScreenUpdating = True
- MsgBox Format(Timer - tms, "拆分完成,共耗时:0.00秒"), 64, "时间统计"
- End Sub
复制代码- Private Sub Workbook_Open()
- End Sub
- Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
- If Sheets.Count < Sheets("目录").[a1].End(xlDown).Rows Then
- Call test
- End If
- End Sub
复制代码- Sub test()
- Sheets("目录").UsedRange.Offset(1, 0).ClearContents
- For i = 1 To ThisWorkbook.Worksheets.Count
- Sheets("目录").Cells(i + 1, 1) = i
- ActiveSheet.Hyperlinks.Add Anchor:=Sheets("目录").Cells(i + 1, 2), Address:="", SubAddress:=Sheets(i).Name & "!A1", TextToDisplay:=Sheets(i).Name
- Next
- End Sub
复制代码
|
|