VBA - 由于命名错误无法保存工作簿。 (错误 1004)不确定如何修复程序

问题描述 投票:0回答:1

函数 export_to_csv(ws 作为工作表,文件名作为字符串,文件路径作为字符串,文件范围作为字符串) 将 LastRow 变暗

Application.ScreenUpdating = False

If ws.AutoFilterMode Then Cells.AutoFilter

ws.Activate
LastRow = ws.Range(Left(fileRange, 2) & Rows.Count).End(xlUp).Row
  
ws.Range(fileRange & LastRow).AutoFilter Field:=11, Criteria1:="<50"
On Error Resume Next
Set dataRange = ws.Range(fileRange & LastRow).SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If dataRange Is Nothing Then
    MsgBox ("No record!")
Else
    dataRange.Copy
    '--- create a temporary workbook that will be saved as a XLSX format
    Dim tempWB As Workbook
    Set tempWB = Application.Workbooks.Add(1)
    tempWB.Sheets(1).Activate
    With tempWB.Sheets(1).Range("A1")
        .PasteSpecial xlPasteValues
    End With
    
    tempWB.Sheets(1).Range("A:A").NumberFormat = "Text"
    tempWB.Sheets(1).Range("f:f").NumberFormat = "0.0000"
    Application.DisplayAlerts = False
    tempWB.SaveAs filename:=filepath & filename, FileFormat:=51, _
                  CreateBackup:=False, Local:=True
    tempWB.Close SaveChanges:=False
    Application.DisplayAlerts = True
    MsgBox (filename & " exported!")
End If

ws.Cells.AutoFilter
Application.ScreenUpdating = True

我有一个宏,它实际上是提取一部分记录(1-50、50-100 等)并将其保存在自己的工作簿中。创建文件路径的日期(2022 年 8 月)工作正常,但是当我将其更新到当前月份(2023 年 5 月)时,代码中断,我不能再使用宏了。

我试图将工作簿从 Active 更改为 Current,因为这似乎是其他人遇到的问题,但并没有太大的区别。我认为问题可能与路由回文件路径和文件名有关,但我不确定当代码目前处于临时状态时如何从另一个工作簿中提取它。

excel vba path save filenames
1个回答
0
投票

文件的完整路径和/或名称有误, 可以是:

  • 不允许使用的字符
 < (less than)
 > greater than
 : (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)
  • 紧跟两个反斜杠
  • 目录中不存在定义的名称
© www.soinside.com 2019 - 2024. All rights reserved.