Dim I As Long
Dim xWeek As Integer
Dim xS As Worksheet
Dim xPH$
xPH = ThisWorkbook.Path & "\"
On Error Resume Next
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set xS = Sheets("週報表")
xWeek = InputBox("請輸入第""?""週")
xS.Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = "第" & xWeek & "週"
Set xName = ActiveSheet
With xName.UsedRange
.Value = .Value
End With
xName.Copy
With ActiveWorkbook
ActiveSheet.Name = "第" & xWeek & "週"
.SaveAs xPH & "第" & xWeek & "週.xlsx", CreateBackup:=False
.Close
End With
xName.Delete
Application.ScreenUpdating = True
Sub test0624()
Dim xWeek As Integer
Dim xS As Worksheet
Dim xPH$
Dim 年份 As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlManual '停用自動重算
年份 = 2020 '判斷每週的起始、每週的結束日期用
xPH = ThisWorkbook.Path & "\"
On Error Resume Next
Set xS = Sheets("週報表")
xWeek = InputBox("請輸入第""?""週")
xlsName = xPH & "第1~" & xWeek & "週.xlsx"
'B結果:一個檔案。(第1~5週.XLSX) 裡頭有5個工作表
With Workbooks.Add
sh_Cnt = .Sheets.Count
For sh = 1 To xWeek
xS.Activate
xS.Copy After:=Sheets(Sheets.Count)
Set xName = ActiveSheet
ActiveSheet.Name = "第" & sh & "週"
With xName.UsedRange
.Calculate
.Value = .Value
End With
xName.Copy After:=.Sheets(.Sheets.Count) '注Sheets前面有 "." 是複製到新的活頁簿
xName.Delete
Next sh
'刪除原本空白表格
For sh = 1 To sh_Cnt: .Sheets(1).Delete: Next
'存檔關閉
.SaveAs xlsName
.Close True
End With
'A結果:分別產生5個檔案。( 第1週.XLSX 第2週.XLSX 第3週.XLSX 第4週.XLSX 第5週.XLSX)
'這段基本上可已與上面那段合併寫,但程式會不好閱讀,為了讓你看懂,先拆開寫給你
'因為我的週報表F1 與 G1 都是錯誤值,檔名的日期我先自己定義,你再自己修改!
With Workbooks.Open(xlsName)
For sh = 1 To .Sheets.Count
Strday = Format(週始日(年份, sh), "emmdd") '利用自定函數抓該週次的起使日期(阿龍Test用)
Endday = Format(週始日(年份, sh) + 6, "emmdd") '利用自定函數抓該週次的結束日期(阿龍Test用)
'Strday = .Sheets(sh).[F1] '你的日期開始,請自行打開測試
'Endday = .Sheets(sh).[H1] '你的日期結束,請自行打開測試
xlsName = "(" & .Sheets(sh).Name & ").xlsx"
xlsName = xPH & Strday & "~" & Endday & xlsName
.Sheets(sh).Copy
ActiveWorkbook.SaveAs xlsName
ActiveWorkbook.Close True
Next
.Close False
End With
Set xS = Nothing
Set xName = Nothing
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic '啟用自動重算
End Sub
Function 週始日(ByVal 西元 As Integer, ByVal 週次 As Integer) As Date
Dim Day1 As Date, 週始1 As Date
Day1 = DateSerial(2020, 1, 1)
週始1 = Day1 - Weekday(Day1) + 1
Dayadd = (週次 - 1) * 7
週始日 = 週始1 + Dayadd
End Function
Sub Create01() '批量複製'
Dim xS As Worksheet, xName As Worksheet
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlManual '停用自動重算
xPH$ = ThisWorkbook.Path & "\"
Set xS = Sheets("週報表")
xWeek% = InputBox("請輸入第1週∼第""?""週")
'A結果:分別產生5個檔案。( 第1週.XLSX 第2週.XLSX 第3週.XLSX 第4週.XLSX 第5週.XLSX)
For i = 1 To xWeek
xS.Copy After:=Sheets(Sheets.Count)
Set xName = ActiveSheet
xName.Name = "第" & i & "週"
With xName.UsedRange
.Calculate '重算
.Value = .Value
End With
xName.Copy
With ActiveWorkbook
.SaveAs xPH & "第" & i & "週.xlsx", CreateBackup:=False
.Close True
End With
xName.Delete
Set xName = Nothing
Next
Set xS = Nothing
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic '啟用自動重算
End Sub
第二個程序 (產生1個檔案,多工作表)
Sub Create02() '獨立複製'
Dim xS As Worksheet, xName As Worksheet
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlManual '停用自動重算
xPH$ = ThisWorkbook.Path & "\"
Set xS = Sheets("週報表")
xWeek% = InputBox("請輸入第""?""週")
xlsName$ = xPH & "第1~" & xWeek & "週.xlsx"
'B結果:一個檔案。(第1~5週.XLSX) 裡頭有5個工作表
With Workbooks.Add
sh_Cnt = .Sheets.Count
For sh = 1 To xWeek
xS.Activate
xS.Copy After:=Sheets(Sheets.Count)
Set xName = ActiveSheet
xName.Name = "第" & sh & "週"
With xName.UsedRange
.Calculate '重算
.Value = .Value
End With
xName.Copy After:=.Sheets(.Sheets.Count) '注Sheets前面有 "." 是複製到新的活頁簿
xName.Delete
Set xName = Nothing
Next sh
'刪除原本空白表格
For sh = 1 To sh_Cnt: .Sheets(1).Delete: Next
'存檔關閉
.SaveAs xlsName
.Close True
End With
Set xS = Nothing
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic '啟用自動重算
End Sub
你的工作表函數運算量太大........很容易死當
所以我執行程式過程中把函數自動重算關閉
建議你瘦身一下,減少函數計算量
出門了,掰掰 作者: 准提部林 時間: 2020-6-25 11:08
Sub 轉存()
Dim xSht As Worksheet, xPH$, NN, SName$, i&
Set xSht = Sheets("週報表")
xPH = ThisWorkbook.Path & "\"
For i = 1 To 1 '1至第?週, 自己搞定
SName = "第" & i & "週.xls"
xSht.Copy
With ActiveWorkbook
With .Sheets(1).UsedRange: .Value = .Value: End With
For Each NN In .Names '被帶過來的小孩--定義名稱--刪除
If InStr(NN.Name, "Print_") = 0 Then NN.Delete '除了Print相關的, 餘刪除
Next
.SaveAs Filename:=xPH & SName, CreateBackup:=False
.Close 0
End With
Next i
End Sub
xPH = ThisWorkbook.Path & "\"
On Error Resume Next
Set xS = Sheets("週報表")
xWeek = InputBox("請輸入第""?""週")
xlsName = xPH & "第1~" & xWeek & "週.xlsx"
'B結果:一個檔案。(第1~5週.XLSX) 裡頭有5個工作表
With Workbooks.Add
sh_Cnt = .Sheets.Count
For sh = 1 To xWeek
xS.Activate
xS.Copy After:=Sheets(Sheets.Count)
Set xName = ActiveSheet
ActiveSheet.Name = "第" & sh & "週"
With xName.UsedRange
.Calculate
.Value = .Value
End With
xName.Copy After:=.Sheets(.Sheets.Count) '注Sheets前面有 "." 是複製到新的活頁簿
xName.Delete
Next sh
'刪除原本空白表格
For sh = 1 To sh_Cnt: .Sheets(1).Delete: Next
'存檔關閉
.SaveAs xlsName
.Close True
End With
'A結果:分別產生5個檔案。( 第1週.XLSX 第2週.XLSX 第3週.XLSX 第4週.XLSX 第5週.XLSX)
'這段基本上可已與上面那段合併寫,但程式會不好閱讀,為了讓你看懂,先拆開寫給你
'因為我的週報表F1 與 G1 都是錯誤值,檔名的日期我先自己定義,你再自己修改!
With Workbooks.Open(xlsName)
For sh = 1 To .Sheets.Count