更改文件名时自动更新macros

我有以下的macros来过滤我的目录与员工小时文件的具体数据,并将其放置到我的zmaster文件。 但是,我需要各种项目的各种主文档(例如更改名称为“项目300000”)。 当我将主文件名从zmaster更改为其他任何内容时,我的macros显然找不到相应的文件。

有没有一种方法来改变我的macros的方式,zmaster.xlsm在我的macros中被当前文件名自动replace?

Option Explicit Sub CopyToMasterFile() Dim MasterWB As Workbook Dim MasterSht As Worksheet Dim MasterWBShtLstRw As Long Dim FolderPath As String Dim TempFile Dim CurrentWB As Workbook Dim CurrentWBSht As Worksheet Dim CurrentShtLstRw As Long Dim CurrentShtRowRef As Long Dim CopyRange As Range Dim ProjectNumber As String FolderPath = "C:\test\" TempFile = Dir(FolderPath) Dim WkBk As Workbook Dim WkBkIsOpen As Boolean 'Check if zmaster is open already For Each WkBk In Workbooks If WkBk.Name = "zmaster.xlsm" Then WkBkIsOpen = True Next WkBk If WkBkIsOpen Then Set MasterWB = Workbooks("zmaster.xlsm") Set MasterSht = MasterWB.Sheets("Sheet1") Else Set MasterWB = Workbooks.Open(FolderPath & "zmaster.xlsm") Set MasterSht = MasterWB.Sheets("Sheet1") End If ProjectNumber = MasterSht.Cells(1, 1).Value Do While Len(TempFile) > 0 'Checking that the file is not the master and that it is a xlsx If Not TempFile = "zmaster.xlsm" And InStr(1, TempFile, "xlsx", vbTextCompare) Then Set CopyRange = Nothing 'Note this is the last used Row, next empty row will be this plus 1 With MasterSht MasterWBShtLstRw = .Cells(.Rows.Count, "A").End(xlUp).Row End With Set CurrentWB = Workbooks.Open(FolderPath & TempFile) Set CurrentWBSht = CurrentWB.Sheets("Sheet1") With CurrentWBSht CurrentShtLstRw = .Cells(.Rows.Count, "A").End(xlUp).Row End With For CurrentShtRowRef = 1 To CurrentShtLstRw If CurrentWBSht.Cells(CurrentShtRowRef, "A").Value = ProjectNumber Then 'This is set to copy from Column A to Column L as per the question If CopyRange Is Nothing Then 'If there is nothing in Copy range then union wont work 'so first row of the work sheet needs to set the initial copyrange Set CopyRange = CurrentWBSht.Range("A" & CurrentShtRowRef & _ ":L" & CurrentShtRowRef) Else 'Union is quicker to be able to copy from the sheet once Set CopyRange = Union(CopyRange, _ CurrentWBSht.Range("A" & CurrentShtRowRef & _ ":L" & CurrentShtRowRef)) End If ' ending If CopyRange Is Nothing .... End If ' ending If CurrentWBSht.Cells.... Next CurrentShtRowRef CopyRange.Select 'add 1 to the master file last row to be the next open row CopyRange.Copy MasterSht.Cells(MasterWBShtLstRw + 1, 1) CurrentWB.Close savechanges:=False End If 'ending If Not TempFile = "zmaster.xlsx" And .... TempFile = Dir Loop ActiveSheet.Range("A1:L200").RemoveDuplicates Columns:=Array(1, 2, 4, 8, 9, 10, 11, 12), Header:=xlYes End Sub 

从硬编码工作簿名称转义的一种方法是使用ActiveWorkbookThisWorkbook对象 – 它们都返回Workbook对象的实例。

的ThisWorkbook

返回一个Workbook对象,该对象表示当前macros代码正在运行的工作簿。 只读。

ActiveWorkbook

返回一个Workbook对象,该对象表示活动窗口中的工作簿(顶部的窗口)。 只读。 如果没有窗口打开,或者信息窗口或剪贴板窗口是活动窗口,则返回Nothing。

然后,您可以使用返回的Workbook对象的Name属性来获取工作簿的名称


另一种方法可能是,如果你将这样的数据作为parameter passing给你的函数。 例如:

 Sub CopyToMasterFile(wbName as String, sheetName as String) 

在这个变种中,如果你从另一个macros代码中调用你的Sub ,你可以传递你想要使用的任何东西 – 这样你就可以逃脱函数中的硬编码的东西。

这对工作表对象也是有效的 – 看看ActiveSheet