Vba 覆盖初始文件名

Vba 覆盖初始文件名,vba,excel,Vba,Excel,通过一些地方的帮助,我已经编写了下面的代码,这些代码工作得非常好,但是我需要能够覆盖初始文件名(我们被要求使用GetOpenFilename选择的文件名)以包括在内!DNU!因此,用户在选择后知道,不要再次选择同一个文件,尤其是因为他们将使用的文件都非常相似。你可以看到,我试着用下面的行“重命名原始文本文件,但它没有做任何事情!在此方面的任何帮助都将不胜感激 Sub BACSConversion() Dim MyNewBook As String Dim MySaveFile As St

通过一些地方的帮助,我已经编写了下面的代码,这些代码工作得非常好,但是我需要能够覆盖初始文件名(我们被要求使用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
Sub-BACSConversion()
将MyNewBook设置为字符串
将MySaveFile设置为字符串
Dim fileToOpen作为变体
将文件名设置为字符串
将sheetName设置为字符串
变暗复制为范围
'关闭显示警报
Application.DisplayAlerts=False
'关闭屏幕更新
Application.ScreenUpdating=False
'确保文件打开目录始终相同
ChDir“S:\MERIT OUTPUTS文件夹\MSI招聘有限公司”
'将文件夹打开到要选择txt文件的位置
fileToOpen=Application.GetOpenFilename(“文本文件(*.txt),*.txt”)
如果fileToOpen为False,则
Workbooks.OpenText文件名:=fileToOpen_
数据类型:=xlDelimited,制表符:=True
如果结束
'根据txt文件名创建文件名
fileName=Mid(fileToOpen,InStrRev(fileToOpen,“\”)+1)
'根据激活的txt文件创建图纸名称
sheetName=Left(ActiveWorkbook.Name,Len(ActiveWorkbook.Name)-4)
'重命名原始文本文件
ActiveWorkbook.SaveAs(“S:\MERIT OUTPUTS文件夹\MSI招聘
有限“&sheetName&”!DNU!&“.txt”)
'将活动文件另存为。。。
ActiveWorkbook.SaveAs(“S:\MERIT OUTPUTS文件夹\MSI招聘
有限\BACS文件原件\“&”_
文件名和“.CSV”),文件格式:=xlCSV
'选择列A中的所有数据并复制到剪贴板
设置rCopy=范围(“A1”,范围(“A1”)。结束(xlDown))
'打开BACS文件所在的原始文档
工作簿。打开“S:\Accounts(New)\Management Information
(分析)\Phil Hanmore-Analysis\bacs conversation calc.xlsx“
'选择名为“原始”的工作表
纸张(“原件”)。范围(“A:A”)。清晰内容
'粘贴上一工作表中的选定值
复印件,复印件
纸张(“原始”)。范围(“A1”)。粘贴特殊粘贴:=XLPasteValue
'选择适当的工作表-非MyPayFINAL
工作表(“非MyPay最终版”)。选择
'选择列A中的所有数据并复制到剪贴板
范围(“A1”,范围(“A1”)。结束(xlDown))。选择
选择,复制
'添加新工作簿
工作手册。添加
'粘贴上一工作表中的选定值
Selection.Paste特殊粘贴:=xlPasteValues
'生成另存为文件名(用于CSV文件)
MySaveFile=Format(Now(),“DDMMYYYY”)和“NonMyPayFINAL”和“.CSV”
'将模板文件另存为…(对于CSV文件)
ActiveWorkbook.SaveAs(“S:\MERIT OUTPUTS文件夹\MSI招聘
有限\“&MySaveFile),文件格式:=xlCSV
'生成另存为文件名(用于Txt文件)
MySaveFile=Format(Now(),“DDMMYYYY”)和“NonMyPayFINAL”和“.Txt”
'将模板文件另存为…(对于Txt文件)
ActiveWorkbook.SaveAs(“S:\MERIT OUTPUTS文件夹\MSI招聘
有限\“&MySaveFile),文件格式:=xlTextWindows
'关闭新保存的文件
活动工作簿。关闭
'选择适当的工作表-MyPayFINAL
工作表(“MyPay FINAL”)。选择
'选择列A中的所有数据并复制到剪贴板
范围(“A1”,范围(“A1”)。结束(xlDown))。选择
选择,复制
'添加新工作簿
工作手册。添加
'粘贴上一工作表中的选定值
Selection.Paste特殊粘贴:=xlPasteValues
'生成另存为文件名(用于CSV文件)
MySaveFile=Format(Now(),“DDMMYYYY”)&“MyPayFINAL”和“.CSV”
'将模板文件另存为…(对于CSV文件)
ActiveWorkbook.SaveAs(“S:\MERIT OUTPUTS文件夹\MSI招聘
有限\“
&MySaveFile),文件格式:=xlCSV
'生成另存为文件名(用于Txt文件)
MySaveFile=Format(Now(),“DDMMYYYY”)和“MyPayFINAL”和“.Txt”
'将模板文件另存为…(对于Txt文件)
ActiveWorkbook.SaveAs(“S:\MERIT OUTPUTS文件夹\MSI招聘
有限\“&MySaveFile),文件格式:=xlTextWindows
'关闭新保存的文件
活动工作簿。关闭
'关闭原始源工作簿(模板)
工作手册(“bacs对话计算”)。关闭
'关闭最终工作簿
ActiveWorkbook.Close savechanges:=True
MsgBox“您的文件已成功处理!”,VBEQUOTE
'打开显示警报
Application.DisplayAlerts=True
'打开屏幕更新
Application.ScreenUpdating=True
端接头
子文件名更改()
将旧路径设置为字符串
将newPath设置为字符串
oldPath=“S:\Accounts(New)\Management Information(Analysis)\菲尔
Hanmore-Analysis\Neil Test\“&Test&.xlsx”
newPath=“S:\Accounts(New)\Management Information(Analysis)\菲尔
Hanmore-Analysis\Neil Test\“&Test&”!DNU!.xlsx”
端接头

VBA中有一个名为
Name
的内置函数,其工作原理如下:

Name [old/current path/name] [new path/name]
因此,对于您的代码,您需要执行以下操作:

Name ("S:\MERIT OUTPUTS FOLDER\MSI Recruitment Limited\" & Filename) ("S:\MERIT OUTPUTS FOLDER\MSI Recruitment Limited\" & Filename & "!DNU!")
我建议将变量分配给路径,称之为oldpath和newpath。所以

Dim oldPath As String, newPath as String

oldPath = "S:\MERIT OUTPUTS FOLDER\MSI Recruitment Limited\" & sheetname & ".txt"
newPath = "S:\MERIT OUTPUTS FOLDER\MSI Recruitment Limited\" & sheetname & "!DNU!.txt"

Name oldPath newPath
但是,需要先关闭文件,然后才能执行此操作。因此,通过循环打开的工作簿并将其全部关闭,确保文件已关闭。然后通过运行此命令,它应该将文件从旧名称重命名为新名称


我建议您制作一份新的工作簿,并将其放在桌面上,在将其与真正的代码/工作簿一起使用之前先进行测试。创建一个新工作簿,将其保存到桌面,将其命名为test.xlsx,然后将其关闭。在一个单独的工作簿中,启动一个新的子文件并粘贴代码,但更改oldPath和newPath以反映桌面路径和test.xlsx文件。试一试。

VBA中有一个名为
Name
的内置函数,其工作原理如下:

Name [old/current path/name] [new path/name]
因此,对于您的代码,您需要执行以下操作:

Name ("S:\MERIT OUTPUTS FOLDER\MSI Recruitment Limited\" & Filename) ("S:\MERIT OUTPUTS FOLDER\MSI Recruitment Limited\" & Filename & "!DNU!")
我建议将变量分配给路径,称之为oldpath和newpath。所以

Dim oldPath As String, newPath as String

oldPath = "S:\MERIT OUTPUTS FOLDER\MSI Recruitment Limited\" & sheetname & ".txt"
newPath = "S:\MERIT OUTPUTS FOLDER\MSI Recruitment Limited\" & sheetname & "!DNU!.txt"

Name oldPath newPath
但是,需要先关闭文件,然后才能执行此操作。因此,确保通过循环打开的工作簿和CLO来关闭文件