2014-01-13 109 views
0

我有幾個列範圍在Excel J10:J111,N10:N111,R10:R111等了,我需要一個下拉列表替換空白單元格,並保留其他單元格區域因爲它們是(即它們都是「不適用」)。COUNTIF代碼VBA

我設法讓它通過使用下面的代碼工作:

Range("N10:N111").Select 
    Selection.SpecialCells(xlCellTypeBlanks).Select 
    With Selection.Validation 
    .Delete 
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ 
    xlBetween, Formula1:="0,1,2,3,4,5" 
    .IgnoreBlank = True 
    .InCellDropdown = True 
    .InputTitle = "" 
    .ErrorTitle = "" 
    .InputMessage = "" 
    .ErrorMessage = "" 
    .ShowInput = True 
    .ShowError = True 
    End With 

不幸的是,如果我有在範圍內沒有空白單元格(因爲它在某些情況下發生)宏關閉。

有沒有告訴代碼「如果只有‘N/A’跳過宏這一節,並繼續到下一個的方式

+3

測試第一:'如果不Selection.SpecialCells(xlCellTypeBlanks)是沒有那麼...' –

回答

1

嘗試使用下面的代碼的修改:

Sub Test() 
    Dim rng As Range 

    On Error Resume Next 
    Set rng = Nothing 
    Set rng = Range("N10:N100").SpecialCells(xlCellTypeBlanks) 
    If Not rng Is Nothing Then 
     With rng.Validation 
      .Delete 
      .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ 
      xlBetween, Formula1:="0,1,2,3,4,5" 
      .IgnoreBlank = True 
      .InCellDropdown = True 
      .InputTitle = "" 
      .ErrorTitle = "" 
      .InputMessage = "" 
      .ErrorMessage = "" 
      .ShowInput = True 
      .ShowError = True 
     End With 
    End If 
    On Error GoTo 0 
End Sub 
+1

這是驚人的 – user3191114

+0

工作就像一個魅力太謝謝你了!! – user3191114