XLS Öffnen TabellenBlätter löschen und speichern

  • VBScript

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von 1nsane.

    XLS Öffnen TabellenBlätter löschen und speichern

    Hallo :-)

    Ich benötige ein Script das mir eine Excel Datei im Ordner öffnet bestimmte Tabellenblätter deren Tabellenblattnamen ein bestimmtes Wort enthält beibehält und anschließen speichert.

    Hier mein bisheriger code der like Operator scheint hier rumzumucken liegt bestimmt am Datentyp

    CODE /*


    Erweiterung = "xls"


    function machdasweg(Datei)
    Dateiname=Datei
    set exApp = CreateObject("Excel.Application")
    exApp.Visible = TRUE
    set exBook = exApp.WorkBooks.Open(Dateiname) ' Excel Datei öffnen

    Set exSheet = exBook.Sheets

    for each Worksheet in exSheet
    exApp.DisplayAlerts = False

    for each Worksheet in exSheet
    exApp.DisplayAlerts = False
    If Worksheet.Name like "*WORT*" =False Then
    Worksheet.Delete
    end if
    Next
    exApp.DisplayAlerts = True
    exApp.ActiveWorkbook.SaveAs Dateiname
    exApp.Quit
    Set exApp=Nothing

    end function


    set fs = CreateObject("Scripting.FilesystemObject")
    scriptname = Wscript.ScriptFullName
    scriptpath = fs.getparentfoldername(scriptname)
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set FileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='" & scriptpath & "'} Where " _
    & "ResultClass = CIM_DataFile")
    For Each objFile In FileList
    If objFile.Extension = Erweiterung Then
    test=machdasweg (objFile.Name)
    End If
    Next

    */


    danke für eure Hilfe




    Könnt's so funktionieren?

    Visual Basic-Quellcode

    1. Erweiterung = "xls"
    2. Set FSO = CreateObject("Scripting.FilesystemObject")
    3. ScriptName = Wscript.ScriptFullName
    4. ScriptPath = FSO.GetParentFolderName(ScriptName)
    5. strComputer = "."
    6. Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    7. Set FileList = objWMIService.ExecQuery("ASSOCIATORS OF {Win32_Directory.Name='" & ScriptPath & "'} Where " & "ResultClass = CIM_DataFile")
    8. For Each objFile In FileList
    9. If objFile.Extension = Erweiterung Then
    10. MachDasWeg objFile.Name
    11. End If
    12. Next
    13. Sub MachDasWeg(Datei)
    14. Dateiname = Datei
    15. Wort = "WORT"
    16. Set ExApp = CreateObject("Excel.Application")
    17. ExApp.Visible = True
    18. Set ExBook = ExApp.Workbooks.Open(Dateiname) 'Excel Datei öffnen
    19. ExApp.DisplayAlerts = False
    20. For Each WSh In ExBook.Sheets
    21. ExApp.DisplayAlerts = False
    22. For i = 1 To Len(WSh.Name) - Len(Wort)
    23. If Mid(WSh.Name, i, Len(Wort)) = Wort And i = Len(Wort) Then
    24. WSh.Delete
    25. End If
    26. Next
    27. Next
    28. ExApp.DisplayAlerts = True
    29. ExBook.SaveAs Dateiname
    30. ExApp.Quit
    31. Set ExApp = Nothing
    32. End Sub

    Higlav schrieb:

    Visual Basic-Quellcode

    1. For Each WSh In ExBook.Sheets
    2. ExApp.DisplayAlerts = False
    3. For i = 1 To Len(WSh.Name) - Len(Wort)
    4. If Mid(WSh.Name, i, Len(Wort)) = Wort And i = Len(Wort) Then
    5. WSh.Delete
    6. End If
    7. Next
    8. Next
    Spätestens nach dem ersten Delete wirst du wohl Probleme bekommen, weil du dir mit Wsh den Ast abgesägt hast, auf dem du sitzt.
    Da sollte dann wenigstens ein "Exit For" rein.

    Besserer Vorschlag:

    Visual Basic-Quellcode

    1. For Each WSh In ExBook.Sheets
    2. If Instr(1, Wsh.Name, Wort) > 0 Then Wsh.Delete
    3. Next
    --
    If Not Program.isWorking Then Code.Debug Else Code.DoNotTouch
    --