简体   繁体   中英

Save new file with filename cell value

I am working on making a universal production time sheet( wbTime ) for each dept that will work across all shifts and lines. I have where all the necessary information is required to be entered, all the data getting copied into a table in another workbook( wbLog ) and saved to be able to do analysis on the production data.

However, when it gets to trying to save the actual time sheet in the proper folder according to shift and machine line I start running into problems. I have it pulling part of the path from certain cells and the filename form the date the enter. It is getting to the last line and throwing a run-time error 1004 "Method 'SaveAs' of object_Worbook'failed".

I have only been playing with vba for 2 months so it is probably something small that I just do not see...

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

a) Don't use Range.Text , use Range.Value2 .
Text will give you exactly what is written in the cell, and if the cell diplays ### because your cell is to narrow to display a number, it will give you ### .

b) Put a statement Debug.print path before the SaveAs and check in the immediate window (Ctrl+G) if the path is exactly what you expect.

c) Be sure that when you issue the SaveAs -command, the same file is not already open in Excel - this happens often when you test your code repeatedly (it may still open from the last test). SaveAs saves a copy of the file and keeps it open!

d) Use FileFormat:=xlOpenXMLWorkbook when you name the file with extension xlsx . xlNormal will save the file with the old Excel file format and expects xls as extension.

e) Try to save the file with exactly the name from the Excel SaveAs dialog to see if the filename is okay and you have permission to save a file.

You are using as name of file the text 2/5/2019.xlsx . As far as I know, the simbol / cannot be used in Windows to name a file.

Try with a different name for file. Something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM