Excel vba удалить строку если ячейка пустая

In this Article

  • Delete Entire Row or Column
    • Delete Multiple Rows or Columns
  • Delete Blank / Empty Rows
    • Delete Row if Cell is Blank
  • Delete Row Based on Cell Value
  • More Delete Row and Column Examples
    • Delete Duplicate Rows
    • Delete Table Rows
    • Delete Filtered Rows
    • Delete Rows in Range
    • Delete Selected Rows
    • Delete Last Row
    • Delete Columns by Number

This tutorial will demonstrate different ways to delete rows and columns in Excel using VBA.

Delete Entire Row or Column

To delete an entire row in VBA use this line of code:

Rows(1).Delete

Notice we use the Delete method to delete a row.

Instead of referencing the Rows Object, you can reference rows based on their Range Object with EntireRow:

Range("a1").EntireRow.Delete

Similarly to delete an entire column, use these lines of code:

Columns(1).Delete
Range("a1").EntireColumn.Delete

Delete Multiple Rows or Columns

Using the same logic, you can also delete multiple rows at once:

Rows("1:3").Delete

or columns:

Columns("A:C").Delete

Notice here we reference the specific row and column numbers / letters surrounded by quotations.

Of course, you can also reference the EntireRow of a range:

Range("a1:a10").EntireRow.Delete

Note: The examples below only demonstrate deleting rows, however as you can see above, the syntax is virtually identically to delete columns.

Delete Blank / Empty Rows

This example will delete a row if the entire row is blank:

Sub DeleteRows_EntireRowBlank()

Dim cell As Range

For Each cell In Range("b2:b20")
    If Application.WorksheetFunction.CountA(cell.EntireRow) = 0 Then
        cell.EntireRow.Delete
    End If
Next cell

End Sub

It makes use of the Excel worksheet function: COUNTA.

Delete Row if Cell is Blank

This will delete a row if specific column in that row is blank (in this case column B):

Range("b3:b20").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

Delete Row Based on Cell Value

This will loop through a range, and delete rows if a certain cell value in that row says “delete”.

Sub DeleteRowswithSpecificValue()

Dim cell As Range

For Each cell In Range("b2:b20")
    If cell.Value = "delete" Then
        cell.EntireRow.Delete
    End If
Next cell

End Sub

More Delete Row and Column Examples

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro — A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

automacro

Learn More

Delete Duplicate Rows

This code will delete all duplicate rows in a range:

Range("b2:c100").RemoveDuplicates Columns:=2

Notice we set Columns:=2. This tells VBA to check both the first two columns of data when considering if rows are duplicates. A duplicate is only found when both columns have duplicate values.

If we had set this to 1, only the first row would’ve been checked for duplicate values.

Delete Table Rows

This code will delete the second row in a Table by referencing ListObjects.

ThisWorkbook.Sheets("Sheet1").ListObjects("list1").ListRows(2).Delete

Delete Filtered Rows

To delete only rows that are visible after filtering:

Range("b3:b20").SpecialCells(xlCellTypeVisible).EntireRow.Delete

VBA Programming | Code Generator does work for you!

Delete Rows in Range

This code will delete all rows in range:

Range("a1:a10").EntireRow.Delete

Delete Selected Rows

This code will delete all selected rows:

Selection.EntireRow.Delete

Delete Last Row

This will delete the last used row in column B:

Cells(Rows.Count, 2).End(xlUp).EntireRow.Delete

By changing 2 to 1, you can delete the last used row in column A, etc.:

Cells(Rows.Count, 1).End(xlUp).EntireRow.Delete

Delete Columns by Number

To delete a column by it’s number, use a code like this:

Columns (2).Delete

Удаление пустых строк с помощью кода VBA из всего задействованного диапазона рабочего листа Excel и из отдельного заданного диапазона.

Главный секрет удаления пустых строк кодом VBA Excel – это построчный просмотр диапазона или отдельного столбца снизу вверх, что исключает возможность при удалении найденных пустых строк получить бесконечный цикл и зависание программы.

Удаление пустых строк в используемом диапазоне

Рассмотрим удаление пустых строк из всего используемого диапазона на рабочем листе. Это может быть как таблица, так и любые наборы данных и произвольные записи, внутри которых присутствуют пустые строки, от которых надо избавиться.

Определить границы используемого диапазона на рабочем листе из кода VBA Excel нам поможет последняя ячейка используемого диапазона: Cells.SpecialCells(xlLastCell).

Самый простой код удаления пустых строк

Сначала определяем номер строки последней ячейки задействованного на рабочем листе диапазона. Затем, с этой строки начинаем построчный просмотр используемого диапазона снизу вверх с поиском и удалением пустых строк.

Пример кода VBA Excel для активного листа:

Sub Primer1()

Dim n As Long, i As Long

‘Определяем номер строки последней ячейки

‘используемого диапазона на рабочем листе

n = Cells.SpecialCells(xlLastCell).Row

    ‘Ищем и удаляем пустые строки

    For i = n To 1 Step 1

        If Rows(i).Text = «» Then Rows(i).Delete

    Next

