Excel精英培训网

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

[已解决]遍历工作簿工作表单元格

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

      下面代码只能实现活动工作表中有数据单元格的公式清除和14位以上数字的完整显示,但如果一个工作簿中有两个以上工作表时,非活动工作表中的数据不起作用。希望将ActiveSheet改为所有工作表,可以重新写可以实现下面效果的代码,谢谢!
Sub setText(sh As Worksheet)
    Dim cell As Range '声明单元格对象变量
    Dim 计算状态 As Long
    计算状态 = Application.Calculation
    If 计算状态 = xlAutomatic Then Application.Calculation = xlManual   '如当前是自动改为手动计算,不然如有随机函数会在转化过程中数值变化
    For Each cell In ActiveSheet.UsedRange
          If Len(cell) > 14 Then
             cell.NumberFormat = "@"
             cell.Value = cell.Value
          ElseIf cell.HasFormula Then
             cell.NumberFormat = "@"
             cell.Value = cell.Value
          End If
    Next cell
    Application.Calculation = 计算状态  '恢复之前计算状态
End Sub

最佳答案
2016-5-18 15:16
  1. Sub setText()
  2.     Dim sht As Worksheet
  3.     Dim cell As Range '声明单元格对象变量
  4.     Dim 计算状态 As Long
  5.     计算状态 = Application.Calculation
  6.     If 计算状态 = xlAutomatic Then Application.Calculation = xlManual   '如当前是自动改为手动计算,不然如有随机函数会在转化过程中数值变化
  7.     For Each sht In Worksheets
  8.     For Each cell In sht.UsedRange
  9.           If Len(cell) > 14 Then
  10.              cell.NumberFormat = "@"
  11.              cell.Value = cell.Value
  12.           ElseIf cell.HasFormula Then
  13.              cell.NumberFormat = "@"
  14.              cell.Value = cell.Value
  15.           End If
  16.     Next cell
  17.     Next sht
  18.     Application.Calculation = 计算状态  '恢复之前计算状态
  19. End Sub
复制代码
发表于 2016-5-18 15:16 | 显示全部楼层    本楼为最佳答案   
  1. Sub setText()
  2.     Dim sht As Worksheet
  3.     Dim cell As Range '声明单元格对象变量
  4.     Dim 计算状态 As Long
  5.     计算状态 = Application.Calculation
  6.     If 计算状态 = xlAutomatic Then Application.Calculation = xlManual   '如当前是自动改为手动计算,不然如有随机函数会在转化过程中数值变化
  7.     For Each sht In Worksheets
  8.     For Each cell In sht.UsedRange
  9.           If Len(cell) > 14 Then
  10.              cell.NumberFormat = "@"
  11.              cell.Value = cell.Value
  12.           ElseIf cell.HasFormula Then
  13.              cell.NumberFormat = "@"
  14.              cell.Value = cell.Value
  15.           End If
  16.     Next cell
  17.     Next sht
  18.     Application.Calculation = 计算状态  '恢复之前计算状态
  19. End Sub
复制代码
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-8 13:19 , Processed in 0.201762 second(s), 7 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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