나는 VB 코딩에 매우 익숙하지 않고 여러 개의 엑셀 파일 워크 시트를 CSV에 저장하려고하는데 여러 시트에 대해이 작업을 수행하는 방법을 모르지만 단일 파일에 대해 수행 할 수있는 방법을 찾았습니다. 이 사이트에서 내가하려는 작업에 매우 유용한 코드를 찾았습니다. 문제는 파일이 워크 시트 이름으로 저장되지만 '파일 이름 _ 워크 시트 이름'과 같은 원본 파일과 워크 시트 이름으로 저장하려고하는데, 직접 시도했지만 계속 오류가 발생합니다. 내가 뭘 잘못하고 있는지 조언 해 주시겠습니까?
제가 사용하고 있는 코드는 다음과 같습니다:
Public Sub SaveWorksheetsAsCsv()
Dim WS As Excel.Worksheet
Dim SaveToDirectory As String
Dim CurrentWorkbook As String
Dim CurrentFormat As Long
CurrentWorkbook = ThisWorkbook.FullName
CurrentFormat = ThisWorkbook.FileFormat
' Store current details for the workbook
SaveToDirectory = "H:\test\"
For Each WS In ThisWorkbook.Worksheets
WS.SaveAs SaveToDirectory & WS.Name, xlCSV
Next
Application.DisplayAlerts = False
ThisWorkbook.SaveAs Filename:=CurrentWorkbook, FileFormat:=CurrentFormat
Application.DisplayAlerts = True
' Temporarily turn alerts off to prevent the user being prompted
' about overwriting the original file.
End Sub
이게 당신이 원하는 것 같아요...
Sub SaveWorksheetsAsCsv()
Dim WS As Excel.Worksheet
Dim SaveToDirectory As String
Dim CurrentWorkbook As String
Dim CurrentFormat As Long
CurrentWorkbook = ThisWorkbook.FullName
CurrentFormat = ThisWorkbook.FileFormat
' Store current details for the workbook
SaveToDirectory = "H:\test\"
For Each WS In Application.ActiveWorkbook.Worksheets
WS.SaveAs SaveToDirectory & WS.Name, xlCSV
Next
Application.DisplayAlerts = False
ThisWorkbook.SaveAs Filename:=CurrentWorkbook, FileFormat:=CurrentFormat
Application.DisplayAlerts = True
' Temporarily turn alerts off to prevent the user being prompted
' about overwriting the original file.
End Sub
나는 유사한 문제입니다. 워크시트에서 데이터가 저장하는 데 필요한 별도의 CSV 파일입니다.
여기's 내 뒤에 코드는 명령 버튼
Private Sub cmdSave()
Dim sFileName As String
Dim WB As Workbook
Application.DisplayAlerts = False
sFileName = "MyFileName.csv"
'Copy the contents of required sheet ready to paste into the new CSV
Sheets(1).Range("A1:T85").Copy 'Define your own range
'Open a new XLS workbook, save it as the file name
Set WB = Workbooks.Add
With WB
.Title = "MyTitle"
.Subject = "MySubject"
.Sheets(1).Select
ActiveSheet.Paste
.SaveAs "MyDirectory\" & sFileName, xlCSV
.Close
End With
Application.DisplayAlerts = True
End Sub
이것이 나를 위해:-)
이걸 시도하고 계신 건가요?
Option Explicit
Public Sub SaveWorksheetsAsCsv()
Dim WS As Worksheet
Dim SaveToDirectory As String, newName As String
SaveToDirectory = "H:\test\"
For Each WS In ThisWorkbook.Worksheets
newName = GetBookName(ThisWorkbook.Name) & "_" & WS.Name
WS.Copy
ActiveWorkbook.SaveAs SaveToDirectory & newName, xlCSV
ActiveWorkbook.Close Savechanges:=False
Next
End Sub
Function GetBookName(strwb As String) As String
GetBookName = Left(strwb, (InStrRev(strwb, ".", -1, vbTextCompare) - 1))
End Function
위의 코드가 완벽하게 작동 하나의 작은 결함 그 결과는 파일에 저장되지 않습니다.csv 확장자. –Tensigh2 일 전
내가 다음 코드를 저장한 파일로 제공합니다. 에 감사를 위해 이 비트의 코드입니다.그것은 모든 일으로 예상된다.
ActiveWorkbook.SaveAs Filename:=SaveToDirectory & ThisWorkbook.Name & "-" & WS.Name & ".csv", FileFormat:=xlCSV