gpt4 book ai didi

vba - 使用 vba name 语句将 xls 转换为 xlsx 会损坏文件

转载 作者:行者123 更新时间:2023-12-04 21:08:52 26 4
gpt4 key购买 nike

我正在尝试从 abcd.xls 重命名文件至xyz.xlsx使用以下代码 -

  NumOfAttachments = Range("RecCount").Value
DestinationFolderPath = Range("destinationfolder").Value
NewShtName = "Sheet1"

If NumOfAttachments <> 0 Then
For X = 0 To NumOfAttachments - 1
OrigName = Range("Startcell").Offset(X, 1).Value
NewName = Range("startcell").Offset(X, 2).Value
SourceFolderPath = Range("startcell").Offset(X, 3).Value
NewFile = DestinationFolderPath & NewName
If Dir(DestinationFolderPath & OrigName) <> "" Then Kill DestinationFolderPath & OrigName
FileCopy SourceFolderPath & OrigName, DestinationFolderPath & OrigName

If Dir(NewFile) <> "" Then Kill NewFile
Name DestinationFolderPath & OrigName As NewFile

在此之后,当我尝试打开文件( xyz.xlsx)时,我收到以下错误-
excel cannot open the file xyz.xlsx because the file format or file extension is not valid

最佳答案

原因很简单。您没有使用正确的文件格式。

您的 .SaveAs代码应该是这样的

.SaveAs "\myserver\test\xyz.xlsx", FileFormat:=51

有关文件格式,请参见下表
50 = xlExcel12 (Excel Binary Workbook in 2007-2010 with or without macro's, xlsb)
51 = xlOpenXMLWorkbook (without macro's in 2007-2010, xlsx)
52 = xlOpenXMLWorkbookMacroEnabled (with or without macro's in 2007-2010, xlsm)
56 = xlExcel8 (97-2003 format in Excel 2007-2010, xls)

会推荐这个 link

关于vba - 使用 vba name 语句将 xls 转换为 xlsx 会损坏文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15800044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com