首页 > 解决方案 > VBA-Excel 文本到数字格式问题 - 用错误的值覆盖

问题描述

我有一个代码,可以将选定单元格中的文本字符串转换为值。它工作得很好,直到选择中有一些隐藏的行。然后隐藏行下方的值将被上述单元格中的值覆盖。

您对如何修复以下代码有任何想法吗?

Sub text_to_values()

With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
End With

With Selection.SpecialCells(xlCellTypeVisible)
    .NumberFormat = "General"
    .Value = .Value
End With

With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
End With

Selection.NumberFormat = "0"

End Sub

标签: excelvba

解决方案


换成这个...

For each cll in Selection.SpecialCells(xlCellTypeVisible)
    cll.NumberFormat = “General”
    cll.Value = cll.Value
Next cll

推荐阅读