|
楼主 |
发表于 2016-1-14 16:18
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
你好,我刚才仔细注释了下你的代码,代码没有问题,还有一个小要求,能否在每个分表里面加上对应的LOGO图片呢,谢谢
'===========================================
Sub SplitIntoWorkbookByLines()
Dim r(1 To 100, 1 To 2)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wbo = ActiveWorkbook
s = Array("-------", "INVOICE #") '常量数组
pth = ThisWorkbook.Path & "\" '该文件路径
For j = 0 To UBound(s)
n = 0
'-------用find 方法 循环找到虚线和invoice#的行号赋给二维数组r-------------------------------------------
Set c = Cells.Find(what:=s(j), LookIn:=xlValues, lookat:=xlPart)
If Not c Is Nothing Then
addr = c.Address
Do
n = n + 1 '计数
r(n, j + 1) = c.Row
Set c = Cells.FindNext(c)
Loop While addr <> c.Address
End If
'-----------------------------------------------------------
Next
cn = 38 '报表列数
ReDim cw(1 To cn) '定义一维数组cw装各个列宽
For i = 1 To cn
cw(i) = Columns(i).ColumnWidth
Next
For i = 1 To n
fnm = wbo.Sheets(1).Cells(r(i, 2), "w") & ".xlsx" 'Cells(r(i, 2), "w")对应invoice#,命名文件,赋给变量fnm
If i = 1 Then
sr = 1
Else
sr = r(i - 1, 1) + 1 '上一个虚线的行号+1 作为开始行号
End If
er = r(i, 1) '结束行号,如果不要虚线设置为 r(i, 1) - 1
wbo.Sheets(1).Range(Cells(sr, "a"), Cells(er, "al")).Copy '复制开始行到结束行的区域
Set wbn = Workbooks.Add '添加工作簿,为活动工作簿
ActiveSheet.[a1].PasteSpecial '粘贴到该工作簿
For k = 1 To cn
Columns(k).ColumnWidth = cw(k) '设置对应的列宽
Next
ActiveWindow.DisplayGridlines = False '网线不显示
wbn.SaveAs pth & fnm '另存到 本工作簿的路径,命名为fnm
wbn.Close False ' 关掉活动工作簿,不保存
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
|
|