Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 10631|回复: 4

[已解决]运行时错误91 对象变量或with块变量未设置

[复制链接]
发表于 2012-3-6 10:23 | 显示全部楼层 |阅读模式
我写了一段代码,可是有时报错,有时不报错,
报错时,如下:
运行时错误91
对象变量或with块变量未设置,在语句 Rng1.EntireRow.Delete处

请高手帮我解决一下吧
Sub 删除空行空列()
    Dim Area_ As Range
    Dim Rng1 As Range
    Dim Rng2 As Range
    Dim Ro As Long
    Dim Col As Integer
    Dim lstRo As Long
    Dim lstCol As Integer
    If [a1] = "" Then [a1] = 1
    Set Area_ = ActiveSheet.UsedRange
    [a1].ClearContents
    lstRo = Area_.rows.Count
    lstCol = Area_.Columns.Count
    Application.ScreenUpdating = False
    For Ro = 1 To lstRo
        If Application.CountA(Area_.rows(Ro)) = 0 Then
            If Rng1 Is Nothing Then
                Set Rng1 = rows(Ro)
            Else
                Set Rng1 = Union(Rng1, rows(Ro))
            End If
        End If
    Next
    For Col = 1 To lstCol
        If Application.CountA(Area_.Columns(Col)) = 0 Then
            If Rng2 Is Nothing Then
                Set Rng2 = Columns(Col)
            Else
                Set Rng2 = Union(Rng2, Columns(Col))
            End If
        End If
    Next

    Rng1.EntireRow.Delete
    Rng2.EntireColumn.Delete
    Set Area_ = Nothing
    Set Rng1 = Nothing
    Set Rng2 = Nothing
    Application.ScreenUpdating = True

    End Sub

最佳答案
2012-3-6 10:55
本帖最后由 adders 于 2012-3-5 22:00 编辑

    For Ro = 1 To lstRo
        If Application.CountA(Area_.Rows(Ro)) = 0 Then
            If Rng1 Is Nothing Then
                Set Rng1 = Rows(Ro)
            Else
                Set Rng1 = Union(Rng1, Rows(Ro))
            End If
        End If
    Next

如果以上红色部分从来没有成立过,即Application.CountA(Area_.Rows(Ro)) 从来就没有=0时, Rng1就从来不会被定义,之后
   Rng1.EntireRow.Delete 就会出错

同理,以下红色部分如果从来未成立,则Rng2就不会被定义,之后的Rng2.EntireColumn.Delete就会出错

    For Col = 1 To lstCol
        If Application.CountA(Area_.Columns(Col)) = 0 Then
            If Rng2 Is Nothing Then
                Set Rng2 = Columns(Col)
            Else
                Set Rng2 = Union(Rng2, Columns(Col))
            End If
        End If
    Next

解决办法:在Rng1.EntireRow.Delete这一行上面加一行: On Error Resume Next
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2012-3-6 10:38 | 显示全部楼层
并说明题意,说不定高手可给出更简单的方法
回复

使用道具 举报

发表于 2012-3-6 10:55 | 显示全部楼层    本楼为最佳答案   
本帖最后由 adders 于 2012-3-5 22:00 编辑

    For Ro = 1 To lstRo
        If Application.CountA(Area_.Rows(Ro)) = 0 Then
            If Rng1 Is Nothing Then
                Set Rng1 = Rows(Ro)
            Else
                Set Rng1 = Union(Rng1, Rows(Ro))
            End If
        End If
    Next

如果以上红色部分从来没有成立过,即Application.CountA(Area_.Rows(Ro)) 从来就没有=0时, Rng1就从来不会被定义,之后
   Rng1.EntireRow.Delete 就会出错

同理,以下红色部分如果从来未成立,则Rng2就不会被定义,之后的Rng2.EntireColumn.Delete就会出错

    For Col = 1 To lstCol
        If Application.CountA(Area_.Columns(Col)) = 0 Then
            If Rng2 Is Nothing Then
                Set Rng2 = Columns(Col)
            Else
                Set Rng2 = Union(Rng2, Columns(Col))
            End If
        End If
    Next

解决办法:在Rng1.EntireRow.Delete这一行上面加一行: On Error Resume Next
回复

使用道具 举报

 楼主| 发表于 2012-3-6 11:17 | 显示全部楼层
高手就是经典的解答,崇敬,而不言谢了
回复

使用道具 举报

发表于 2013-6-14 09:28 | 显示全部楼层
adders 发表于 2012-3-6 10:55
For Ro = 1 To lstRo
        If Application.CountA(Area_.Rows(Ro)) = 0 Then
            If Rng1 ...

你的回复帮我大忙了 哈哈
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-21 15:51 , Processed in 0.267082 second(s), 8 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表