VBA wont work due to merged cells

impressive

New Member
Joined
Oct 3, 2022
Messages
12
Office Version
  1. 2019
Platform
  1. Windows
  2. MacOS
Hello everyone,

I am working on a list where the Area from A1 to Q7 is basically the header (Client Name, Location, Project Start, Project End, etc...), which consists mostly of merged cells.

In this list are items listed that a client gets.

So the listing of the Items starts from A8, and this is also where I want my Macro to work,

I have a macro that should delete empty rows if a Cell in the A-Column is empty.

This is what i have so far:

Sub DeleteRow()

Range("a8:a257").Select
Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub


When I execute it, I get an error message and in the debug is written:
Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete

After i removed in my sheet the header area with the merged cells (A1 to Q7) , it worked perfectly fine.

Could someone please help me?

Kind regards
 
Hi,

your code

Rich (BB code):
Sub DeleteRow_SOW()

With Range("a8:a257").Select
Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub

Code posted by me:

VBA Code:
With Range("A8:A257")
  If WorksheetFunction.CountBlank(.Cells) > 0 Then
    .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  End If
End With

I explained before that if no empty cells are within the range the codeline to delete will raise an error. One way to learn VBA is to avoid Select, Selection, Activate as there will be another way to do so (in more that 99% of all cases). These commands are used by the Macro Recorder due to the way the objects are handled there (if recorded).

Holger
 
Upvote 0
Solution

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi,

Merged Cells are a True Nightmare ...and should almost be Banned ...

However, should you be facing the situation of having to find the First row and the Last Row of a Merged Cell

VBA Code:
Sub TestCell()
' To differentiate Merged from Standard Cells
Dim MyMergedCell As Range
Dim FirstRow As Long, LastRow As Long

Set MyMergedCell = ActiveCell.MergeArea
FirstRow = MyMergedCell.Row
LastRow = MyMergedCell.Row + MyMergedCell.Rows.Count - 1

MsgBox "For " & IIf(FirstRow = LastRow, "this Cell ", "this Merged Cell, the Range Is ") & MyMergedCell.Address(0, 0) & _
vbNewLine & vbNewLine & "First Cell Row Number is " & FirstRow & vbNewLine & vbNewLine & "Last Cell Row Number is " & LastRow

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top