Excel精英培训网

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

[已解决]请高手帮忙看一下 代码 出错在哪里 谢谢~!

[复制链接]
发表于 2013-10-16 13:58 | 显示全部楼层 |阅读模式
Sub SearchFormula()
Dim mySearch(), sh As Worksheet
Dim i As Integer
Dim c As Range
mySearch = Array("SUMIFS", "INDEX")
For i = 0 To UBound(mySearch)
For Each sh In Worksheets
With ActiveSheet.UsedRange.SpecialCells(xlCellTypeFormulas)
    Set c = .Find(mySearch(i), LookIn:=xlFormulas)
    Do
        c.Value = c.Value
        Set c = .FindNext(c)
    Loop Until c Is Nothing
End With,next
Next i
End Sub

最佳答案
2013-10-16 14:53
包含SUMIFS,INDEX函数的单元格转化成值了。
  1. Sub SearchFormula()
  2.     Dim mySearch(), sh As Worksheet
  3.     Dim i As Integer
  4.     Dim c As Range
  5.     Dim rg As Range
  6.     On Error Resume Next
  7.     mySearch = Array("SUMIFS", "INDEX")
  8.     For i = 0 To UBound(mySearch)
  9.         For Each sh In Worksheets
  10.             Set rg = sh.UsedRange.SpecialCells(xlCellTypeFormulas)
  11.             If Not rg Is Nothing Then
  12.                 With rg
  13.                     Set c = .Find(mySearch(i), LookIn:=xlFormulas, lookat:=xlPart)
  14.                     Do While Not c Is Nothing
  15.                         c.Value = c.Value
  16.                         Set c = .FindNext(c)
  17.                     Loop
  18.                 End With
  19.             End If
  20.             Set rg = Nothing
  21.             'Set c = Nothing
  22.         Next
  23.     Next i
  24. End Sub
复制代码
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2013-10-16 14:07 | 显示全部楼层
                End With,next
这一个错的地方。
回复

使用道具 举报

 楼主| 发表于 2013-10-16 14:07 | 显示全部楼层
hwc2ycy 发表于 2013-10-16 14:07
End With,next
这一个错的地方。

请问 该怎么改呢?
回复

使用道具 举报

发表于 2013-10-16 14:10 | 显示全部楼层
End With
next
回复

使用道具 举报

 楼主| 发表于 2013-10-16 14:19 | 显示全部楼层
hwc2ycy 发表于 2013-10-16 14:10
End With
next

老师,我改过之后
c.Value = c.Value 提示 对象变量或with块变量未设置
回复

使用道具 举报

发表于 2013-10-16 14:42 | 显示全部楼层
  1. Sub SearchFormula()
  2.     Dim mySearch(), sh As Worksheet
  3.     Dim i As Integer
  4.     Dim c As Range
  5.     Dim rg As Range
  6.     On Error Resume Next
  7.     mySearch = Array("SUMIFS", "INDEX")
  8.     For i = 0 To UBound(mySearch)
  9.         For Each sh In Worksheets
  10.             Set rg = sh.UsedRange.SpecialCells(xlCellTypeFormulas)
  11.             If Not rg Is Nothing Then
  12.                 With rg
  13.                     Set c = .Find(mySearch(i), LookIn:=xlFormulas)
  14.                     Do While c Is Nothing
  15.                         c.Value = c.Value
  16.                         Set c = .FindNext(c)
  17.                     Loop
  18.                 End With
  19.             End If
  20.         Next
  21.     Next i
  22. End Sub
复制代码
回复

使用道具 举报

发表于 2013-10-16 14:42 | 显示全部楼层
你是要去掉所有包含SUMIFS,INDEX函数的公式的单元格为值吧。
回复

使用道具 举报

发表于 2013-10-16 14:46 | 显示全部楼层
你还是说你要实现的用途吧,代码有点乱。
回复

使用道具 举报

发表于 2013-10-16 14:53 | 显示全部楼层    本楼为最佳答案   
包含SUMIFS,INDEX函数的单元格转化成值了。
  1. Sub SearchFormula()
  2.     Dim mySearch(), sh As Worksheet
  3.     Dim i As Integer
  4.     Dim c As Range
  5.     Dim rg As Range
  6.     On Error Resume Next
  7.     mySearch = Array("SUMIFS", "INDEX")
  8.     For i = 0 To UBound(mySearch)
  9.         For Each sh In Worksheets
  10.             Set rg = sh.UsedRange.SpecialCells(xlCellTypeFormulas)
  11.             If Not rg Is Nothing Then
  12.                 With rg
  13.                     Set c = .Find(mySearch(i), LookIn:=xlFormulas, lookat:=xlPart)
  14.                     Do While Not c Is Nothing
  15.                         c.Value = c.Value
  16.                         Set c = .FindNext(c)
  17.                     Loop
  18.                 End With
  19.             End If
  20.             Set rg = Nothing
  21.             'Set c = Nothing
  22.         Next
  23.     Next i
  24. End Sub
复制代码

评分

参与人数 1 +1 收起 理由
pvppk + 1 很给力!

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2013-10-16 15:01 | 显示全部楼层
hwc2ycy 发表于 2013-10-16 14:53
包含SUMIFS,INDEX函数的单元格转化成值了。

对的 是希望把表格中 包含 SUMIFS INDEX 函数的单元格都变成数值,
但是之前的代码都只能作用于一个 sheet ,
所以加上了 sh as worksheet ,for each sh in worksheets希望代码能对整个工作表起作用
修改后发现 还是不行
现在用了您这段代码 已经完全可以实现了 ,非常感谢!
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 04:04 , Processed in 0.459342 second(s), 13 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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