End Sub

То же самое, но с указанием книги и рабочего листа:

Sub Primer2()

Dim n As Long, i As Long

    With ThisWorkbook.Worksheets(«Лист1»)

        n = .Cells.SpecialCells(xlLastCell).Row

            For i = n To 1 Step 1

                If .Rows(i).Text = «» Then .Rows(i).Delete

            Next

    End With

End Sub

Программа определения времени выполнения макроса показала, что этот код отработал в диапазоне из 3000 строк за 17,5 секунд.

Улучшенный код удаления пустых строк

Предыдущий код VBA Excel анализирует на наличие текста каждую строку по всей длине в пределах рабочего листа. Эта процедура проверяет каждую строку по длине только в переделах используемого диапазона:

Sub Primer3()

Dim n As Long, i As Long, myRange As Range

‘Присваиваем объектной переменной ссылку на диапазон от первой ячейки

‘рабочего листа до последней ячейки используемого диапазона

Set myRange = Range(Range(«A1»), Cells.SpecialCells(xlLastCell))

    With myRange

        n = .Rows.Count

        For i = n To 1 Step 1

            If .Rows(i).Text = «» Then .Rows(i).Delete

        Next

    End With

End Sub

Программа определения времени выполнения макроса показала, что этот код отработал в диапазоне из 3000 строк за 13,3 секунды.

Удаление строк по пустым ячейкам

Иногда может появиться необходимость удалить не только полностью пустые строки, но и строки с пустыми ячейками в определенном столбце. Тогда следует действовать так:

Sub Primer4()

Dim n As Long, i As Long

n = Cells.SpecialCells(xlLastCell).Row

    For i = n To 1 Step 1

        If Cells(i, 1).Text = «» Then Rows(i).Delete

    Next

End Sub

или так:

Sub Primer5()

Dim n As Long, i As Long, myRange As Range

Set myRange = Range(Range(«A1»), Cells.SpecialCells(xlLastCell))

    With myRange

        n = .Rows.Count

        For i = n To 1 Step 1

            If .Cells(i, 1).Text = «» Then .Rows(i).Delete

        Next

    End With

End Sub

В этих примерах поиск пустой ячейки производится в первом столбце: Cells(i, 1).

Удаление пустых строк в заданном диапазоне

Процедуры VBA Excel для удаления пустых строк из заданного диапазона рассмотрим на примере объекта Selection, который можно заменить на любой диапазон, указанный явно.

Удаление полностью пустых строк в пределах заданного диапазона:

Sub Primer6()

Dim n As Long, i As Long

    With Selection

        n = .Rows.Count

        For i = n To 1 Step 1

            If .Rows(i).Text = «» Then .Rows(i).Delete

        Next

    End With

End Sub

Удаление строк по пустым ячейкам в одном из столбцов:

Sub Primer7()

Dim n As Long, i As Long

    With Selection

        n = .Rows.Count

        For i = n To 1 Step 1

            If .Cells(i, 1).Text = «» Then .Rows(i).Delete

        Next

    End With

End Sub


Can anyone walk me through how to write a script to delete the entire row if a cell in column D = «» on sheet 3 in range D13:D40.

Also, how to prevent the user from accidentally running the script again once those cells in the range are already deleted and other cells are now on the D13:D40 range?

Alex P's user avatar

Alex P

12.2k5 gold badges51 silver badges69 bronze badges

asked Jul 3, 2015 at 11:46

Diego's user avatar

4

Solution: This is working for me:

Sub DeleteRowsWithEmptyColumnDCell()
    Dim rng As Range
    Dim i As Long
    Set rng = ThisWorkbook.ActiveSheet.Range("D13:D40")
    With rng
        ' Loop through all cells of the range
        ' Loop backwards, hence the "Step -1"
        For i = .Rows.Count To 1 Step -1
            If .Item(i) = "" Then
                ' Since cell is empty, delete the whole row
                .Item(i).EntireRow.Delete
            End If
        Next i
    End With
End Sub

Explanation: Run a for loop through all cells in your Range in column D and delete the entire row if the cell value is empty. Important: When looping through rows and deleting some of them based on their content, you need to loop backwards, not forward. If you go forward and you delete a row, all subsequent rows get a different row number (-1). And if you have two empty cells next to each other, only the row of the first one will be deleted because the second one is moved one row up but the loop will continue at the next line.

answered Jul 3, 2015 at 12:05

nicolaus-hee's user avatar

nicolaus-heenicolaus-hee

7871 gold badge9 silver badges25 bronze badges

0

No need for loops:

Sub SO()

Static alreadyRan As Integer

restart:

If Not CBool(alreadyRan) Then
    With Sheets("Sheet3")
        With .Range("D13:D40")
            .AutoFilter 1, "="
            With .SpecialCells(xlCellTypeVisible)
                If .Areas.Count > 1 Then
                    .EntireRow.Delete
                    alreadyRan = alreadyRan + 1
                End If
            End With
        End With
        .AutoFilterMode = False
    End With
