Advertisement
gocha

Find non-empty cell function - Excel VBA

Oct 16th, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Private Sub Workbook_AddinInstall()
  2. '
  3. '  Application.MacroOptions _
  4. '    Macro:="NONBLANK", _
  5. '    Description:="空白でないセルを検索します。", _
  6. '    Category:=5, _
  7. '    ArgumentDescriptions:=("には、連続したセル範囲を指定します。|には TRUE または FALSE のいずれかを指定し、値を検索する方向を指定します。|には、取得対象に含めない値を指定します。", "|")
  8. '
  9. 'End Sub
  10.  
  11. Option Explicit
  12.  
  13. Function NONBLANK(検査範囲 As Range, Optional 先頭から検索 As Boolean = False, Optional 除外する値 As String = "") As Range
  14.     Dim cellIndex As Long
  15.     Dim candidateCell As Range
  16.  
  17.     If 先頭から検索 Then
  18.         ' 先頭(左上)から検索
  19.        Set NONBLANK = 検査範囲(1)
  20.         For cellIndex = 1 To 検査範囲.Count
  21.             Set candidateCell = 検査範囲(cellIndex)
  22.             If Not IsEmpty(candidateCell) Then
  23.                 If Not candidateCell Like 除外する値 Then
  24.                     Set NONBLANK = candidateCell
  25.                     Exit For
  26.                 End If
  27.             End If
  28.         Next cellIndex
  29.     Else
  30.         ' 末尾(右下)から検索
  31.        Set NONBLANK = 検査範囲(検査範囲.Count)
  32.         For cellIndex = 検査範囲.Count To 1 Step -1
  33.             Set candidateCell = 検査範囲(cellIndex)
  34.             If Not IsEmpty(candidateCell) Then
  35.                 If Not candidateCell Like 除外する値 Then
  36.                     Set NONBLANK = candidateCell
  37.                     Exit For
  38.                 End If
  39.             End If
  40.         Next cellIndex
  41.     End If
  42. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement