将工作表的列转换为仅值(删除公式)

时间:2015-11-24 11:15:08

标签: excel-vba vba excel

请你帮我解决下面的公式?它给出了对象定义或app定义的错误。非常感谢。

Sub cellstovalues()

    Sheets("Parsing").Select
    Columns("B:B").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub

2 个答案:

答案 0 :(得分:0)

您的代码在选择整个列B时遇到问题。试试这个。这应该找到列中最后使用的单元格,然后复制并粘贴以将公式转换为您想要的值。

Sub cellstovalues()
    Dim ws As Worksheet
    Dim LastRow As Integer
    Dim rng As Range

    Set ws = ThisWorkbook.Sheets("Parsing")
    With ws
        LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
        Set rng = .Range(.Cells(1, "B"), .Cells(LastRow, "B"))
    End With
    rng.Copy
    rng.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub

答案 1 :(得分:0)

作为替代方案,您只需将值分配给范围的Value2(或Value)属性:

Sub cellstovalues()
    With Sheets("Parsing")
        With Intersect(.Range("B:B"), .UsedRange)
            .Value2 = .Value2
        End With
    End With
End Sub

或针对特定范围:

Sub cellstovalues()
    With Sheets("Parsing").Range("B1:C10")
         .Value2 = .Value2
    End With
End Sub