【问题标题】:Save new file with filename cell value使用文件名单元格值保存新文件
【发布时间】:2019-02-05 22:49:04
【问题描述】:

我正在为每个部门制作一个通用的生产时间表(wbTime),该时间表将适用于所有班次和生产线。我有需要输入所有必要信息的地方,所有数据都被复制到另一个工作簿(wbLog)的表中并保存以便能够对生产数据进行分析。

但是,当尝试根据班次和机器生产线将实际时间表保存在正确的文件夹中时,我开始遇到问题。我让它从某些单元格中提取部分路径,文件名形成输入日期。它正在到达最后一行并引发运行时错误 1004“object_Worbook'的方法'SaveAs'失败”。

我只玩了两个月的 vba,所以它可能是我看不到的小东西......

Sub TransferData()

   If ActiveSheet.Range("E2").Value = "" Then
        MsgBox "Operator Name Required", vbInformation, "ALERT: Missing Information"
    Cancel = True
   Exit Sub
   End If

   If ActiveSheet.Range("H2").Value = "" Then
        MsgBox "Date Required", vbInformation, "ALERT: Missing Information"
    Cancel = True
   Exit Sub
   End If

   If ActiveSheet.Range("K2").Value = "" Then
        MsgBox "Shift Required", vbInformation, "ALERT: Missing Information"
    Cancel = True
   Exit Sub
   End If

   If ActiveSheet.Range("M2").Value = "" Then
       MsgBox "Line Required", vbInformation, "ALERT: Missing Information"
    Cancel = True
   Exit Sub
   End If



   Dim wbTime As Workbook
   Set wbTime = ThisWorkbook
   Dim wbData As Workbook
   Dim LastRow As Long

   Set wbTime = ActiveWorkbook
   With wbTime.Sheets("Production Time Sheet")
       LastRow = .Range("E" & .Rows.Count).End(xlUp).Row
   End With
   wbTime.Sheets("Production Time Sheet").Range("A6:R" & LastRow).Copy

   Set wbData = Workbooks.Open("S:\Lean Carrollton Initiative\Michael\Time Sheet Data LT Test.xlsm")
   Set wbData = ActiveWorkbook
   wbData.Worksheets("Log").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues

   wbData.Close SaveChanges:=True



   Dim Fname As String
   Dim Path As String
   Dim shft As String
   Dim Line As String

Set wbTime = ActiveWorkbook
    Fname = Sheets("Production Time Sheet").Range("I2").Text

    shft = Sheets("Production Time Sheet").Range("Z9").Text

    Line = Sheets("Production Time Sheet").Range("AC11").Text

    Path = "K:\Groups\OFS Time Sheets\8hr Production Schedule\LT Jacketing\" & shft & Line & Fname & ".xlsx"

ActiveWorkbook.SaveAs filename:=Path, FileFormat:=xlNormal

End Sub

【问题讨论】:

  • 可能在尝试另存为之前添加Msgbox PathDebug.Print Path,以查看Path 的内容是什么。希望这会显示您的问题。如果没有,请尝试手动 SaveAs 到确切的文件路径和名称,看看是否会收到更有用的错误。
  • 我运行了debug.print,路径对我来说看起来是正确的。 'K:\Groups\OFS 时间表\8 小时生产计划\LT Jacketing\1st\J4\2/5/2019.xlsx'
  • 想通了!这是日期的格式...文件名中不能有斜线...告诉你一些小事哈哈
  • @mdavis0510 这正是我在回答中所说的。很高兴你找到它
  • @FoxfireAndBurnsAndBurns 是的,我才恍然大悟,但如果我没有注意到,您的帮助将不胜感激!我想我在花了一个小时才注意到这一点后需要再喝一杯咖啡哈哈。

标签: excel vba save-as


【解决方案1】:

您使用文本2/5/2019.xlsx 作为文件名。据我所知,符号/不能在Windows中用于命名文件。

尝试为文件使用不同的名称。比如:

Fname = Replace(Sheets("Production Time Sheet").Range("I2").Text,"/","-")

【讨论】:

    【解决方案2】:

    a) 不要使用Range.Text,使用Range.Value2
    Text准确地告诉您单元格中的内容,以及单元格是否显示 ###因为您的单元格要缩小以显示数字,所以它会给您###

    b) 在SaveAs 之前添加声明Debug.print path 并检查即时窗口 (Ctrl+G) 是否符合您的预期。

    c) 确保当您发出SaveAs-命令时,同一个文件尚未在 Excel 中打开 - 当您重复测试代码时经常发生这种情况(它可能仍会从上次测试中打开)。 SaveAs 保存文件的副本并保持打开状态!

    d) 使用扩展名xlsx 命名文件时使用FileFormat:=xlOpenXMLWorkbookxlNormal 将使用旧的 Excel 文件格式保存文件,并期望 xls 作为扩展名。

    e) 尝试使用 Excel SaveAs 对话框中的名称保存文件,以查看文件名是否正确以及您是否有权保存文件。

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多