Else
    If MsgBox("procedure has already been run, do you wish to continue anyway?", vbYesNo) = vbYes Then
        alreadyRan = 0
        GoTo restart:
    End If
End If

End Sub

Use AutoFilter to find blank cells, and then use SpecialCells to remove the results. Uses a Static variable to keep track of when the procedure has been run.

answered Jul 3, 2015 at 13:04

SierraOscar's user avatar

SierraOscarSierraOscar

17.4k6 gold badges41 silver badges68 bronze badges

Here’s my take on it. See the comments in the code for what happens along the way.

Sub deleterow()
  ' First declare the variables you are going to use in the sub
  Dim i As Long, safety_net As Long
  ' Loop through the row-numbers you want to change.
  For i = 13 To 40 Step 1
    ' While the value in the cell we are currently examining = "", we delete the row we are on
    ' To avoid an infinite loop, we add a "safety-net", to ensure that we never loop more than 100 times
    While Worksheets("Sheet3").Range("D" & CStr(i)).Value = "" And safety_net < 100
      ' Delete the row of the current cell we are examining
      Worksheets("Sheet3").Range("D" & CStr(i)).EntireRow.Delete
      ' Increase the loop-counter
      safety_net = safety_net + 1
    Wend
    ' Reset the loop-counter
    safety_net = 0
  ' Move back to the top of the loop, incrementing i by the value specified in step. Default value is 1.
  Next i
End Sub

To prevent a user from running the code by accident, I’d probably just add Option Private Module at the top of the module, and password-protect the VBA-project, but then again it’s not that easy to run it by accident in the first place.

answered Jul 3, 2015 at 12:10

eirikdaude's user avatar

eirikdaudeeirikdaude

3,0916 gold badges25 silver badges49 bronze badges

This code executes via a button on the sheet that, once run, removes the button from the worksheet so it cannot be run again.

Sub DeleteBlanks()
    Dim rw As Integer, buttonID As String

    buttonID = Application.Caller

    For rw = 40 To 13 Step -1

        If Range("D" & rw) = "" Then
            Range("D" & rw).EntireRow.Delete
        End If

    Next rw

    ActiveSheet.Buttons(buttonID).Delete
End Sub

You’ll need to add a button to your spreadsheet and assign the macro to it.

answered Jul 4, 2015 at 9:11

Alex P's user avatar

Alex PAlex P

12.2k5 gold badges51 silver badges69 bronze badges

There is no need for loops or filters to find the blank cells in the specified Range. The Range.SpecialCells property can be used to find any blank cells in the Range coupled with the Range.EntireRow property to delete these. To preserve the run state, the code adds a Comment to the first cell in the range. This will preserve the run state even if the Workbook is closed (assuming that it has been saved).

Sub DeleteEmpty()
   Dim ws As Excel.Worksheet
   Set ws = ActiveSheet       ' change this as is appropriate
   Dim sourceRange As Excel.Range
   Set sourceRange = ws.Range("d13:d40")
   Dim cmnt As Excel.Comment
   Set cmnt = sourceRange.Cells(1, 1).Comment

   If Not cmnt Is Nothing Then
      If cmnt.Text = "Deleted" Then
         If MsgBox("Do you wish to continue with delete?", vbYesNo, "Already deleted!") = vbNo Then
            Exit Sub
         End If
      End If
   End If

   Dim deletedThese As Excel.Range
   On Error Resume Next
   ' the next line will throw an error if no blanks cells found
   ' hence the 'Resume Next'
   Set deletedThese = sourceRange.SpecialCells(xlCellTypeBlanks)
   On Error GoTo 0
   If Not deletedThese Is Nothing Then
      deletedThese.EntireRow.Delete
   End If

   ' for preserving run state
   If cmnt Is Nothing Then Set cmnt = sourceRange.Cells(1, 1).AddComment
   cmnt.Text "Deleted"
   cmnt.Visible = False
End Sub

answered Jul 5, 2015 at 3:50

TnTinMn's user avatar

TnTinMnTnTinMn

11.4k3 gold badges17 silver badges39 bronze badges

1

I’ve recently had to write something similar to this. I’m not sure that the code below is terribly professional, as it involves storing a value in cell J1 (obviously this can be changed), but it will do the job you require. I hope this helps:

Sub ColD()

Dim irow As long
Dim strCol As String

Sheets("sheet2").Activate
If Cells(1, 10) = "" Then
    lrun = " Yesterday."
Else: lrun = Cells(1, 10)
End If

MsgBox "This script was last run: " & lrun & "  Are you sure you wish to     continue?", vbYesNo
If vbYes Then
    For irow = 40 To 13 step -1
        strCol = Cells(irow, 4).Value
        If strCol = "" Then
            Cells(irow, 4).EntireRow.Delete
        End If
    Next
    lrun = Now()
    Cells(1, 10) = lrun
Else: Exit Sub
End If
End Sub

answered Jul 3, 2015 at 12:15

