Excel精英培训网

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

[已解决]修改简化或重写代码

[复制链接]
发表于 2016-6-12 23:13 | 显示全部楼层 |阅读模式
本帖最后由 乐乐2006201506 于 2016-6-13 14:37 编辑

修改或简化下面代码。或者按照要求重新写代码。这是宏录制的代码。
主要实现隐藏所有有公式单元格中的公式的代码。谢谢!

Sub Macro1()
    Cells.Select
    Selection.Locked = False
    Selection.FormulaHidden = False
    Range("S5:S41").Select
    Selection.Locked = True
    Selection.FormulaHidden = True
    Range("S5").Select
    Sheets("管理 (模板)").Select
    ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
        False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
        AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
        :=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
        AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
        AllowUsingPivotTables:=True
    Range("C5").Select
End Sub

最佳答案
2016-6-13 09:01
不制定那些单元格是公式,将当前表格所有含有公式的单元格都隐藏:
  1. Sub Macro1()
  2.     Dim rng As Range
  3.     With Sheet1
  4.         .Unprotect
  5.         .Cells.Locked = False
  6.         .Cells.FormulaHidden = False
  7.         Set rng = .Cells.SpecialCells(xlCellTypeFormulas, 23)
  8.         rng.Locked = True
  9.         rng.FormulaHidden = True
  10.         .Protect
  11.     End With
  12. End Sub
复制代码

隐藏公式.rar

34.84 KB, 下载次数: 11

发表于 2016-6-13 08:15 | 显示全部楼层
  1. Cells.Locked = False
  2. Cells.FormulaHidden = False
  3. Range("S5:S41").Locked = True
  4. Range("S5:S41").FormulaHidden = True
  5. ActiveSheet.Protect passwrod = 124
复制代码
Range("S5:S41")  公式区域   自己修改替换
回复

使用道具 举报

发表于 2016-6-13 08:30 | 显示全部楼层
  1. Sub Macro1()
  2. Cells.Locked = False
  3. Cells.FormulaHidden = False
  4. Range("H5:H38,N5:N38,T5:U38,E24:U24,E40:U41").Select
  5. Selection.Locked = True
  6. Selection.FormulaHidden = True
  7. ActiveSheet.Protect passwrod = 124
  8. End Sub
复制代码
适用你的附件,密码124
回复

使用道具 举报

发表于 2016-6-13 09:01 | 显示全部楼层    本楼为最佳答案   
不制定那些单元格是公式,将当前表格所有含有公式的单元格都隐藏:
  1. Sub Macro1()
  2.     Dim rng As Range
  3.     With Sheet1
  4.         .Unprotect
  5.         .Cells.Locked = False
  6.         .Cells.FormulaHidden = False
  7.         Set rng = .Cells.SpecialCells(xlCellTypeFormulas, 23)
  8.         rng.Locked = True
  9.         rng.FormulaHidden = True
  10.         .Protect
  11.     End With
  12. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2016-6-13 11:58 | 显示全部楼层
本帖最后由 乐乐2006201506 于 2016-6-13 12:02 编辑
老司机带带我 发表于 2016-6-13 09:01
不制定那些单元格是公式,将当前表格所有含有公式的单元格都隐藏:

       谢谢您,您的代码中将我录制代码中下面代码删掉了。影不影响效果?我试了下,好像没有影响,是不是?
        ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
        False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
        AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
        :=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
        AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
        AllowUsingPivotTables:=True
        另外,我将密码添上了。您选中所有有公式的单元格的方法非常好,谢谢啦!
        Sub 老司机()    Dim rng As Range
    With Sheet1
        .Unprotect ("124")
        .Cells.Locked = False
        .Cells.FormulaHidden = False
        Set rng = .Cells.SpecialCells(xlCellTypeFormulas, 23)
        rng.Locked = True
        rng.FormulaHidden = True
        .Protect ("124")
    End With
End Sub

回复

使用道具 举报

发表于 2016-6-13 12:09 | 显示全部楼层
乐乐2006201506 发表于 2016-6-13 11:58
谢谢您,您的代码中将我录制代码中下面代码删掉了。影不影响效果?我试了下,好像没有影响,是不是 ...

你之前的是录制的,所以他吧保护工作表的选项都设置了一遍,我这个是按照默认的,所以效果实际一样!
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-19 15:11 , Processed in 0.187774 second(s), 6 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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