繁体   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