下面列舉一些名稱操作的VBA代碼示例,以更好地理解名稱。
代碼示例1:檢查當前工作簿中是否存在某名稱 下面的代碼給出了兩個測試名稱是否存在于工作簿中的函數(shù),過程test用于測試代碼。 Subtest() Dim bln As Boolean bln = NameIfExists('myName') If bln = True Then MsgBox '這個名稱在當前工作簿中已存在.' Else MsgBox '這個名稱不存在.' End If EndSub
FunctionNameIfExists(strFindName As String) As Boolean Dim rng As Range Dim myName As Range
On Error Resume Next
myName =ActiveWorkbook.Names(strFindName).Name If Err.Number = 0 Then NameIfExists = True EndFunction
FunctionNameExists(strName As String) As Boolean On Error Resume Next NameExists =Len(ThisWorkbook.Names(strName).Name) <> 0 EndFunction
代碼示例2:使工作簿中的所有名稱都可見
SubUnHideName() Dim nm As Name For Each nm In Names nm.Visible = True Next nm EndSub
代碼示例3:列出當前工作簿中所有名稱的相關(guān)信息
SubAllNamesInfo() Dim i As Integer For i = 1 To ActiveWorkbook.Names.Count On Error Resume Next Cells(i, 1) = ''' &ActiveWorkbook.Names(i).Name Cells(i, 2) = ''' &ActiveWorkbook.Names(i).RefersToRange.Address Cells(i, 3) = ''' &ActiveWorkbook.Names(i).ShortcutKey Cells(i, 4) = ''' &ActiveWorkbook.Names(i).Visible Next i EndSub 吳姐姐講歷史故事(精美盒裝,青少版,全15冊)作者:[臺灣] 吳涵碧 著 步印童書 出品
當當
廣告
購買
示例代碼4:列出所有隱藏的名稱 在“名稱框”或者“名稱管理器”對話框中不會顯示隱藏的名稱,下面的代碼在新工作表中列出所有隱藏的名稱。 SubListHideNames() Dim nm As Name, r As Long Worksheets.Add r = 1 For Each nm In ActiveWorkbook.Names If Not nm.Visible Then Cells(r, 1) = nm.Name Cells(r, 2) = ''' &nm.RefersTo r = r 1 End If Next nm EndSub
示例代碼5:顯示當前單元格所命名的名稱
SubShowActiveCellName() On Error Resume Next MsgBox ActiveCell.Name.Name Select Case Err.Number Case 0 Case 1004 MsgBox '單元格' & ActiveCell.Address(4) &'沒有被命名.' Case Else MsgBox Err.Number & ' -' & Err.Description End Select EndSub
示例代碼6:刪除當前工作簿中含有“excel”字符的名稱
SubDeleleNameWithString() Dim nm As Range For Each nm In ActiveWorkbook.Names If nm.Name Like '*excel*'Then nm.Delete End If Next nm EndSub 寫給兒童的中國歷史(全14冊)作者:陳衛(wèi)平著 步印童書 出品
當當
廣告
購買
示例代碼7:判斷某單元格或單元格區(qū)域是否與命名區(qū)域部分重疊 如果參數(shù)rng所代表的單元格或單元格區(qū)域與命名區(qū)域存在重疊,則返回命名區(qū)域的名稱,否則返回空。 FunctionNameOfParentRange(rng As Range) As String Dim nm As Name For Each nm In ThisWorkbook.Names If rng.Parent.Name =nm.RefersToRange.Parent.Name Then If Not Application.Intersect(rng,nm.RefersToRange) Is Nothing Then NameOfParentRange = nm.Name Exit Function End If End If Next nm NameOfParentRange = '' EndFunction
示例代碼8:為名稱框定義快捷鍵 可以使用VBA代碼為名稱框設(shè)置快捷鍵。在標準模塊中輸入下面的代碼: PublicDeclare Function SetFocus Lib 'user32' (ByVal hwnd As Long) As Long PublicDeclare Function FindWindow Lib 'user32' Alias'FindWindowA' _ (ByVal lpClassName As String, ByVallpWindowName As String) As Long PublicDeclare Function FindWindowEx Lib 'user32' Alias'FindWindowExA' _ (ByVal hWnd1 As Long, ByVal hWnd2 As Long,_ ByVal lpsz1 As String, ByVal lpsz2 AsString) As Long
SubSetFocusNameBox() Dim res As Long res = SetFocus( _ FindWindowEx( _ FindWindowEx( _ FindWindow('XLMAIN',Application.Caption) _ , 0, 'EXCEL;',vbNullString) _ , 0, 'combobox',vbNullString)) EndSub
在Excel中,選擇“開發(fā)工具”選項卡中的“宏”,調(diào)出“宏”對話框,選擇SetFocusNameBox過程,單擊“選項”,指定快捷鍵例如Ctrl Shift N。那么,在該工作簿中,按下Ctrl Shift N組合鍵,可直接定位到名稱框。
歡迎分享本文,轉(zhuǎn)載請注明出處。 歡迎在下面留言,完善本文內(nèi)容,讓更多的人學到更完美的知識。 |
|