2016-04-21 49 views
2

第一次在这里(和新手编码器!)发布海报,所以我希望我能够很好地解释自己,因为我一直在挣扎在互联网上搜索时找到答案。在从vba创建的新工作簿中设置命名范围使其成为本地范围而不是全局范围

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

我做了一个工作簿,使她在点击一个按钮,指定所有她想要导入的时间表是一个目录,选择所有这些之后,它遍历各一个,发现DataRange和将其粘贴到一个新的工作簿中,并将其放入A列中,并将B列中的日期放入Timesheet_ 名称

我的下一步是允许使用此数据创建新表。在循环中创建一个新的命名范围,该范围涵盖从时间表工作簿粘贴的所有数据,并为其指定A列中所有内容的名称(因此,如果循环中的第一个时间表已粘贴,名称将为Timesheet_JohnSmith,范围将涵盖所有来自时间表工作簿的数据。)

这一切都很好,但是,我有一个问题,当这些新的命名范围被创建时,它们被设置为他们所在工作表的范围,而不是作为整体的工作簿。这意味着,如果您想在其他工作表中使用它们(这是我未来的意图,以便将它们创建到新表格中),则必须将它们引用为(例如,如果它们位于工作簿中的工作表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 
+0

尝试将名称添加到工作簿而不是工作表。 – brettdj

+0

请参阅[这里](http://www.thespreadsheetguru.com/blog/the-vba-guide-to-named-ranges)了解一个很好的解释和例子。您正在告诉它将其附加到表单上。 –

+0

@ScottCraner感谢您的链接,超级有用,现在似乎很明显,现在把它放在这里约2分钟:) – Clusks

回答

1

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

DestRange.Name = setUserName