Excel精英培训网

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

[分享] 【烟花原创】VBA零基础之第66篇 单元格对象Range(十)

[复制链接]
发表于 2013-12-30 22:56 | 显示全部楼层 |阅读模式
  今天我们聊聊单元格中的Clear家族,该家族成员极具破坏性。
  在VBA中,有一点大家需要注意,当用代码操作对象后(典型的工作簿,工作表,单元格)没有撤消一说。
  万能的Ctrl+Z在这是不起作用的(当然恢复还是有方法的,不过得自己来写这个恢复的过程)。
  大家慎用,万一哪天说数据给清没了,找我也无济于事。
  Clear 方法
  清除整个对象。
  返回值: Variant
  清除指定对象的内容,格式,批注,相当于“全部清除”命令。
  示例:
  Dim rg As Range
  With Range("a3:e14")
     .Value = "=row()"
     For Each rg In .Cells
         If rg.Comment Is Nothing Then
             rg.AddComment rg.Value & ""
         End If
     Next
     MsgBox "确定后,开始清除A3:E14单元格区域", vbOKOnly
     .Clear
  End With
  先在单元格区域内填入公式,然后添加批注,最后再调用clear方法。

  ClearComments 方法
  清除指定区域的所有单元格批注。
  示例:
    Dim rg As Range
    With Range("a3:e14")
       .Value = "=row()"
       For Each rg In .Cells
           If rg.Comment Is Nothing Then
               rg.AddComment rg.Value &""
           End If
       Next
       MsgBox "确定后,开始清除A3:E14区域内的批注", vbOKOnly
       .ClearComments
    End With

  ClearContents 方法
  清除区域中的内容(公式),但保留区域中的格式设置。
  示例:
    With Range("c3:g20")
       .Clear
       .Value = 1
       .Interior.Color = vbBlue
       .Font.Bold = True
       .Borders.LineStyle = xlContinuous
       .Cells(1).AddComment "test"
       MsgBox "确定后,清除C3:G20内容"
       .ClearContents
       MsgBox "内容清除完成,重新填入公式,注意格式设置"
       .Value = "=row()&column()"
    End With

  ClearFormats 方法
  清除对象的格式设置。
  示例:
    With Range("c3:g20")
       .Clear
       .Value = "=row()^2*column()"
       .NumberFormat = "yyyy-m-d"
       .Interior.Color = 49407
       .Font.Bold = True
       .Borders.LineStyle = xlContinuous
       .Cells(1).AddComment "test"
        MsgBox "确定后,清除C3:G20区域的格式"
       .ClearFormats
       MsgBox "格式清除完成"
    End With

  ClearHyperlinks 方法
  删除指定区域中的所有超链接,但是格式还是保留在。
  示例:
    Dim rg As Range
    Dim strSheetName$
    '$字符串类型的缩写
    '完整写法为 dimstrSheetName as string
    Worksheets.Addafter:=Worksheets(Worksheets.Count)
    strSheetName = Worksheets(1).Name
    For Each rg In Range("a1:a10")
       rg.Hyperlinks.Add rg, "", "'" & strSheetName& "'!" & rg.Address(False, False)
        '为了避免工作表名中有特殊字符,前后用单引号包围。 '工作表名'!A1,链接类似。
    Next
    Range("a11:b20").Value ="hwc2ycy"
    MsgBox "测试单元格填充完成,确定后,开始清除单元格内的超链接"
    Range("a1").CurrentRegion.ClearHyperlinks
    MsgBox "超链接清除完成"

  ClearOutline 方法
  清除指定区域的分级显示。
  示例:
    Range("a1").CurrentRegion.ClearOutline
  有分组则清除分级,没有也不报错。

  这几个方法,在10的版本中,在 开始 选项卡中的 编辑 组内可找到对应的命令。
     66-1.jpg






评分

参与人数 1 +15 收起 理由
云影 + 15 很给力!

查看全部评分

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2013-12-31 08:28 | 显示全部楼层
回复

使用道具 举报

发表于 2014-6-18 12:37 | 显示全部楼层
回复

使用道具 举报

发表于 2014-6-19 09:41 | 显示全部楼层
{:1112:}
回复

使用道具 举报

发表于 2014-6-26 21:36 | 显示全部楼层
  ClearOutline 方法
  清除指定区域的分级显示。
谢谢分享,标记,六十六
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-2 18:28 , Processed in 0.452168 second(s), 15 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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