第一次在這裏(和新手編碼器!)發佈海報,所以我希望我能夠很好地解釋自己,因爲我一直在掙扎在互聯網上搜索時找到答案。在從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
嘗試將名稱添加到工作簿而不是工作表。 – brettdj
請參閱[這裏](http://www.thespreadsheetguru.com/blog/the-vba-guide-to-named-ranges)瞭解一個很好的解釋和例子。您正在告訴它將其附加到表單上。 –
@ScottCraner感謝您的鏈接,超級有用,現在似乎很明顯,現在把它放在這裏約2分鐘:) – Clusks