For j = 2 To ActiveWorkbook.Sheets.Count '從#2開始的工作表中循環
ActiveWorkbook.Sheets(j).Activate
[A2] = [A2].Value '值化
[B1] = [B1].Value '值化
If j > xd Then
Sheets(j).Delete '刪除大於月底日的工作表
End If
Next
Sheets("1").Activate
For k = 1 To [U1] '將U1儲存格的值,作為變數存取次數,依序命名檔案名並存檔
[P1] = k '指定儲存格的值
ActiveWorkbook.SaveAs filename:=myPath & [G1] & " _" & m & ".xlsx" 'one by one 存檔k次
Next
ActiveWorkbook.Close True '存檔後關閉檔案
End With
複製代碼
作者: 准提部林 時間: 2020-2-9 10:53
本帖最後由 准提部林 於 2020-2-10 16:16 編輯
Sub TEST()
Dim xPath$, xFile$, i&, k&
Dim Lastday$, mDay%, xBK As Workbook
Lastday = DateSerial(Year(Date), Month(Date) + 2, 0) '下個月月底
mDay = Day(Lastday) '下個月天數
Set xBK = Workbooks.Open(xPath & xFile) '開啟指定檔案
'-----------------------------------
Application.DisplayAlerts = False
On Error Resume Next
For i = mDay + 1 To 31: xBK.Sheets(i & "").Delete: Next i '刪工作表
On Error GoTo 0
Application.DisplayAlerts = True
'-----------------------------------
For i = 1 To mDay
With xBK.Sheets(i & "")
.[A2] = .[A2].Value '值化
.[B1] = .[B1].Value '值化
End With
Next i
End Sub
准大,
2月已經無法測試,3月又是31天,所以我測試4月
這句
For i = mDay + 1 To 31: BK.Sheets(i & "").Delete: Next i '刪工作表
與前面的Dim Lastday$, mDay%, xBK As Workbook不同,無法刪除大於月底日的工作表,所以我改為
For i = mDay + 1 To 31: xBK.Sheets(i & "").Delete: Next i
刪除大於月底日的工作表可執行,但又有其他地方卡關,請幫忙看下是否我有弄錯的語法??
Sub EX()
Dim Path$, File$, i&, k&
Dim Lastday$, mDay%, xBK As Workbook, BK As Workbook
紅色字體為修改語法:
Sheet刪除由左向右會造成異常
For i = mDay + 1 To 31: xBK.Sheets(i & "").Delete: Next i '刪工作表
改為由右向左刪除
For i = 31 To mDay + 1 Step -1: xBK.Sheets(i & "").Delete: Next i
藍色字體為刪除
試試看!!
Sub EX()
Dim Path$, File$, i&, k&
Dim Lastday$, mDay%, xBK As Workbook, BK As Workbook
Dim myPath$, xFile$, m$, h$
Lastday = DateSerial(Year(Date), Month(Date) + 3, 0) '下下個月月底
mDay = Day(Lastday) '下個月天數
h = DateSerial(Year(Date), Month(Date) + 2, 1) '設定下個月1日
m = Format(h, "M月") '設定下個月份
Application.ScreenUpdating = False '關閉屏幕更新
Application.DisplayAlerts = False '一般提警示訊息關閉
Path = "D:\backup20060523\MDBView\麻辣學園\1.下個月理貨單_測試\檔案\" '來源資料夾
myPath = "D:\backup20060523\MDBView\麻辣學園\1.下個月理貨單_測試\2_暫\" '另存目的資料夾
File = Dir(Path & "*.xlsx") '來源檔名
Do While File <> ""
With Workbooks.Open(Path & File)
On Error Resume Next
Sheets("1").Activate
[A2] = Format(h, "M/D") '輸入指定日期,為下個月1日
ActiveWorkbook.Save '**存檔不關閉
End With
Set xBK = Workbooks.Open(Path & File) '開啟指定檔案
On Error Resume Next
' For i = mDay + 1 To 31: xBK.Sheets(i & "").Delete: Next i '刪工作表
For i = 31 To mDay + 1 Step -1: xBK.Sheets(i & "").Delete: Next i '刪工作表
On Error GoTo 0
For i = 1 To mDay
With xBK.Sheets(i & "")
.[A2] = .[A2].Value '值化
.[B1] = .[B1].Value '值化
End With
Next i
' For i = 1 To mDay '在工作表中循環
' With xBK.Sheets(i & "")
Sheets("1").Activate
For k = 1 To [U1] '將U1儲存格的值,作為變數存取次數,依序命名檔案名並存檔
[P1] = k '指定儲存格的值
ActiveWorkbook.SaveAs Filename:=myPath & [V2] & [G1] & " _" & m & ".xlsx" 'one by one 存檔k次
Next
ActiveWorkbook.Close True '存檔後關閉檔案
' End With
' Next i
File = Dir
Loop作者: 准提部林 時間: 2020-2-10 16:15
請問准大,
For i = mDay + 1 To 31: xBK.Sheets(i & "").Delete: Next i '刪工作表
可以刪除大於月底日的工作表
但卡在這裡無法執行,是什麼問題?
For i = 1 To mDay '在工作表中循環
With xBK.Sheets(i & "")
Sheets("1").Activate
For k = 1 To [U1]作者: jcchiang 時間: 2020-2-10 16:45