在从vba创build的新工作簿中设置命名范围使其成为本地范围而不是全局范围

第一次在这里(和一个新手编码!)的海报,所以我希望我可以解释自己,因为我一直在努力寻找在互联网上search这个答案。

我公司有一个非常简单的时间表系统在使用Excel的时刻,每个人都在本周末发送一个预定义的工作簿,并列出他们已经完成的任务,分成半天(所以可以说周五上午 – pipe理员,周五下午 – 分析,例如)。 为了让那些必须将所有这些数据用于项目成本的人变得简单,我在人们使用的名为DataRange的时间表中做了一个命名范围,然后可以在VBA中调用该范围。

我已经创build了一个工作簿,允许她点击一个button,并指定一个目录,以便在其中导入所有时间表,并在select所有这些后,循环遍历每一个,findDataRange并将其粘贴到一个新的工作簿,一个接一个地将Timesheet_ Name放在A列中,将date放在B列中。

我的下一步是允许使用这些数据创build新表。 在循环内创build一个新的命名范围,覆盖从时间表工作簿中粘贴的所有数据,并给出它在A列中的任何名称(因此,如果循环中的第一个时间表被粘贴,名称将是Timesheet_JohnSmith,范围将涵盖来自时间表工作簿的所有数据。)

这一切都很好,但是,我有一个问题,当这些新的命名范围创build时,他们被设置为他们所在的工作表范围,而不是作为一个整体的工作簿。 这意味着如果您想在其他工作表中使用它们(这是我将来的意图,以便将它们创build到新表格中),则必须将它们引用为(例如,如果它们位于工作簿中的工作表1上)Sheet1 Timesheet_JohnSmith而不只是Timesheet_JohnSmith。

我的代码如下,它是这样的: SummarySheet.Names.Add Name:= setUserName,RefersTo:= DestRange其中设置了新的命名范围。 我想知道的是为什么它将其设置在工作表的范围内,并且是否有简单的方法将其更改为整个工作簿的范围。 谢谢!

Sub MergeSelectedWorkbooks() Dim SummarySheet As Worksheet Dim FolderPath As String Dim SelectedFiles() As Variant Dim NRow As Long Dim FileName As String Dim getUserFileName() As String Dim setUserName As String Dim NFile As Long Dim WorkBk As Workbook Dim SourceRange As Range Dim DestRange As Range ' Create a new workbook and set a variable to the first sheet. Set SummarySheet = Workbooks.Add(x1WBATWorksheet).Worksheets(1) SummarySheet.SaveAs ("SummaryTest") Workbooks("SummaryTest.xlsx").Activate ActiveWorkbook.Sheets.Add ' Modify this folder path to point to the files you want to use. FolderPath = Worksheets("Summary").Range("FilePath") ' Set the current directory to the the folder path. ChDrive FolderPath ChDir FolderPath ' Open the file dialog box and filter on Excel files, allowing multiple files ' to be selected. SelectedFiles = Application.GetOpenFilename( _ filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True) ' NRow keeps track of where to insert new rows in the destination workbook. NRow = 1 ' Loop through the list of returned file names For NFile = LBound(SelectedFiles) To UBound(SelectedFiles) ' Set FileName to be the current workbook file name to open. FileName = SelectedFiles(NFile) ' Open the current workbook. Set WorkBk = Workbooks.Open(FileName) ' Get the file name to be used for column A, removing the path and file extension getUserFileName = Split(FileName, "\") setUserName = getUserFileName(UBound(getUserFileName)) setUserName = Replace(setUserName, ".xlsx", "") ' Set the cell in column A to be the file name. SummarySheet.Range("A" & NRow).Value = setUserName SummarySheet.Range("B" & NRow).Value = Date ' Set the source range to be A9 through C9. ' Modify this range for your workbooks. It can span multiple rows. Set SourceRange = WorkBk.Worksheets(1).Range("DataRange") ' Set the destination range to start at column B and be the same size as the source range. Set DestRange = SummarySheet.Range("C" & NRow) Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _ SourceRange.Columns.Count) ' Copy over the values from the source to the destination. DestRange.Value = SourceRange.Value 'Create the name range in the new workbook to be used for future calculations SummarySheet.Activate SummarySheet.Names.Add Name:=setUserName, RefersTo:=DestRange ' Increase NRow so that we know where to copy data next. NRow = NRow + DestRange.Rows.Count ' Close the source workbook without saving changes. WorkBk.Close savechanges:=False Next NFile ' Call AutoFit on the destination sheet so that all data is readable. SummarySheet.Columns.AutoFit End Sub 

这是做你告诉它 – 这是将名称添加到工作表,而不是工作簿。 你可以使用:

 DestRange.Name = setUserName