2017-03-08 64 views
0

我試圖刪除隱藏的Names,但有一個規則,我選擇隱藏的Name什麼不能刪除。試圖通過選擇刪除隱藏的名字excel宏

使用Microsoft提供的支持的代碼我設法讓在記錄表的名稱 的列表,並添加一列,當我進入1旁邊我要不要刪除名字,當我將其留空你希望它刪除名稱。從Microsoft支持

這裏是我的代碼:

Sub clean_names() 

Application.ScreenUpdating = False 
On Error Resume Next 

Set nms = ActiveWorkbook.Names 
MsgBox (nms.Count) 

For R = 1 To nms.Count 
    Name_Name = nms(R).Name 
    Name_Referance = nms(R).RefersTo 
    '###########ActiveWorkbook.Names(Name_Name).Delete 
    'ActiveWorkbook.nms(R).Delete 
    Sheets("LOG").Cells(R + 1, 1).Value = Name_Name 
    Sheets("LOG").Cells(R + 1, 2).Value = "'" + Name_Referance 
    'Application.StatusBar = R 
Next R 
'Application.StatusBar = False 
Application.ScreenUpdating = True 

End Sub 

'================================================================ 

Sub DelNames() 

Dim xName As Variant 
Dim Indx As Integer 
Dim Vis As Variant 

Cells(2, 1).Select 
If (ActiveCell = "") Then Exit Sub 
Indx = 1 

Do 
    If (ActiveCell.Offset(Indx, 2) = "") Then 

     xName = ActiveCell.Offset(Indx, 0).Value 
     If xName.Visible = True Then 
      Vis = "Visible" 
     Else 
      Vis = "Hidden" 
     End If 
     xName.Delete 
    End If 
    Indx = Indx + 1 
Loop While Len(ActiveCell.Offset(Indx, 0)) 

End Sub 

我怎樣才能讓這段代碼的工作?

回答

0

請嘗試下面的代碼,它會循環遍歷列A中的所有行,檢查列C是否爲空,並將從您的工作簿中刪除該Name

:我評論從原始代碼5條線路,因爲根據您的文章你不關心,如果NamesVisible與否,你要基於C列中的值刪除

代碼

Option Explicit 

Sub DelNames() 

Dim xName   As Name 
Dim Indx   As Long 
Dim Vis    As Variant 
Dim LastRow   As Long 

With Worksheets("LOG") 
    If IsEmpty(.Range("A2").Value) Then Exit Sub 

    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row '<-- get last row in column A (where you have a NamedRange) 
    For Indx = 2 To LastRow 
     If .Range("C" & Indx).Value = "" Then 
      ' set xName with the text entered in column A (as the Named Range Name) 
      Set xName = ThisWorkbook.Names(.Range("A" & Indx).Value) 

      ' not sure you need the 5 lines with the If criteria below so I Commented them for now 
      'If xName.Visible = True Then 
      ' Vis = "Visible" 
      'Else 
      ' Vis = "Hidden" 
      'End If 
      xName.Delete 
     End If 
    Next Indx 
End With 

End Sub 
+0

@Tal Kuce你試過上面我的代碼?如果它爲你工作,標記爲「答案」,這就是我們如何在SO上做的事情。人們提出問題,一旦他們得到答案,他們通過標記爲「答案」 –