Consulting

Results 1 to 6 of 6

Thread: How to close all .csv or .xlsx type file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Regular
    Joined
    Mar 2024
    Posts
    20
    Location

    How to close all .csv or .xlsx type file

    Hi sirs
    i want to close all csv and xlsx type file, when i restart program. i use * to filter, but it looks some problem... please give me some suggest, thanks.


    Sub check_menu()
       Dim wb As Workbook
       'ThisWorkbook.Path:傳回本工作簿、檔案所儲存的路徑。
       'ThisWorkbook.FullName:傳回本工作簿、檔案所儲存的路徑 + 檔名。
       'ThisWorkbook.Name:傳回本工作簿、檔案所儲存的檔名。
       'ActiveWindow.ActiveSheet.Name 或 ActiveSheet.Name 目前工作表名稱
       work_book = ThisWorkbook.Name
    '檔案檢查
       If Dir("C:\stock", vbDirectory) = "" Then MkDir "C:\stock"
    If Dir("C:\stock\analysis", vbDirectory) = "" Then MkDir "C:\stock\analysis"
       If Dir("C:\stock\data", vbDirectory) = "" Then MkDir "C:\stock\data"  
    For file_check = 1 To Workbooks.Count
        If Workbooks(file_check).Name = "*.csv" Then
            Windows("*.csv").Activate
            Application.DisplayAlerts = False
            Windows("*.csv").Close (vb = yes)
            Application.DisplayAlerts = True
        End If
       Next file_check
    For file_check = 1 To Workbooks.Count
        If Workbooks(file_check).Name = "*.xlsx" Then
           Windows("*.xlsx").Activate
            Application.DisplayAlerts = False
            Windows("*.xlsx").Close (vb = no)
            Application.DisplayAlerts = True
        End If
       Next file_check
    '舊檔案刪除
    If Len(Dir("C:\stock\*.*")) > 0 Then Kill "C:\stock\*.*"
    'SetAttr "C:\stock\*.*", vbNormal   ' 排除唯讀檔問
    If Len(Dir("C:\stock\data\*.*")) > 0 Then Kill "C:\stock\data\*.*"
    'SetAttr "C:\stock\data\*.*", vbNormal   ' 排除唯讀檔問題
    If Len(Dir("C:\stock\analysis\*.*")) > 0 Then Kill "C:\stock\analysis\*.*"
    SetAttr "C:\stock\analysis\*.*", vbNormal   ' 排除唯讀檔問題
    End Sub
    Last edited by Aussiebear; 04-06-2024 at 01:08 AM. Reason: Added code tags o supplied code (yet again....)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •