提取EXCEL工作表中公式的单元格的代码

Sub 提取带公式的单元格()
    Dim FormulaCells As Range, Cell As Range
    Dim FormulaSheet As Worksheet
    Dim Row As Integer
    
    '创建Range对象
    On Error Resume Next
    Set FormulaCells = Range("A1").SpecialCells(xlCellTypeFormulas, 23)
    
    '没有找到公式
    If FormulaCells Is Nothing Then
        MsgBox "当前工作表中没有公式!"
        Exit Sub
    End If
    
    '增加一个新工作表
    Application.ScreenUpdating = False
    Set FormulaSheet = ActiveWorkbook.Worksheets.Add
    FormulaSheet.Name = "“" & FormulaCells.Parent.Name & "”表中的公式"
    
    '列标题
    With FormulaSheet
        Range("A1") = "公式所在单元格"
        Range("B1") = "公式"
        Range("C1") = "值"
        Range("A1:C1").Font.Bold = True
    End With
    
    '读取公式,同时在状态栏中显示进度。
    Row = 2
    For Each Cell In FormulaCells
        Application.StatusBar = Format((Row - 1) / FormulaCells.Count, "0%")
        With FormulaSheet
            Cells(Row, 1) = Cell.Address _
                (RowAbsolute:=False, ColumnAbsolute:=False)
            Cells(Row, 2) = " " & Cell.Formula
            Cells(Row, 3) = Cell.Value
            Row = Row + 1
        End With
    Next Cell
    
    '调整列宽
    FormulaSheet.Columns("A:C").AutoFit
    Application.StatusBar = False

End Sub


---------------------------------------------------

Sub 提取带公式的单元格()
    Dim FormulaCells As Range, Cell As Range
    Dim FormulaSheet As Worksheet
    Dim Row As Integer
    
    '创建Range对象
    On Error Resume Next
    Set FormulaCells = Range("A1").SpecialCells(xlFormulas, 23)
    
    '没有找到公式
    If FormulaCells Is Nothing Then
        MsgBox "当前工作表中没有公式!"
        Exit Sub
    End If
    
    '增加一个新工作表
    Application.ScreenUpdating = False
    Set FormulaSheet = ActiveWorkbook.Worksheets.Add
    FormulaSheet.Name = "“" & FormulaCells.Parent.Name & "”表中的公式"
    
    '列标题
    With FormulaSheet
        Range("A1") = "公式所在单元格"
        Range("B1") = "公式"
        Range("C1") = "值"
        Range("A1:C1").Font.Bold = True
    End With
    
    '读取公式,同时在状态栏中显示进度。
    Row = 2
    For Each Cell In FormulaCells
        Application.StatusBar = Format((Row - 1) / FormulaCells.Count, "0%")
        With FormulaSheet
            Cells(Row, 1) = Cell.Address _
                (RowAbsolute:=False, ColumnAbsolute:=False)
            Cells(Row, 2) = " " & Cell.Formula
            Cells(Row, 3) = Cell.Value
            Row = Row + 1
        End With
    Next Cell
    
    '调整列宽
    FormulaSheet.Columns("A:C").AutoFit
    Application.StatusBar = False
End Sub
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值