圖一
執行階段錯誤
EXCEL無法將工作表插入目的活頁簿.因為它包含的列和欄比來源活頁簿少.若要移動或複製資料至目的地活頁簿.你可以選取資料.然後用[複製]與[貼上]命令將他插入另一個活頁簿的工作表.
圖二
Sub 產生程式分頁()
Dim A As Workbook, f$, fn$, k%
Dim p$, Sh As Worksheet
Set A = ThisWorkbook
p = "" & Sheet9.Range("B2") & "\"
f = Dir(p & "" & Sheet9.Range("D2") & "*.TXT")
Application.ScreenUpdating = False
Do While f <> ""
Workbooks.Open p & f
k = 0
For Each Sh In Worksheets
If Not IsEmpty(Sh.UsedRange) Then
fn = IIf(k = 0, Replace(f, ".TXT", ""), Replace(f, ".TXT", "_") & k)
Sh.Copy after:=A.Sheets(A.Sheets.Count)
ActiveSheet.Name = fn
k = k + 1
End If
Next
Windows(f).Close True
f = Dir
Loop
Application.ScreenUpdating = True
Sheet9.Select
Range("A1").Select
圖一
Sub 產生程式分頁()
Dim A As Workbook, f$, fn$, k%
Dim p$, Sh As Worksheet
Set A = ThisWorkbook
p = "" & Sheet9.Range("B2") & "\"
f = Dir(p & "" & Sheet9.Range("D2") & "*.TXT")
Application.ScreenUpdating = False
Do While f <> ""
Workbooks.Open p & f
k = 0
For Each Sh In Worksheets
If Not IsEmpty(Sh.UsedRange) Then
fn = IIf(k = 0, Replace(f, ".TXT", ""), Replace(f, ".TXT", "_") & k)
With A.Sheets.Add(after:=Sheets(A.Sheets.Count))
.Name = Sh.Name
Sh.UsedRange.Copy .[A1]
End With
ActiveSheet.Name = fn
k = k + 1
End If
Next
Windows(f).Close True
f = Dir
Loop
Application.ScreenUpdating = True
Sheet9.Select
Range("A1").Select
End Sub作者: n7822123 時間: 2019-11-19 03:48