Tom37's user avatar

Tom37Tom37

717 bronze badges

7

I would like to delete the empty rows my ERP Quotation generates. I’m trying to go through the document (A1:Z50) and for each row where there is no data in the cells (A1-B1...Z1 = empty, A5-B5...Z5 = empty) I want to delete them.

I found this, but can’t seem to configure it for me.

On Error Resume Next
Worksheet.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0

Community's user avatar

asked Feb 21, 2012 at 14:55

CustomX's user avatar

4

How about

sub foo()
  dim r As Range, rows As Long, i As Long
  Set r = ActiveSheet.Range("A1:Z50")
  rows = r.rows.Count
  For i = rows To 1 Step (-1)
    If WorksheetFunction.CountA(r.rows(i)) = 0 Then r.rows(i).Delete
  Next
End Sub

answered Feb 21, 2012 at 15:15

Alex K.'s user avatar

Alex K.Alex K.

170k30 gold badges263 silver badges286 bronze badges

1

Try this

Option Explicit

Sub Sample()
    Dim i As Long
    Dim DelRange As Range

    On Error GoTo Whoa

    Application.ScreenUpdating = False

    For i = 1 To 50
        If Application.WorksheetFunction.CountA(Range("A" & i & ":" & "Z" & i)) = 0 Then
            If DelRange Is Nothing Then
                Set DelRange = Range("A" & i & ":" & "Z" & i)
            Else
                Set DelRange = Union(DelRange, Range("A" & i & ":" & "Z" & i))
            End If
        End If
    Next i

    If Not DelRange Is Nothing Then DelRange.Delete shift:=xlUp
LetsContinue:
    Application.ScreenUpdating = True

    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub

IF you want to delete the entire row then use this code

Option Explicit

Sub Sample()
    Dim i As Long
    Dim DelRange As Range

    On Error GoTo Whoa

    Application.ScreenUpdating = False

    For i = 1 To 50
        If Application.WorksheetFunction.CountA(Range("A" & i & ":" & "Z" & i)) = 0 Then
            If DelRange Is Nothing Then
                Set DelRange = Rows(i)
            Else
                Set DelRange = Union(DelRange, Rows(i))
            End If
        End If
    Next i

    If Not DelRange Is Nothing Then DelRange.Delete shift:=xlUp
LetsContinue:
    Application.ScreenUpdating = True

    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub

answered Feb 21, 2012 at 15:13

Siddharth Rout's user avatar

Siddharth RoutSiddharth Rout

146k17 gold badges206 silver badges250 bronze badges

3

I know I am late to the party, but here is some code I wrote/use to do the job.

Sub DeleteERows()
    Sheets("Sheet1").Select
    Range("a2:A15000").Select
    Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub

Kingsley's user avatar

Kingsley

14.3k5 gold badges33 silver badges52 bronze badges

answered Dec 12, 2018 at 21:45

Smiley Lando's user avatar

3

for those who are intersted to remove «empty» and «blank» rows ( Ctrl + Shift + End going deep down of your worksheet ) .. here is my code.
It will find the last «real»row in each sheet and delete the remaining blank rows.

Function XLBlank()
    For Each sh In ActiveWorkbook.Worksheets
        sh.Activate
        Cells(1, 1).Select
        lRow = Cells.Find(What:="*", _
            After:=Range("A1"), _
            LookAt:=xlPart, _
            LookIn:=xlFormulas, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlPrevious, _
            MatchCase:=False).Row
        
        Range("A" & lRow + 1, Range("A1").SpecialCells(xlCellTypeLastCell).Address).Select
        On Error Resume Next
        Selection.EntireRow.SpecialCells(xlBlanks).EntireRow.Delete
        Cells(1, 1).Select
    Next
    ActiveWorkbook.Save
    ActiveWorkbook.Worksheets(1).Activate
End Function

Open VBA ( ALT + F11 ), Insert -> Module,
Copy past my code and launch it with F5.
Et voila :D

Jonathan Schiffner's user avatar

answered Mar 5, 2019 at 8:47

jjsteing's user avatar

I have another one for the case when you want to delete only rows which are complete empty, but not single empty cells. It also works outside of Excel e.g. on accessing Excel by Access-VBA or VB6.

Public Sub DeleteEmptyRows(Sheet As Excel.Worksheet)
    Dim Row As Range
    Dim Index As Long
    Dim Count As Long

    If Sheet Is Nothing Then Exit Sub

    ' We are iterating across a collection where we delete elements on the way.
    ' So its safe to iterate from the end to the beginning to avoid index confusion.
    For Index = Sheet.UsedRange.Rows.Count To 1 Step -1
        Set Row = Sheet.UsedRange.Rows(Index)

        ' This construct is necessary because SpecialCells(xlCellTypeBlanks)
        ' always throws runtime errors if it doesn't find any empty cell.
        Count = 0
        On Error Resume Next
        Count = Row.SpecialCells(xlCellTypeBlanks).Count
        On Error GoTo 0

        If Count = Row.Cells.Count Then Row.Delete xlUp
    Next
