2017-07-06 17 views
0

私はオープンされた元のファイルを変数fileNameとして保存し、わずかに異なる名前の新しいバージョンのファイルを保存するためのコードを記述しました。私は元のファイルを殺すためのコードを書いていますが、それはまったく振る舞いません。vba変数を使用してファイルを削除する

キルラインにファイルが見つかりません。

'Deletes the original copy 
    Kill "S:\MERIT OUTPUTS FOLDER\MSI Recruitment Limited\" & fileName & 
    ".txt" 


    Sub BACSConversion() 

    Dim MyNewBook As String 
    Dim MySaveFile As String 
    Dim fileToOpen As Variant 
    Dim fileName As String 
    Dim sheetName As String 
    Dim rCopy As Range 

    'Turn off display alerts 
    Application.DisplayAlerts = False 
    'Turn off screen updates 
    Application.ScreenUpdating = False 

    'Ensures that the file open directory is always the same 
    ChDir "S:\MERIT OUTPUTS FOLDER\MSI Recruitment Limited\" 

    'Opens the folder to location to select txt file 
    fileToOpen = Application.GetOpenFilename("Text Files (*.txt), *.txt") 
    If fileToOpen <> False Then 
    Workbooks.OpenText fileName:=fileToOpen, _ 
    DataType:=xlDelimited, Tab:=True 
    End If 
'Creates the file name based on txt file name 
fileName = Mid(fileToOpen, InStrRev(fileToOpen, "\") + 1) 
'Creates the sheet name based on the active txt file 
sheetName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) 

    'Rename the original text file 
    ActiveWorkbook.SaveAs ("S:\MERIT OUTPUTS FOLDER\MSI Recruitment 
    Limited\" 
    & "DNU_" & fileName & ".txt") 

'Save active file as... 
ActiveWorkbook.SaveAs ("S:\MERIT OUTPUTS FOLDER\MSI Recruitment 
Limited\BACS File Original\" & _ 
    fileName & ".CSV"), FileFormat:=xlCSV 

'Selects all data in column A and copies to clipboard 
    Set rCopy = Range("A1", Range("A1").End(xlDown)) 

'Open the original document where the BACS file is located 
    Workbooks.Open "S:\Accounts (New)\Management Information 
(Analysis)\Phil 
    Hanmore - Analysis\bacs conversation calc.xlsx" 
'Selects the worksheet called "Original" 
    Sheets("Original").Range("A:A").ClearContents 

    'Paste selected values from previous sheet 
    rCopy.Copy 
    Sheets("Original").Range("A1").PasteSpecial Paste:=xlPasteValues 

    'Selects appropriate worksheet - Non-MyPayFINAL 
    Sheets("Non-MyPay FINAL").Select 

    'Selects all data in column A and copies to clipboard 
    Range("A1", Range("A1").End(xlDown)).Select 
    Selection.Copy 

'Add a new workbook 
    Workbooks.Add 
'Paste selected values from previous sheet 
    Selection.PasteSpecial Paste:=xlPasteValues 

'Build SaveAs file name (for CSV file) 
    MySaveFile = Format(Now(), "DDMMYYYY") & "NonMyPayFINAL" & ".CSV" 
'Save template file as...(for CSV file) 
    ActiveWorkbook.SaveAs ("S:\MERIT OUTPUTS FOLDER\MSI Recruitment 
    Limited\" 
    & MySaveFile), FileFormat:=xlCSV 

    'Build SaveAs file name (for Txt file) 
    MySaveFile = Format(Now(), "DDMMYYYY") & "NonMyPayFINAL" & ".Txt" 
'Save template file as...(for Txt file) 
    ActiveWorkbook.SaveAs ("S:\MERIT OUTPUTS FOLDER\MSI Recruitment 
    Limited\" 
    & MySaveFile), FileFormat:=xlTextWindows 

    'Close the new saved file 
    ActiveWorkbook.Close 

    'Selects appropriate worksheet - MyPayFINAL 
    Sheets("MyPay FINAL").Select 

    'Selects all data in column A and copies to clipboard 
    Range("A1", Range("A1").End(xlDown)).Select 
    Selection.Copy 

    'Add a new workbook 
    Workbooks.Add 
    'Paste selected values from previous sheet 
    Selection.PasteSpecial Paste:=xlPasteValues 

    'Build SaveAs file name (for CSV file) 
    MySaveFile = Format(Now(), "DDMMYYYY") & "MyPayFINAL" & ".CSV" 
    'Save template file as...(for CSV file) 
    ActiveWorkbook.SaveAs ("S:\MERIT OUTPUTS FOLDER\MSI Recruitment 
    Limited\" 
    & MySaveFile), FileFormat:=xlCSV 

    'Build SaveAs file name (for Txt file) 
    MySaveFile = Format(Now(), "DDMMYYYY") & "MyPayFINAL" & ".Txt" 
    'Save template file as...(for Txt file) 
    ActiveWorkbook.SaveAs ("S:\MERIT OUTPUTS FOLDER\MSI Recruitment 
    Limited\" 
    & MySaveFile), FileFormat:=xlTextWindows 

    'Close the new saved file 
    ActiveWorkbook.Close 
    'Close original source workbook (template) 
    Workbooks("bacs conversation calc").Close 
    'Close final workbook 
    ActiveWorkbook.Close savechanges:=True 
    'Deletes the original copy 
    Kill "S:\MERIT OUTPUTS FOLDER\MSI Recruitment Limited\" & fileName & 
    ".txt" 

    'Displays message box 
    MsgBox "Your file has been processed successfully!", vbExclamation 

    'Turn on display alerts 
    Application.DisplayAlerts = True 
'Turn on screen updates 
    Application.ScreenUpdating = True 

    End Sub 
+0

どのようなエラーが表示されますか?私はいくつかのファイルのエラーが見つかりませんと仮定したいのですか? – ainwood

+0

@ainwoodそれは正しいです、ファイルが見つかりません! – Dyhouse

+0

それが問題なのです。ファイル名の構造に何かがある - ファイル名に既にパスがついていますか? &smtifiedFile = "S:\ MERIT OUTPUTS FOLDER \ MSI Recruitment Limited \"&fileName& ".txt"そして次に 'Debug.Print sModifiedFile' – ainwood

答えて

0

@ainwoodの助けを借りて、私はその答えが本当に簡単だったことに気付きました。私がfilename変数を使用していたところでは、これはファイルが見つかりませんでした - fileToOpenを使って問題を完全に解決しました。

関連する問題