2015-06-27 43 views
1

我是新来的宏,但我怀疑其他可能在这里问题。运行一个简单的空白行遇到问题删除

这里是简单的宏在多片删除空白行workbook.Yes这里的片在的问题是张数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 

其中出现的错误是“用户定义的类型不定义为“

早些时候,我曾尝试下面的代码,并收到”语法错误“。我试着用搜索引擎,做了所有标准的修订(确保宏被启用,设计师关闭等等,我甚至救了我板作为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的细节问题我会真的很想学习如何使用最少的定制开箱即可开发宏。

谢谢

回答

1

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


测试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 
+0

非常感谢奥马尔。第一个在For Looop上有一个运行时错误,但第二个完美地工作。对此,我真的非常感激。 请您指出我发布的两套代码的问题吗? – ResponsiveConsilience

+0

第一个''.Row 1 Step -1'应该是'.row到1 Step -1'的语法错误,而且你所缺少的Dim i As Long代码没有写好代码,现在在第一个确保你改变了所有的sheet1名称因为它在我的电脑上运行正常。 – 0m3r

+0

非常感谢奥马尔。其实第二个完美地运行,但空行不被删除:(我不知道它是否因为我使用Excel 2013 – ResponsiveConsilience