End Sub

answered Aug 27, 2019 at 11:34

Aranxo's user avatar

AranxoAranxo

6073 silver badges14 bronze badges

2

To make Alex K’s answer slightly more dynamic you could use the code below:

Sub DeleteBlankRows()

Dim wks As Worksheet
Dim lngLastRow As Long, lngLastCol As Long, lngIdx As Long, _
    lngColCounter As Long
Dim blnAllBlank As Boolean
Dim UserInputSheet As String

UserInputSheet = Application.InputBox("Enter the name of the sheet which you wish to remove empty rows from")

Set wks = Worksheets(UserInputSheet)

With wks
    'Now that our sheet is defined, we'll find the last row and last column
    lngLastRow = .Cells.Find(What:="*", LookIn:=xlFormulas, _
                             SearchOrder:=xlByRows, _
                             SearchDirection:=xlPrevious).Row
    lngLastCol = .Cells.Find(What:="*", LookIn:=xlFormulas, _
                             SearchOrder:=xlByColumns, _
                             SearchDirection:=xlPrevious).Column

    'Since we need to delete rows, we start from the bottom and move up
    For lngIdx = lngLastRow To 1 Step -1

        'Start by setting a flag to immediately stop checking
        'if a cell is NOT blank and initializing the column counter
        blnAllBlank = True
        lngColCounter = 2

        'Check cells from left to right while the flag is True
        'and the we are within the farthest-right column
        While blnAllBlank And lngColCounter <= lngLastCol

            'If the cell is NOT blank, trip the flag and exit the loop
            If .Cells(lngIdx, lngColCounter) <> "" Then
                blnAllBlank = False
            Else
                lngColCounter = lngColCounter + 1
            End If

        Wend

        'Delete the row if the blnBlank variable is True
        If blnAllBlank Then
            .rows(lngIdx).delete
        End If

    Next lngIdx
End With


MsgBox "Blank rows have been deleted."

 End Sub

This was sourced from this website and then slightly adapted to allow the user to choose which worksheet they want to empty rows removed from.

answered Dec 18, 2017 at 21:50

RugsKid's user avatar

RugsKidRugsKid

3247 silver badges25 bronze badges

In order to have the On Error Resume function work you must declare the workbook and worksheet values as such

On Error Resume Next  
ActiveWorkbook.Worksheets("Sheet Name").Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete  
On Error GoTo 0

I had the same issue and this eliminated all the empty rows without the need to implement a For loop.

answered Apr 30, 2018 at 14:39

Jerome's user avatar

This worked great for me (you can adjust lastrow and lastcol as needed):

Sub delete_rows_blank2()

t = 1
lastrow = ActiveSheet.UsedRange.Rows.Count
lastcol = ActiveSheet.UsedRange.Columns.Count

Do Until t = lastrow

For j = 1 To lastcol
    'This only checks the first column because the "Else" statement below will skip to the next row if the first column has content.
    If Cells(t, j) = "" Then

        j = j + 1

            If j = lastcol Then
            Rows(t).Delete
            t = t + 1
            End If

    Else
    'Note that doing this row skip, may prevent user from checking other columns for blanks.
        t = t + 1

    End If

Next

Loop

End Sub

Mike's user avatar

answered Feb 27, 2018 at 15:10

Here is the quickest way to Delete all blank Rows ( based on one Columns )

Dim lstRow as integet, ws as worksheet

Set ws = ThisWorkbook.Sheets("NameOfSheet")

With ws

     lstRow = .Cells(Rows.Count, "B").End(xlUp).Row ' Or Rows.Count "B", "C" or "A" depends 

     .Range("A1:E" & lstRow).SpecialCells(xlCellTypeBlanks).EntireRow.Delete

End with

answered Mar 10, 2022 at 15:46

Om_VBA's user avatar

I think I need a loop function for this but i’m not sure how to go about it.

The task for this is to see if a specific column is blank, then to delete that row. But sometimes there is no blank cells and i am getting a end debug error.

Here is my code:

Sub DeleteRow()
Dim lr As Long
Dim shCurrentWeek As Worksheet
Set shCurrentWeek = AciveWorkbook.Sheets("Current Week")
lr = shCurrentWeek.Range("A" & Rows.Count).End(xlUp).Row 

