我明白你的代碼是否正確,從目標文件夾中讀取所有的文件,紙張問題是,你只需要提取一個Sheet
名爲每個文件Data
,所以如果是這樣的話試試這個:
編輯只包括選定的列提取!
方法:複製目標工作
Sub SaveToCSVs()
Const kWshName As String = "Data"
Dim sPathInp As String, sPathOut As String
Dim sPathFile As String, sCsvFile As String
Dim WbkSrc As Workbook, WshSrc As Worksheet
Dim WbkCsv As Workbook, WshCsv As Worksheet
Dim rData As Range
sPathInp = "C:\temp\pydev\"
sPathOut = "C:\temp\"
sPathFile = Dir(sPathInp)
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Do While (sPathFile <> "")
If Right(sPathFile, 4) = ".xls" Or Right(sPathFile, 5) = ".xlsx" Then
Rem Initialize Objects
Set WbkSrc = Nothing
Set WshSrc = Nothing
Rem Set Objects
On Error Resume Next
Set WbkSrc = Workbooks.Open(sPathInp & sPathFile)
If Not (WbkSrc Is Nothing) Then
Set WshSrc = WbkSrc.Sheets(kWshName)
If Not (WshSrc Is Nothing) Then
On Error GoTo 0
Rem Set Csv Filename
sCsvFile = Left(sPathFile, -1 + InStrRev(sPathFile, "."))
sCsvFile = sCsvFile & " - " & kWshName
Rem Calculate, Unhide Rows & Columns & Copy Data Sheet
With WshSrc
.Calculate
.Cells.EntireRow.Hidden = False
.Cells.EntireColumn.Hidden = False
.Copy
End With
Set WshCsv = ActiveSheet
Rem Delete All Other Columns
With Range(WshCsv.Cells(1), WshCsv.UsedRange.SpecialCells(xlLastCell))
.Value = .Value
Set rData = Union(Columns("A"), Columns("P"), Columns("AC"))
rData.EntireColumn.Hidden = True
.SpecialCells(xlCellTypeVisible).EntireColumn.Delete
rData.EntireColumn.Hidden = False
End With
Rem Save as Csv
WshCsv.SaveAs Filename:=sPathOut & sCsvFile, FileFormat:=xlCSV
WshCsv.Parent.Close
WbkSrc.Close
End If: End If: End If
sPathFile = Dir
On Error GoTo 0
Loop
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub
方法:打開工作簿爲只讀
Sub SaveToCSVs()
Const kWshName As String = "Data"
Dim sPathInp As String
Dim sPathOut As String
Dim sPathFile As String
Dim sCsvFile As String
Dim WbkSrc As Workbook
Dim WshSrc As Worksheet
Dim rData As Range
sPathInp = "C:\temp\pydev\"
sPathOut = "C:\temp\"
sPathFile = Dir(sPathInp)
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Do While (sPathFile <> "")
If Right(sPathFile, 4) = ".xls" Or Right(sPathFile, 5) = ".xlsx" Then
Rem Initialize Objects
Set WbkSrc = Nothing
Set WshSrc = Nothing
Rem Set Objects
On Error Resume Next
Set WbkSrc = Workbooks.Open(Filename:=sPathInp & sPathFile, ReadOnly:=True)
If Not (WbkSrc Is Nothing) Then
Set WshSrc = WbkSrc.Sheets(kWshName)
If Not (WshSrc Is Nothing) Then
On Error GoTo 0
Rem Set Csv Filename
sCsvFile = Left(sPathFile, -1 + InStrRev(sPathFile, "."))
sCsvFile = sCsvFile & " - " & kWshName
Rem Calculate, Unhide Rows & Columns & Copy Data Sheet
With WshSrc
.Calculate
.Cells.EntireRow.Hidden = False
.Cells.EntireColumn.Hidden = False
Rem Delete All Other Columns
With Range(.Cells(1), .UsedRange.SpecialCells(xlLastCell))
.Value = .Value
Set rData = Union(Columns("A"), Columns("P"), Columns("AC"))
rData.EntireColumn.Hidden = True
.SpecialCells(xlCellTypeVisible).EntireColumn.Delete
rData.EntireColumn.Hidden = False
End With: End With
Rem Save as Csv
WshSrc.SaveAs Filename:=sPathOut & sCsvFile, FileFormat:=xlCSV
WbkSrc.Close
End If: End If: End If
sPathFile = Dir
On Error GoTo 0
Loop
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub
來源
2016-11-05 07:34:01
EEM
我居然因此未對我自己寫的,但我還是把幫助從下面的代碼 –
子SaveToCSVs() Dim fDir As String Dim wB As Workbook Dim wS作爲工作表 昏暗fPath作爲字符串 昏暗SPATH作爲字符串 fPath = 「C:\ TEMP \的PyDev \」 SPATH = 「C:\ TEMP \」 FDIR = DIR(fPath) 的do while(FDIR <> 「」 ) If Right(fDir,4)=「.xls」或Right(fDir,5)=「.xlsx」然後 On Error Resume Next 設置wB = Workbooks.Open(fPath&fDir) 對於每個wS在wB .Sheets wS.SaveAs SPATH&wS.Name,xlCSV 下一步WS wB.Close假 設置白平衡=無 結束如果 FDIR = DIR 對錯誤轉到0 環路 End Sub –
我不知道如何在這裏以適當的格式寫上述..我是這個新手 –