|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 乐乐2006201505 于 2017-12-14 11:33 编辑
- Private Sub CommandButton1_Click()
- Application.ScreenUpdating = False
- Dim d As Object
- Set d = CreateObject("Scripting.Dictionary")
- lastrow = [a65536].End(xlUp).Row
- arr = Range("A1:A" & lastrow)
- startrow = 3
- For i = 1 To lastrow
- If Trim(arr(i, 1)) = "合计" Then
- d(arr(startrow, 1)) = i
- startrow = i + 1
- End If
- Next
- k = d.keys
- For m = 0 To d.Count - 1
- Set sht = Sheets.Add(After:=Sheets(Sheets.Count))
- sht.Name = k(m)
- Next
- startrow = 3
- For i = 1 To lastrow
- If Trim(arr(i, 1)) = "合计" Then '通过"合计"来判断从哪一行开始拆分
- If d.exists(arr(startrow, 1)) Then
- Set sht = Sheets(arr(startrow, 1))
- If sht.Range("A3") = "" Then
- Range("A1:L2").Copy sht.Range("A1")
- Range("A" & startrow).Resize(i - startrow + 1, 12).Copy sht.Range("A3")
- startrow = i + 1
- Else
- Range("A" & startrow).Resize(i - startrow + 1, 12).Copy sht.Range("A" & sht.Range("a65536").End(3).Row)
- startrow = i + 1
- End If
- End If
- End If
- Next
- Application.ScreenUpdating = True
- End Sub
复制代码
为了实现多次点击按钮不出错,已将代码优化,请下载附件运行即可。望看到代码的高手可以优化我的代码,尤其是8-18句。
|
评分
-
1
查看全部评分
-
|