'Delete Row
shCurrentWeek.Range("B4:B" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete

End Sub

any ideas?

asked Dec 6, 2012 at 17:57

kmiao91's user avatar

How about just putting on error resume next right before your Delete line? LIke this:

On error resume next
shCurrentWeek.Range("B4:B" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete

I’m assuming you don’t care if it fails, so there’s no need to do error trapping. This will keep an error message from displaying if there are no cells returned from your call to SpecialCells.

answered Dec 6, 2012 at 18:04

Daniel's user avatar

DanielDaniel

13k2 gold badges36 silver badges60 bronze badges

2

While Daniel Cook is right, you could use On Error Resume Next, here is another way of going about it, since using On Error Resume Next is really a last resort option in VBA (IMO).

The code below checks for blanks before it tries to use the SpecialCells method.

Option Explicit

Sub DeleteRow()

Dim lr As Long
Dim shCurrentWeek As Worksheet

Set shCurrentWeek = ActiveWorkbook.Sheets("Current Week")
lr = shCurrentWeek.Range("A" & Rows.Count).End(xlUp).Row

If Application.WorksheetFunction.CountBlank(shCurrentWeek.Range("B4:B" & lr)) <> 0 Then

    shCurrentWeek.Range("B4:B" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete

End If


End Sub

answered Dec 6, 2012 at 18:26

Scott Holtzman's user avatar

Scott HoltzmanScott Holtzman

27k5 gold badges36 silver badges72 bronze badges

1

Here is a piece of code I use for that type of operation.

Sub ClearBlank()

Dim i As Long

For i = Range(«B65536»).End(xlUp).Row To 8 Step -1
If IsEmpty(Cells(i, 4)) Then Rows(i).Delete

Next i

End Sub

I hope this helps!

answered Dec 6, 2012 at 19:23

Desert Spider's user avatar

Desert SpiderDesert Spider

7341 gold badge12 silver badges30 bronze badges

Not used VBA before today so I’m sure there is a neater way…but this code will delete any row with at least one empty cell. It assumes that you have a header row and that the first column is used for IDs so these cells are not ‘looked at’.

The nice thing about this script is that it works for any size of spreadsheet so you don’t need to hard-code values each time:

Sub DeleteIncompleteRows()
Dim row As Integer, col As Integer
Dim deleted As Integer
deleted = 0
Dim actualRow As Integer
Dim totalRows As Integer
Dim totalCols As Integer
totalRows = Application.CountA(Range("A:A"))
totalCols = Application.CountA(Range("1:1"))

For row = 2 To totalRows
actualRow = row - deleted
For col = 2 To totalCols
If ActiveSheet.Cells(actualRow, col).Value = "" Then
ActiveSheet.Rows(actualRow).Delete
deleted = deleted + 1
Exit For
End If
Next col
Next row
End Sub

All the best,

Scott

answered Jan 17, 2013 at 21:39

Scott's user avatar

ScottScott

1011 silver badge1 bronze badge

Удаление строк с пустыми ячейками

Rioran

Дата: Вторник, 24.06.2014, 21:28 |
Сообщение № 1

Группа: Авторы

Ранг: Ветеран

Сообщений: 903


Репутация:

290

±

Замечаний:
0% ±


Excel 2013

Всем привет.

Искал вариант попроще. Не нашёл, написал свой.

Следующий макрос на активном листе удаляет все строки, где в столбце А нет значения. Первая строка (заголовки) игнорируется.

[vba]

Код

Sub Row_Cleaner()
With ThisWorkbook.ActiveSheet

Dim Z As Long

For Z = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
     If .Cells(Z, 1).Value = «» Then Rows(Z & «:» & Z).Delete Shift:=xlUp
Next Z

End With
End Sub

[/vba]


Роман, Москва, voronov_rv@mail.ru
Яндекс-Деньги: 41001312674279

 

Ответить

ikki

Дата: Вторник, 24.06.2014, 21:54 |
Сообщение № 2

Группа: Друзья

Ранг: Старожил

Сообщений: 1906


Репутация:

504

±

Замечаний:
0% ±


Excel 2003, 2010

Rioran, и этот макрос работает у Вас правильно? %)


помощь по Excel и VBA
ikki@fxmail.ru, icq 592842413, skype alex.ikki

 

Ответить

RAN

Дата: Вторник, 24.06.2014, 21:56 |
Сообщение № 3

Группа: Друзья

Ранг: Экселист

Сообщений: 5645

Я тоже сомнении. <_<


Быть или не быть, вот в чем загвоздка!

 

Ответить

Rioran

Дата: Вторник, 24.06.2014, 21:59 |
Сообщение № 4

Группа: Авторы

Ранг: Ветеран

Сообщений: 903


Репутация:

290

±

Замечаний:
0% ±


Excel 2013

ikki, да, мою задачу решает. В оригинале у меня вместо ActiveSheet стоит Sheets(1), но это не меняет сути.

Мне же надо было удалить те строки, для которых в ячейках столбца А пусто. What could go wrong? :D


Роман, Москва, voronov_rv@mail.ru
Яндекс-Деньги: 41001312674279

 

Ответить

ikki

Дата: Вторник, 24.06.2014, 22:02 |
Сообщение № 5

Группа: Друзья

Ранг: Старожил

Сообщений: 1906


Репутация:

504

±

Замечаний:
0% ±


Excel 2003, 2010

в принципе — он может сработать правильно.
если в столбце A нет двух и более подряд пустых ячеек.


помощь по Excel и VBA
ikki@fxmail.ru, icq 592842413, skype alex.ikki

Сообщение отредактировал ikkiВторник, 24.06.2014, 22:03

 

Ответить

Rioran

Дата: Вторник, 24.06.2014, 22:05 |
Сообщение № 6

Группа: Авторы

Ранг: Ветеран

Сообщений: 903


Репутация:

290

±

Замечаний:
0% ±


Excel 2013

ikki, я всё осознал, иду исправлять :D


Роман, Москва, voronov_rv@mail.ru
Яндекс-Деньги: 41001312674279

 

Ответить

Rioran

Дата: Вторник, 24.06.2014, 22:08 |
Сообщение № 7

Группа: Авторы

Ранг: Ветеран

Сообщений: 903


Репутация:

290

±

Замечаний:
0% ±


Excel 2013

Госсссподи, Юпитер всемогущий, конечно же правильный вариант:

[vba]

Код

Sub Row_Cleaner()
With ThisWorkbook.ActiveSheet

Dim Z As Long

For Z = .Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
       If .Cells(Z, 1).Value = «» Then Rows(Z).Delete Shift:=xlUp
Next Z

End With
End Sub

[/vba]
Для начинающих экселистов — сравните два моих кода и найдите ошибку в логике первого кода yes


Роман, Москва, voronov_rv@mail.ru
Яндекс-Деньги: 41001312674279

Сообщение отредактировал RioranВторник, 24.06.2014, 22:15

 

Ответить

ikki

Дата: Вторник, 24.06.2014, 22:10 |
Сообщение № 8

Группа: Друзья

Ранг: Старожил

Сообщений: 1906


Репутация:

504

±

Замечаний:
0% ±


Excel 2003, 2010

кстати, вполне достаточно писать так: Rows(Z)


помощь по Excel и VBA
ikki@fxmail.ru, icq 592842413, skype alex.ikki

 

Ответить

Rioran

Дата: Вторник, 24.06.2014, 22:15 |
Сообщение № 9

Группа: Авторы

Ранг: Ветеран

Сообщений: 903


Репутация:

290

±

Замечаний:
0% ±


Excel 2013

ikki, спасибо, протестировал, убедился.

Заменил в посте № 32 громоздкое макро-рекодеровское Rows(Z & «:» & Z) на Ваше Rows(Z), так красивее =)


Роман, Москва, voronov_rv@mail.ru
Яндекс-Деньги: 41001312674279

 

Ответить

ikki

Дата: Вторник, 24.06.2014, 22:23 |
Сообщение № 10

Группа: Друзья

Ранг: Старожил

Сообщений: 1906


Репутация:

504

±

Замечаний:
0% ±


Excel 2003, 2010


может, так?[vba]

Код

Sub test()
     On Error Resume Next
     With Sheets(1)
         .Range(.[a2], .Cells(.Rows.Count, 1).End(xlUp)).SpecialCells(4).EntireRow.Delete
     End With
End Sub

[/vba]


помощь по Excel и VBA
ikki@fxmail.ru, icq 592842413, skype alex.ikki

 

Ответить

RAN

Дата: Вторник, 24.06.2014, 22:40 |
Сообщение № 11

Группа: Друзья

Ранг: Экселист

Сообщений: 5645

При всем моем уважении
Вам кажется, что ГОТОВЫЕ решения , это именно то место, где есть смысл обсуждать подобные вопросы?
Может есть смысл попросить модераторов почистить?


Быть или не быть, вот в чем загвоздка!

 

Ответить

Rioran

Дата: Вторник, 24.06.2014, 22:51 |
Сообщение № 12

Группа: Авторы

Ранг: Ветеран

Сообщений: 903


Репутация:

290

±

Замечаний:
0% ±


Excel 2013

ikki, классный метод, красиво! =)
RAN, здесь как раз обсуждается именно готовое решение по теме. Готовое решение предложено, теме соответствует. Что не так?


