0

現在のワークシートをコピーして空白の新しいワークブックに貼り付け、いくつかのセル (変数に格納されている) の値に応じて保存するコードがあります。

具体的には、サイト、クライアント、訪問日です。

サイトとクライアントではすべて正常に動作しますが、保存するファイル名に日付変数を含めると、エラーがスローされます: ランタイム エラー 76 - パスが見つかりません。

ヘルプ/アドバイスをいただければ幸いです。

Sub Pastefile()

Dim client As String
Dim site As String
Dim visitdate As String
client = Range("B3").Value
site = Range("B23").Value
screeningdate = Range("B7").Value

Dim SrceFile
Dim DestFile

SrceFile = "C:\2013 Recieved Schedules\schedule template.xlsx"
DestFile = "C:\2013 Recieved Schedules" & "\" & client & " " & site & " " & visitdate  & ".xlsx"

FileCopy SrceFile, DestFile

ActiveWindow.SmallScroll Down:=-12
Range("A1:I37").Select
Selection.Copy
ActiveWindow.SmallScroll Down:=-30
Workbooks.Open Filename:= _
    "C:\Schedules\2013 Recieved Schedules" & "\" & client & " " & site & " " &     visitdate & ".xlsx", UpdateLinks:= _
0
Range("A1:I37").PasteSpecial Paste:=xlPasteValues
Range("C6").Select
Application.CutCopyMode = False
ActiveWorkbook.Save
ActiveWindow.Close

End Sub
4

2 に答える 2

2

ファイル名に日付を使用する場合、現在のロケールに依存するため、日付のデフォルトのテキスト表現に依存することはありません。

そもそも日付を日付として保存し、ファイル名に対して安全な方法で明示的にフォーマットする必要があります。

Dim visitdate As Date
visitdate = Range("b7").Value

dim visitdate_text as string
visitdate_text = Format$(visitdate, "yyyy\-mm\-dd")

また、や\などの他の値からのような特殊文字を削除することを検討することもできます。そうしないと、問題が再び発生する可能性があります。clientsite

于 2013-03-18T12:08:51.430 に答える
0

コードの書き換えに関する私の提案は次のとおりです。

Sub Pastefile()

Dim client As String
Dim site As String
Dim visitdate As String
client = Range("B3").Value
site = Range("B23").Value
visitdate = Range("B7").Value

Dim SrceFile
Dim DestFile

If IsDate(visitdate) Then

SrceFile = "C:\2013 Received Schedules\schedule template.xlsx"
DestFile = "C:\2013 Received Schedules" & "\" & Trim(client) & " " & Trim(site) & " " & Str(Format(Now(), "yyyymmdd")) & ".xlsx"

If Trim(Dir("C:\2013 Received Schedules\schedule template.xlsx")) <> "" Then
FileCopy SrceFile, DestFile
Else
MsgBox (SrceFile & " is not available in the folder")
GoTo EndCode
End If

Range("A1:I37").Select
Selection.Copy

Workbooks.Open Filename:= _
"C:\Schedules\2013 Received Schedules" & "\" & client & " " & site & " " & visitdate & ".xlsx", UpdateLinks:= 0
Range("A1:I37").PasteSpecial Paste:=xlPasteValues
Range("C6").Select
Application.CutCopyMode = False
ActiveWorkbook.Save
ActiveWindow.Close

Else
MsgBox ("Please input the correct date in cell B7")
ActiveSheet.Range("B7").Activate
End If
EndCode:
End Sub
于 2013-03-18T12:20:11.027 に答える