如何在每个select单元格中添加ROUND(),但是

细胞是:

3.141516 =10/6 =rand() or blank etc... 

结果:

 =ROUND(3.141516,1) =ROUND(10/6,1) =ROUND(RAND(),1) 

如果空白 – 留空白(不是ROUND(,1))

我想select范围,并通过InputBox或十进制的东西

我发现如何在公式周围添加ROUND(),在常量周围添加空白单元格,使用inputbox,但全部使用单独的代码,而不是在一起。 我不是英雄所以我需要帮助。 谢谢 :)

 Sub RoundNum() Dim Rng As Range Dim WorkRng As Range Dim xNum As Integer On Error Resume Next xTitleId = "Round Numbers" Set WorkRng = Application.Selection Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8) xNum = Application.InputBox("Decimal", xTitleId, Type:=1) For Each Rng In WorkRng Rng.Value = Application.WorksheetFunction.Round(Rng.Value, xNum) Next End Sub Sub Makro1() Dim Str As String For Each cell In Selection Str = cell.FormulaR1C1 If Mid(Str, 1, 1) = "=" Then Str = Mid(Str, 2) cell.FormulaR1C1 = "=ROUND(" & Str & ",1)" Next cell End Sub 

最后我做了这样的事情:

 Sub rRoundIt() Dim rng As Range Dim rngArea As Range Dim AppCalc As Long On Error Resume Next With Application AppCalc = .Calculation .ScreenUpdating = False .Calculation = xlCalculationManual End With Set rng = Union(Selection.SpecialCells(xlCellTypeFormulas, xlNumbers), _ Selection.SpecialCells(xlCellTypeConstants, xlNumbers)) For Each rngArea In rng If Left(rngArea.Formula, 7) <> "=ROUND(" Then _ rngArea.Formula = "=ROUND(" & Replace(rngArea.Formula, Chr(61), vbNullString) & ", 1)" Next rngArea With Application .ScreenUpdating = True .Calculation = AppCalc End With End Sub 

谢谢你Jeeped 🙂

这个简短的sub利用Range.SpecialCells方法使用xlCellType枚举中的xlCellTypeConstants和xlCellTypeFormulas选项。 .SpecialCells进一步过滤只获得那些常数或公式,导致数字与xlNumbers选项。

 Sub roundIt() Dim r As Range, rng As Range With Worksheets("Sheet1") With .UsedRange.Cells Set rng = Union(.SpecialCells(xlCellTypeFormulas, xlNumbers), _ .SpecialCells(xlCellTypeConstants, xlNumbers)) For Each r In rng If Left(r.Formula, 7) <> "=ROUND(" Then _ r.Formula = "=ROUND(" & Replace(r.Formula, Chr(61), vbNullString) & ", 1)" Next r End With End With End Sub 

理想情况下,如果工作表的.UsedRange属性中没有表示数字的公式或常量,那么您会想要提供一些错误控制。 如果没有find,那么.SpecialCells将不会返回nothing

通过只集中于那些可能具有数值的单元格来应用ROUND函数 ,您应该大大缩短循环遍历工作表中的单元格的迭代次数。