2013-07-02 1 views
2

현재 작은 폴더에있는 xslx 개의 파일을 모두 탐색하여 모두 xml 개의 워크 시트로 저장할 수 있습니다. 잘 작동하지만 하위 폴더에 저장하고 싶습니다, 나는 항상 다시 같은 파일을 저장하고있어 같은 일이 잘못 곳이야하위 폴더에 * .xml 통합 문서를 저장할 때 Dir 문제가 발생했습니다.

. 나는 Dir 구문에 너무 익숙하지 않기 때문에 누군가가 나를 조금 도와 줄 수 있다면 정말 감사 할 것입니다.

이 부분은 예상대로 작동합니다

Sub XLS2XML() 
Application.DisplayAlerts = False 

Dim folderPath As String 
Dim Report As String 
Dim ReportName As String 
Dim XMLLocation As String 
Dim XMLReport As String 

Dim WB As Workbook 

'set path to current location 
folderPath = ThisWorkbook.Path 

If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\" 

'loop through all xlsx files 
Report = Dir(folderPath & "*.xlsx") 
Do While Report <> "" 
    Set WB = Workbooks.Open(folderPath & Report) 

    'get the file name without path 
    ReportName = Split(Report, ".")(0) 
    XMLLocation = folderPath 
    XMLReport = XMLLocation & ReportName & ".xml" 

    'save the file as xml workbook 
    ActiveWorkbook.SaveAs filename:=XMLReport, _ 
    FileFormat:=xlXMLSpreadsheet, ReadOnlyRecommended:=False, CreateBackup:=False 

    'close and next 
    WB.Close False 
    Report = Dir 
Loop 

MsgBox "All XML files have been created" 

Application.DisplayAlerts = True 
End Sub 

이 하나 나에 실패

Sub XLS2XML() 
Application.DisplayAlerts = False 

Dim folderPath As String 
Dim Report As String 
Dim ReportName As String 
Dim XMLLocation As String 
Dim XMLReport As String 

Dim WB As Workbook 

'set path to current location 
folderPath = ThisWorkbook.Path 

If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\" 

'loop through all xlsx files 
Report = Dir(folderPath & "*.xlsx") 
Do While Report <> "" 
    Set WB = Workbooks.Open(folderPath & Report) 

    'get the file name without path and save it in xml folder 
    ReportName = Split(Report, ".")(0) 
    XMLLocation = folderPath & "xml" 
    XMLReport = XMLLocation & "\" & ReportName & ".xml" 

    'create xml folder if it doesn't exist yet 
    If Len(Dir(XMLLocation, vbDirectory)) = 0 Then 
     MkDir XMLLocation 
    End If 

    'save the file as xml workbook 
    ActiveWorkbook.SaveAs filename:=XMLReport, _ 
    FileFormat:=xlXMLSpreadsheet, ReadOnlyRecommended:=False, CreateBackup:=False 

    'close and next 
    WB.Close False 
    Report = Dir 
Loop 

내 구문이 잘못되면 어떤 생각? 또한 자동 모드에서 동일한 작업을 수행 할 수 있습니까? 그럼 통합 문서를 열지 않고?

감사합니다.

답변

1

문제는 테스트하고 XML 하위 디렉토리를 만들려면 초기 Dir 루프 내 두 번째 Dir을 사용하는 것입니다.

루프 밖에서이 작업을 수행 할 수 있습니다. 특히 일회 검사이므로 루프를 반복해서 수행하면 안됩니다.

아래이 같은 뭔가

Sub XLS2XML() 
Application.DisplayAlerts = False 

Dim folderPath As String 
Dim Report As String 
Dim ReportName As String 
Dim XMLlocation As String 
Dim XMLReport As String 

Dim WB As Workbook 

'set path to current location 
folderPath = ThisWorkbook.Path 
XMLlocation = folderPath & "xml" 

If Len(Dir(XMLlocation, vbDirectory)) = 0 Then MkDir XMLlocation 
If Right$(folderPath, 1) <> "\" Then folderPath = folderPath + "\" 

'loop through all xlsx files 
Report = Dir(folderPath & "*.xlsx") 

Do While Len(Report) > 0 
    Set WB = Workbooks.Open(folderPath & Report) 

    'get the file name without path and save it in xml folder 
    ReportName = Split(Report, ".")(0) 
    XMLReport = XMLlocation & "\" & ReportName & ".xml" 

    'save the file as xml workbook 
    WB.SaveAs Filename:=XMLReport, _ 
    FileFormat:=xlXMLSpreadsheet, ReadOnlyRecommended:=False, CreateBackup:=False 

    'close and next 
    WB.Close False 
    Report = Dir 
Loop 
End Sub 
(당신은 그렇지 Loop through files in a folder using VBA? 내 간단한 와일드 카드 코드 예제에 따라, Dir 벌금을 사용)
관련 문제