2016-04-23 55 views
0

我試圖找到名稱以_結尾並且單元格的值爲空的每個命名單元格。這是我使用的代碼無濟於事。 (msgbox只是爲了幫助我看看它找到了多少)。如果你知道一個更好的方法(不必每次都做一個),請讓我知道。謝謝!查找名稱中包含特定字符的所有命名單元格和空白值

namedrange = 0 

For Each cell In ActiveWorkbook.Names 
If Right(cell.Name, 1) = "_" And cell.Value = "" Then 
cell.Interior.ColorIndex = 3 
namedrange = namedrange + 1 
End If 
Next cell 

MsgBox namedrange 

回答

0

解決方案:

requiredmissing = 0 

Dim RangeName As Name 
Dim namedrange As Long 

For Each RangeName In ActiveWorkbook.Names 
    If Right(RangeName.Name, 1) <> "_" Then GoTo skipthisone 

     If Range(RangeName.Name).Value = "" Then 
     Range(RangeName.Name).Interior.ColorIndex = 6 
     requiredmissing = requiredmissing + 1 
     Else 
     Range(RangeName.Name).Interior.ColorIndex = 0 
     End If 

    all = all + 1 
skipthisone: 
Next RangeName 
1

試試這個:

Sub test() 
    Dim RangeName As Name 
    Dim namedrange As Long 
    namedrange = 0 

    For Each RangeName In ActiveWorkbook.Names 
     If Right(RangeName.Name, 1) = "_" And Range(RangeName).Value = "" Then 
     Range(RangeName).Interior.ColorIndex = 3 
     namedrange = namedrange + 1 
     End If 
     all = all + 1 
    Next RangeName 

    MsgBox namedrange 
End Sub 
+0

謝謝,@Mrig! '和範圍'...需要一個調整,我結束了下面。我非常感謝你的幫助! –

+0

@JoePatrick - 技術上'Range(RangeName).Value'和'Range(RangeName.Name).Value'都會給你相同的結果,因爲如果'RangeName'是單元格地址,那麼'RangeName.Name'只不過是名稱給予相同的單元地址。 – Mrig

+0

嗨,@Mrig,我不確定技術性,但沒有調整就得到了運行時錯誤13錯誤。它返回的是單元格的值而不是單元格的名稱。 –

相關問題