遇到问题 运行 一个简单的空白行删除

Having Trouble Running a Simple Blank Row Deleting

我是宏的新手,但我怀疑这里可能有其他问题。

这是删除多行中空白行的简单宏sheet workbook.Yes 这里的 sheet 是 sheet 数字 9。

Sub FnDelete­BlankRows()
Dim mwb As Work­Book
Set mwb = Active­Work­Book
For x = mwb.Sheets(“Sheet1”).Cells.SpecialCells(xlCellTypeLastCell).Row 1 Step –1
If WorksheetFunction.CountA(mwb.Sheets(“Sheet1”).Rows(x)) = 0 Then
mwb.Sheets(“Sheet9”).Rows(x).Delete
End If
Next
End Sub

出现的错误是"User-defined type not defined"

早些时候我尝试了下面的代码并收到了 "Syntax Error"。我尝试使用谷歌搜索并进行了所有标准修复(确保启用了宏,关闭了设计器等,我什至将我的 sheet 保存为 xltm)

Sub RemoveRows()
Dim lastrow As Long
Dim ISEmpty As Long
Count how many records in the list. This is done so that the Do loop has a finish point.

lastrow = Application.CountA(Range(“A:A”))
‘Start at the top of the list
Range(“A1″).Select

Loop until the end of the list
Do While ActiveCell.Row < lastrow
Assign number of non empty cells in the row
ISEmpty = Application.CountA(ActiveCell.EntireRow)
If ISEmpty = 0 then delete the row, if not move down a cell into the next row
If ISEmpty = 0 Then
ActiveCell.EntireRow.Delete 
Else
ActiveCell.Offset(1, 0).Select
End If
LoopEnd Sub

尽管我很喜欢学习 VBA 的精髓,但我真的很想学习如何以最少的自定义设置开箱即用的宏。

谢谢

试试这个 - 确保将 Sheet1 更改为您正在处理的 sheet。


已测试 Excel 2010

Option Explicit
'// Delete blank Rows
Sub xlDeleteBlankRows()
   Dim xlWb As Workbook
   Dim i As Long
   Set xlWb = ActiveWorkbook
   For i = xlWb.Sheets("Sheet1").Cells.SpecialCells(xlCellTypeLastCell).row To 1 Step -1
      If WorksheetFunction.CountA(xlWb.Sheets("Sheet1").Rows(i)) = 0 Then
         xlWb.Sheets("Sheet1").Rows(i).Delete
      End If
   Next
End Sub

Option Explicit
Sub xlRemoveRows()
    Dim lastrow As Long
    Dim ISEmpty As Long

    '// Count how many records in the list. This is done so that the Do loop has a finish point.
    lastrow = Application.CountA(Range("A:A"))

    '// Start at the top of the list
    Range("A1").Select

    '// Loop until the end of the list
    Do While ActiveCell.Row < lastrow

    '// Assign number of non empty cells in the row
    ISEmpty = Application.CountA(ActiveCell.EntireRow)
            '// If ISEmpty = 0 then delete the row, if not move down a cell into the next row
            If ISEmpty = 0 Then
            ActiveCell.EntireRow.Delete
            Else
        ActiveCell.Offset(1, 0).Select
        End If
    Loop
End Sub

请参阅 MSDN 上的有用文章 Getting Started with VBA in Excel 2010 & 2013


Option Explicit
Sub xlDeleteRow()
    Dim lngRow, lngCrnt, lngCol As Long
    Dim xlBln As Boolean
    Application.ScreenUpdating = False
    lngRow = Cells(Rows.count, "A").End(xlUp).Row
    For lngCrnt = lngRow To 1 Step -1
        xlBln = False
        For lngCol = 1 To 2
            If Cells(lngCrnt, lngCol).Value <> vbNullString Then
                xlBln = True
                Exit For
            End If
        Next lngCol
        If Not xlBln Then Rows(lngCrnt).Delete
    Next lngCrnt
    Application.ScreenUpdating = True
End Sub