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

在从vba创建的新工作簿中设置命名范围使其成为本地范围而不是全局范围,vba,excel,macros,Vba,Excel,Macros,第一次贴在这里(也是一个新手),所以我希望我能很好地解释我自己,因为我在网上搜索时一直在努力寻找答案 我的公司目前有一个非常简单的时间表系统,它使用Excel,每个人都会在周末发送一份预定义的工作簿,其中包含他们完成的任务列表,分半天发送(例如,可以说周五上午-管理,周五下午-分析)。为了使那些必须使用所有这些数据来计算项目成本的人的生活变得简单,我在人们使用的时间表中创建了一个命名范围,称为DataRange,然后可以在VBA中调用该范围 我制作了一个工作簿,允许她单击一个按钮并指定她要导入的

第一次贴在这里(也是一个新手),所以我希望我能很好地解释我自己,因为我在网上搜索时一直在努力寻找答案

我的公司目前有一个非常简单的时间表系统,它使用Excel,每个人都会在周末发送一份预定义的工作簿,其中包含他们完成的任务列表,分半天发送(例如,可以说周五上午-管理,周五下午-分析)。为了使那些必须使用所有这些数据来计算项目成本的人的生活变得简单,我在人们使用的时间表中创建了一个命名范围,称为DataRange,然后可以在VBA中调用该范围

我制作了一个工作簿,允许她单击一个按钮并指定她要导入的所有时间表所在的目录,选择所有这些后,它循环遍历每个时间表,找到数据范围并将其粘贴到一个新工作簿中,一个接一个地将时间表的名称放在a列中,和B列中的日期

我的下一步是允许使用此数据创建新表。在循环中,将创建一个新的命名范围,该范围将覆盖从时间表工作簿粘贴的所有数据,并为其提供a列中任何数据的名称(因此,如果在循环中粘贴第一个时间表,则该名称将为timesheet_JohnSmith,该范围将覆盖来自时间表工作簿的所有数据。)

这一切都很好,但是,我有一个问题,当这些新的命名范围被创建时,它们被设置为工作表的一个范围,而不是工作簿的一个整体。这意味着,如果您想在其他工作表中使用它们(这是我将来创建新表的目的),您必须将它们称为(例如,如果它们位于工作簿的工作表1中)工作表1!时间表而不仅仅是时间表

下面是我的代码,这是一行代码:SummarySheet.Names.Add Name:=setUserName,referesto:=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

尝试将名称添加到工作簿而不是工作表中。有关详细说明和示例,请参阅。您正在告诉它将其附加到一张纸上。@ScottCraner感谢您提供的链接,非常有用,现在在这里放置它大约2分钟后,它看起来非常明显:)