[Excel VBA] Efficient Processing by Skipping Blank Cells! How to Implement “Do Nothing if Blank” Using If Statements

When processing multiple cells at once in Excel VBA, one common challenge is “how to handle blank cells.”

For example, have you ever encountered situations like these?

  • You want to sum only cells where numbers are entered.
  • You want to transfer only filled rows to another sheet.
  • You want to do nothing for blank cells and proceed to the next process.

In such cases, conditional branching using VBA’s If statement is effective. This article introduces the basic concept of “skipping processing if a cell is blank” and provides two practical code examples.

目次

Two Main Ways to Determine if a Cell is Blank

There are two main ways to judge whether a cell is blank:

1. Using the IsEmpty Function

The IsEmpty function determines if a cell is in a state where “absolutely nothing is entered.” If a formula is entered and the result is a blank string, it returns False.

2. Comparing with “” (Empty String)

This method considers a cell blank if its value is "" (empty string). This treats cells where a formula result is an empty string as “blank” as well.

Code Example 1: Summing Excluding Blank Cells (Using IsEmpty)

Option Explicit

Sub SumNonBlankCells()
    
    Dim rngTarget As Range
    Dim c As Range
    Dim total As Double
    
    Set rngTarget = Sheet1.Range("A1:A100")
    
    For Each c In rngTarget
        If Not IsEmpty(c.Value) Then
            total = total + Val(c.Value)
        End If
    Next c
    
    MsgBox "The sum of entered cells is " & total & ".", vbInformation
    
End Sub

Explanation

This code aggregates only “non-empty cells” within the range A1 to A100 of the sheet. The IsEmpty function excludes only cells that are completely unentered.

Code Example 2: Copying Only Filled Rows to Another Sheet (Using Empty String)

Option Explicit

Sub CopyFilledRows()
    
    Dim wsSrc As Worksheet
    Dim wsDst As Worksheet
    Dim lastRow As Long
    Dim dstRow As Long
    Dim i As Long
    
    Set wsSrc = Worksheets("Sheet1")
    Set wsDst = Worksheets("Sheet2")
    
    lastRow = wsSrc.Cells(wsSrc.Rows.Count, "A").End(xlUp).Row
    dstRow = 1
    
    For i = 1 To lastRow
        If wsSrc.Cells(i, "A").Value <> "" Then
            wsSrc.Rows(i).Copy Destination:=wsDst.Rows(dstRow)
            dstRow = dstRow + 1
        End If
    Next i
    
    MsgBox "Transfer complete.", vbInformation
    
End Sub

Explanation

This macro transfers only rows with data in column A to another sheet (Sheet2). By comparing with "", cells where a formula returns an empty string are also treated as “blank” and skipped.

Distinguishing Between the Methods

MethodSuitable Case
IsEmptyWhen you want to identify “only unentered cells,” even if formulas exist.
“” ComparisonWhen you want to treat cells as “blank” even if a formula result is empty.

The key is to choose the method flexibly according to the sheet structure and your purpose.

Summary

  • The requirement to “skip blank cells and process” is very common in Excel VBA.
  • Incorporating conditional branching with If statements significantly improves processing efficiency and error avoidance.

By mastering the basic writing styles introduced here, you will greatly expand the range of macro creation in your practical work.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次