簡體   English   中英

無法工作簿。打開 XLS 文件類型?

[英]Can't Workbook.Open an XLS File Type?

卡在這個了。 我正在創建一個 2016 Excel 宏,它(希望)遍歷文件夾中的所有 .xls 文件並進行一些格式更改,但是我的 workbook.open 不會打開 .xls 文件。 它似乎只是瞥了一眼? 我已取消選中信任中心中的所有框,那么為什么這不會在我的 VBA 代碼中打開 .xls 文件? 還有什么我可以嘗試的嗎? 如果文件是 .xlsx 格式,但由於某種原因不是 .xls 格式,它會起作用。 相關代碼部分如下。 謝謝!

更新

根據要求,我將嘗試在這里獲得更多技術。 我在這里添加了一個完整的腳本(沒有它所做的任何格式化的東西),以便有人與我一起進行故障排除。

這里值得注意的是:

盡管我將它設置為僅在循環中查找.xls文件類型,但出於某種原因,它會在當前宏.xlsm工作簿中執行我的所有格式化命令,而它不應該這樣做。 因此,作為一種解決方法,我還包含了該行

' Start a loop for all .xlsx files in the folder (defined in the last step)
Do While Filename <> "" And Filename <> "Bid_Report_Macro.xlsm" ' This workbook's name

以防止在當前工作簿中發生這種情況。 我會注意到,一旦我在同一個工作簿中單擊此按鈕,我的所有格式化內容都會執行,而不是它應該首先打開的 .xls 文件。 不要問我為什么會這樣,因為我真的不知道。

此外,我嘗試打開的.xls文件是Microsoft Excel 97-2003 Worksheet (.xls)文件。 我已取消選中信任中心中有關阻止打開某些文件類型/文件阻止的所有框。 如果我運行相同的代碼,但將所有.xlsx文件放在一個文件夾中,它將起作用。 所以我不知道為什么它不適用於.xls文件。 當我單擊我的宏按鈕時,沒有任何反應。

我試圖在 open 函數中包含CorruptLoad:=xlExtractDataCorruptLoad:=xlRepairFile ,但這也沒有做任何事情。

我已經嘗試了下面顯示的建議,但沒有奏效。 我假設它與我的本地機器有關,因為它是一台工作計算機,但我真的不知道在哪里看,我想完成這個項目。

有任何想法嗎? 謝謝。

Sub Bid_Report_Function()

' Create (but don't define yet) some variables used throughout
Dim folderPath As String
Dim Filename As String
Dim wb As Workbook
Dim SrchRng As Range, cel As Range
Dim EmptyDays As Integer

' Run it in the background.
Application.ScreenUpdating = False

' Directory of this macro that has the other .xls files
folderPath = Application.ActiveWorkbook.Path

' A conditional for finding the directory with proper structure
If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"

' Do this for each xls workbooks in the folder
Filename = Dir(folderPath & "*.xls")

' Start a loop for all .xlsx files in the folder (defined in the last step)
Do While Filename <> "" 'And Filename <> "Bid_Report_Macro.xlsm" ' This workbook's name

    ' Open a workbook in the folderPath
    Set wb = Workbooks.Open(folderPath & Filename, CorruptLoad:=xlRepairFile)

    ' Always start on the first worksheet of the opened workbook
    wb.Worksheets(1).Activate

    '......my formatting code is here. Skipping to the end...

    ' Rename the current tab
    wb.ActiveSheet.Name = "RENAMED TAB"


    ' Save as, a copy as a .xls file
    wb.SaveAs Filename:="TEST.xls"

    ' Close but don't save the current workbook to keep it the same
    wb.Close False


' This prevents the macro from doing all of the above endlessly.
Exit_Loop:
    Set wb = Nothing
    Filename = Dir

' Next workbook in the directory
Loop

' Can turn screen updating back on
Application.ScreenUpdating = True

' End program
End Sub

也許你的 Do 循環應該更像這樣:

' Start a loop for all .xlsx files in the folder (defined in the last step)
Do While Filename <> ""
    If Filename <> ThisWorkbook.Name Then 'This workbook's name
        ' Open a workbook in the folderPath
        Set wb = Workbooks.Open(folderPath & Filename, CorruptLoad:=xlRepairFile)
        ' Always start on the first worksheet of the opened workbook
        wb.Worksheets(1).Activate
    End If
    'get the next file in the folder which matches the Directory search
    Filename = Dir
Loop

我想到了。 雖然這不是一個很好的解決方案,但我通過實際定義我希望使用的 .xls 文件的完整文件名使其工作。 這不是很好,因為它不能識別文件夾中的所有 .xls 文件,從而使循環無用,但現在它可以工作。 這是工作代碼。 總有一天我會嘗試為它找到一個工作循環。 謝謝!

' Run it in the background.
Application.ScreenUpdating = False

' Directory of this macro that has the other .xls files
folderPath = Application.ActiveWorkbook.Path

' A conditional for finding the directory with proper structure
If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"

' Do this for each xls workbooks in the folder
Filename = Dir(folderPath & "translate_quote_42666432_v6_all.xls")

' Start a loop for all .xlsx files in the folder (defined in the last step)
Do While Filename <> ""

    ' Open a workbook in the folderPath
    Set wb = Application.Workbooks.Open(folderPath & Filename)

    ' Always start on the first worksheet of the opened workbook
    wb.Worksheets(1).Activate

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM