actuallife
actuallife

Reputation: 89

How to conditionally format a row in Excel VBA based on if first cell is empty?

What I'm trying to do is very simple, I just don't have much VBA / Excel experience to know how to do it. I am working on a macro to do a few different things, I'm just stuck on this part.

What I want to do is go through all of my rows, and if the first cell in the row is empty, I want to select the entire row and UnMerge it. I know that if the first cell is empty, that row contains merged cells that I want unmerged.

I know that these two lines below will select row 2 and Unmerge it, but I need a way to loop through my data and find and unmerge the rows automatically, because the row numbers wont be the same every time.

Rows("2:2").Select

Selection.UnMerge

I want to do something like this:

For (each row) {
   if (the first cell is empty) {
       UnMerge all cells in that row;
   }
}

I just don't know how to do that in VBA syntax.

Any help is appreciated!

Upvotes: 0

Views: 101

Answers (1)

Variatus
Variatus

Reputation: 14373

This should do the job.

Sub UnmergeRows()

    Dim R       As Long                 ' loop counter: rows
    
    With Worksheets("Sheet1")           ' change tab name as required
        ' "B" should be the longest column in the worksheet
        For R = 2 To .Cells(.Rows.Count, "B").End(xlUp).Row
            If IsEmpty(.Cells(R, 1)) Then .Rows(R).MergeCells = False
        Next R
    End With
End Sub

Upvotes: 1

Related Questions