0
在地方提供了一些幫助之後,我寫了下面的代碼,它完美地工作,但我需要能夠覆蓋最初的文件名(我們被要求用GetOpenFilename選擇的那個)來包含!DNU!所以用戶在選擇它之後就知道了,而不是再次選擇相同的文件,特別是他們將使用的文件都非常相似。你可以看到我試着用'重命名原始文本文件'下面的行,但它什麼也沒做!任何幫助,將不勝感激。覆蓋初始文件名
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" & sheetName & "!DNU!" & ".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
MsgBox "Your file has been processed successfully!", vbExclamation
'Turn on display alerts
Application.DisplayAlerts = True
'Turn on screen updates
Application.ScreenUpdating = True
End Sub
Sub FileNameChange()
Dim oldPath As String
Dim newPath As String
oldPath = "S:\Accounts (New)\Management Information (Analysis)\Phil
Hanmore - Analysis\Neil Test\" & Test & ".xlsx"
newPath = "S:\Accounts (New)\Management Information (Analysis)\Phil
Hanmore - Analysis\Neil Test\" & Test & "!DNU!.xlsx"
End Sub
@馬特杯我必須在這裏做錯事,因爲那種方法根本不適用於我。我想知道,因爲我已經管理過在名稱中創建帶有DNU的文件的另一個副本,只是刪除了原始文件,並將其中帶有DNU的副本刪除。這是否合理?將它應用到我的代碼中的最佳方式是什麼?出於某種原因,我實際上在文件名和表名中得到.txt,你能看到我需要刪除的內容嗎?我試着把這一行的一部分寫出來fileToOpen = Application.GetOpenFilename(「Text Files(* .txt),* .txt」),但它也沒有工作! – Dyhouse
@Dyhouse學會使用調試器。使用F9設置/清除斷點,F8跳過,Shift + F8跳過,您可以拖動黃線到您打入的範圍內的任何可執行指令。使用即時窗格(Ctrl + G )和* locals *工具窗口來檢查變量的值以及它們在分配時的變化。 –
@Dyhouse你收到一個錯誤?或者它只是完全不工作。你可以編輯你的原始文章並顯示你正在嘗試使用Name的完整代碼嗎? – Busse