Роман, Москва, voronov_rv@mail.ru
Яндекс-Деньги: 41001312674279

Сообщение отредактировал RioranВторник, 24.06.2014, 22:53

 

Ответить

RAN

Дата: Вторник, 24.06.2014, 23:26 |
Сообщение № 13

Группа: Друзья

Ранг: Экселист

Сообщений: 5645

Ну хотя бы то, что предложенный макрос не соответствует теме, т.е. не удаляет пустые строки. (в т.ч и макрос ikki)


Быть или не быть, вот в чем загвоздка!

 

Ответить

Rioran

Дата: Среда, 25.06.2014, 00:45 |
Сообщение № 14

Группа: Авторы

Ранг: Ветеран

Сообщений: 903


Репутация:

290

±

Замечаний:
0% ±


Excel 2013

RAN, с этой точки зрения да, вопрос обсуждаем другой, хоть и смежный. Похоже, было бы лучше новую тему открыть, да поздно.

******

О, спасибо, перенесли! =)


Роман, Москва, voronov_rv@mail.ru
Яндекс-Деньги: 41001312674279

Сообщение отредактировал RioranСреда, 25.06.2014, 11:54

 

Ответить

_Boroda_

Дата: Среда, 25.06.2014, 01:38 |
Сообщение № 15

Группа: Модераторы

