根据值替换范围内的公式

时间:2020-08-07 14:34:12

标签: excel vba case

我是新手,完全是Google在VBA中教的,所以请客气,大声笑。我正在尝试寻找一种更快的方式来查看范围,并将公式替换为单元格的值,但仅适用于显示除“ Not Here”以外的值的单元格。我在下面粘贴了我当前正在使用的内容。它可以工作,但是处理速度很慢。我到处搜索过,但无法提出另一种方法。

Dim rng2 As Range

Set rng2 = Sheets("User Daily").Range("I2:I" & Application.WorksheetFunction.CountA(Sheets("Punch Times").Range("G:G")))

For Each Row In rng2.Rows
    For Each Cell In Row.Cells
        Select Case Cell.Value
        Case "Not Here"
            'do nothing
        Case Else
            Cell.Select
            Selection.Copy
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                   :=False, Transpose:=False
        End Select
    Next Cell
Next Row

1 个答案:

答案 0 :(得分:0)

将我的评论放入答案中

您可以使用Range.SpecialCells(xlCellTypeFormulas)

对于Case Else,以下3行可以简化为Cell.Value = Cell.Value

您可以将Set rng2 = ...行之后的所有内容替换为以下内容:

On Error Resume Next
Dim formulaCells As Range
Set formulaCells = rng2.SpecialCells(xlCellTypeFormulas)
On Error GoTo 0

If formulaCells Is Nothing Then Exit Sub '<~ no cells with formulas

Dim cell As Range
For Each cell In formulaCells
    If cell.Value <> "Not Here" Then
        cell.Value = cell.Value
    End If
Next