2016-07-12 3 views
0

enter image description here列に「Null」(文字列値)が空でないか空白でないかどうかをチェックするvbaを作成しようとしています。 Null "文字列値は列全体を削除します。 forループを使用してセル内のすべての値をループすることはできますが、時間がかかります。私は、オートフィルタまたは検索コマンドを使用するオプションを探しています。 何か提案がありがとうございます。 ありがとうございます。ワークシートでvba delete列全体が "Null"の場合

答えて

0

Dim col as Range 
For Each col In UsedRange.Columns 
    If col.Find("*") Is Nothing Then 
     col.EntireColumn.Delete 
    End If 
Next 

ような何かを更新

私は、Excelで "ヌル" セル値を持つことができるとは思いません。私はこの

Dim cell As Range 
Set cell = Range("A1") 
cell.Value = ""  ' cell.Value2 shows as "Empty" in the Locals window 
cell.Value = Empty ' cell.Value2 still "Empty" 
cell.Value = Nothing ' this gives "Run-time error '1004': Application-defined or object-defined Error" 

アップデート2

もう遅く方法は

Dim col as Range, blanks as Range 
For Each col In UsedRange.Columns 
    Set blanks = col.SpecialCells(xlCellTypeBlanks) ' gives error if no blank cells in the col range 
    If blanks.Cells.Count = col.Cells.Count Then 
     col.EntireColumn.Delete 
    End If 
Next 

アップデート3

あなたはWorksheetFunction.CountIf(col, "Null")を使用することができ、カラム内のすべての空白のセルを見つけることができてみました"ヌル"セルの数を数える

Dim col As Range 
For Each col In UsedRange.Columns 
    'NullCount = WorksheetFunction.CountIf(col, "Null") 
    'NotNullCount = WorksheetFunction.CountIf(col, "<>Null") 
    'blanksCount = WorksheetFunction.CountBlank(col) 
    'nonBlanksCount = WorksheetFunction.CountA(col) 
    With WorksheetFunction 
     If .CountA(col) = .CountIf(col, "Null") + 1 Then ' 1 for the column header cell 
      col.EntireColumn.Delete 
     End If 
    End With 
Next 
+0

スレイ、お返事ありがとうございます。列内のすべてのセルに「Null」Stringが含まれている場合は、上記のコードが見つかるかどうかはわかりません.Null値をチェックしていないことに注意してください。その "ヌル"文字列。申し訳ありませんが私は私の質問でこれを逃した。 –

+0

とにかく、<>(検索文字列)の検索、検索、一致コマンドを使用できることはありますか?だから私は列の "<> Null"のようなものを検索し、見つからなければ削除することができます。 –

+0

ホーム>検索と選択>定数または数式 – Slai

2

、式:その列のすべてのセルがNULLまたは完全に空のどちらかである場合

=COUNTBLANK(A:A) 

は1048576を返します。

Sub KolumnKiller() 
    With Application.WorksheetFunction 
     For i = Columns.Count To 1 Step -1 
      If .CountBlank(Columns(i)) <> Rows.Count Then Exit For 
     Next i 

     For j = i To 1 Step -1 
      If .CountBlank(Columns(j)) = Rows.Count Then 
       Columns(j).Delete 
      End If 
     Next j 
    End With 
End Sub 

最初のループは、2番目のループの開始場所として適しています。

関連する問題