Ранг: Местный житель

Сообщений: 16618


Репутация:

6465

±

Замечаний:
0% ±


2003; 2007; 2010; 2013 RUS

Внесу и свои 5 копеек.

SpecialCells(4).EntireRow.Delete

Это был мой любимый способ до той поры, пока я не попался на ограничение Excelя. При достаточно большом количестве несвязанных диапазонов в SpecialCells этот метод правильно не работает. Тут (на форуме) это уже неоднократно обсуждалось.


Скажи мне, кудесник, любимец ба’гов…
Платная помощь:
Boroda_Excel@mail.ru
Яндекс-деньги: 41001632713405 | Webmoney: R289877159277; Z102172301748; E177867141995

 

Ответить

Alex_ST

Дата: Среда, 25.06.2014, 09:24 |
Сообщение № 16

Группа: Друзья

Ранг: Участник клуба

Сообщений: 3176


Репутация:

604

±

Замечаний:
0% ±


2003

Может есть смысл попросить модераторов почистить?

Абсолютно согласен.
Это всё оффтоп, т.к. посвящено не полировке сабжа топика, а переделке его «под себя». Хотите об этом поговорить? Создавайте топик в соответствующем разделе форума .
_Boroda_, Саша, ты же сам модератор и своим постом поощряешь офтопп и флуд?
[offtop]А по поводу SpecialCells, так с ними вообще нужно очень аккуратно, т.к. у этого свойства имеется гнусное свойство :) — если в диапазоне, для которого запрашиваешь SpecialCells, более одной ячейки, то всё работает нормально и выбираются только нужные ячейки этого диапазона. А вот если в диапазоне всего одна ячейка, то SpecialCells выбирают ВСЕ ячейки листа, удовлетворяющие условиям.[/offtop]



С уважением,
Алексей
MS Excel 2003 — the best!!!

 

Ответить

_Boroda_

Дата: Среда, 25.06.2014, 09:57 |
Сообщение № 17

Группа: Модераторы

Ранг: Местный житель

Сообщений: 16618


Репутация:

6465

±

Замечаний:
0% ±


2003; 2007; 2010; 2013 RUS

Саша, ты же сам модератор и своим постом поощряешь офтопп и флуд?

Бе-бе-бе. :p
Все равно тему чистить нужно было. А мне вчера лениво было дележкой заниматься.


Скажи мне, кудесник, любимец ба’гов…
Платная помощь:
Boroda_Excel@mail.ru
Яндекс-деньги: 41001632713405 | Webmoney: R289877159277; Z102172301748; E177867141995

 

Ответить

Rioran

Дата: Среда, 25.06.2014, 11:58 |
Сообщение № 18

Группа: Авторы

Ранг: Ветеран

Сообщений: 903


Репутация:

290

±

Замечаний:
0% ±


Excel 2013

_Boroda_, спасибо за отделение зёрен от плевел зёрен :)

Alex_ST, ikki, _Boroda_, получается, что надёжнее код из поста № 7, поочерёдно перебирающий ячейки первого столбца?


Роман, Москва, voronov_rv@mail.ru
Яндекс-Деньги: 41001312674279

Сообщение отредактировал RioranСреда, 25.06.2014, 11:58

 

Ответить

RAN

Дата: Среда, 25.06.2014, 12:14 |
Сообщение № 19

Группа: Друзья

Ранг: Экселист

Сообщений: 5645

[vba]

Код

Sub Мяу()
     For i = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
         If Len(Cells(i, 1)) Then
         Else
             If r Is Nothing Then
                 Set r = Rows(i)
             Else
                 Set r = Union(r, Rows(i))
             End If
         End If
     Next
     r.Delete
End Sub

[/vba]


Быть или не быть, вот в чем загвоздка!

 

Ответить

SkyPro

Дата: Среда, 25.06.2014, 12:24 |
Сообщение № 20

Группа: Друзья

Ранг: Старожил

Сообщений: 1206


Репутация:

255

±

Замечаний:
0% ±


2010

Ну и я вариант предложу :)
[vba]

Код

Sub deleteEmptyRows()
Dim x, i&, delRa As Range
x = Range(«A1:A» & [a65535].End(xlUp).Row).Value
      For i = 1 To UBound(x)
          If x(i, 1) = «» Then
              If delRa Is Nothing Then
                  Set delRa = Cells(i, 1)
              Else
                  Set delRa = Union(Cells(i, 1), delRa)
              End If
          End If
      Next
If Not delRa Is Nothing Then delRa.EntireRow.Delete
End Sub

[/vba]
Для офиса младше 2007 [a65535] заменить на [A1048576].


skypro1111@gmail.com

Сообщение отредактировал SkyProСреда, 25.06.2014, 12:26

 

Ответить

Понравилась статья? Поделить с друзьями:
  • Excel vba удалить строку если в ячейке
  • Excel vba удалить строки умной таблицы
  • Excel vba удалить строки диапазона
  • Excel vba удалить символы слева
  • Excel vba удалить символы в строке