Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 使用VBA将舍入函数插入当前单元格_Excel_Vba_Cell_Formula - Fatal编程技术网

Excel 使用VBA将舍入函数插入当前单元格

Excel 使用VBA将舍入函数插入当前单元格,excel,vba,cell,formula,Excel,Vba,Cell,Formula,我正试图使将Round函数插入到一些已经有公式的单元格中变得更容易 例如,如果单元格A1的公式为=b1+b2,使用此宏后,我希望单元格内容为=Round(b1+b2,)。每个单元格中的公式都不相同,因此b1+b2部分必须是任意内容 我所能做的就是: Sub Round() Activecell.FormulaR1C1 = "=ROUND(b1+b2,)" End Sub 因此,我真的在寻找某种方法来获取选定单元格中的公式,然后使用VBA编辑这些内容。我到处都找不到答案。这个

我正试图使将Round函数插入到一些已经有公式的单元格中变得更容易

例如,如果单元格A1的公式为
=b1+b2
,使用此宏后,我希望单元格内容为
=Round(b1+b2,)
。每个单元格中的公式都不相同,因此
b1+b2
部分必须是任意内容

我所能做的就是:

Sub Round()

    Activecell.FormulaR1C1 = "=ROUND(b1+b2,)"     
End Sub
因此,我真的在寻找某种方法来获取选定单元格中的公式,然后使用VBA编辑这些内容。我到处都找不到答案。

这个怎么样

Sub applyRound(R As Range)
    If Len(R.Formula) > 0 Then
        If Left(R.Formula, 1) = "=" Then
            R.Formula = "=round(" & Right(R.Formula, Len(R.Formula) - 1) & ",1)"
        End If
    End If
End Sub

这是brettville方法的一个变体,基于此

  • 适用于当前选定内容中的所有公式单元格
  • 使用数组、特殊单元格和字符串函数优化速度。如果有许多单元格,在范围内循环可能非常慢

    Sub Mod2()
    Dim rng1 As Range
    Dim rngArea As Range
    Dim i As Long
    Dim j As Long
    Dim X()
    Dim AppCalc As Long
    
    On Error Resume Next
    Set rng1 = Selection.SpecialCells(xlFormulas)
    On Error GoTo 0
    If rng1 Is Nothing Then Exit Sub
    
    With Application
        AppCalc = .Calculation
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With
    
    For Each rngArea In rng1.Areas
        If rngArea.Cells.Count > 1 Then
            X = rngArea.Formula
            For i = 1 To rngArea.Rows.Count
                For j = 1 To rngArea.Columns.Count
                    X(i, j) = "=ROUND(" & Right$(X(i, j), Len(X(i, j)) - 1) & ",1)"
                Next j
            Next i
            rngArea = X
        Else
            rngArea.Value = "=Rround(" & Right$(rngArea.Formula, Len(rngArea.Formula) - 1) & ",1)"
        End If
    Next rngArea
    
    With Application
        .ScreenUpdating = True
        .Calculation = AppCalc
    End With
    End Sub
    
  • 第二个“
    =round
    ”函数的键入错误为“
    =Rround
    ”。一旦修改为2轮,而不是1轮,这个过程对我来说非常有效。我可以添加另一个
    if
    语句,以检查是否已经存在一个“
    =round
    ”公式,以防止某人在一轮中运行多次或舍入


    Darryl

    完全修改的程序如下

        Sub Round_Formula()
        Dim c As Range
        Dim LResult As Integer
        Dim leftstr As String
        Dim strtemp As String
        Set wSht1 = ActiveSheet
        Dim straddress As String
        Dim sheet_name As String
        sheet_name = wSht1.Name
        'MsgBox (sheet_name)
    
        straddress = InputBox(Prompt:="Full cell Address where to insert round function as D8:D21", _
          Title:="ENTER Address", Default:="D8:D21")
    
    
        With Sheets(sheet_name)
        For Each c In .Range(straddress)
           If c.Value <> 0 Then
            strtemp = c.Formula
            'MsgBox (strtemp)
            LResult = StrComp(Left(strtemp, 7), "=ROUND(", vbTextCompare)
            'MsgBox ("The value of LResult is " & LResult)
            If LResult <> 0 Then
                'c.Formula = "=ROUND(" & Right(c.Formula, Len(c.Formula) - 1) & ",2)"
                c.Formula = "=ROUND(" & Right(c.Formula, Len(c.Formula) - 1) & ",0)"
            End If
        End If
    Next c
    
    End With
    End Sub
    
    Sub-Round_公式()
    调光范围
    Dim LResult作为整数
    Dim leftstr作为字符串
    将strtemp设置为字符串
    设置wSht1=ActiveSheet
    她穿得像绳子
    尺寸表名称为字符串
    工作表\u name=wSht1.name
    'MsgBox(图纸名称)
    straddress=InputBox(提示:=“插入圆函数的完整单元格地址为D8:D21”_
    标题:=“输入地址”,默认值:=“D8:D21”)
    带图纸(图纸名称)
    对于每个c In.范围(跨接裙)
    如果c.值为0,则
    strtemp=c.公式
    'MsgBox(strtemp)
    LResult=StrComp(左(strtemp,7),“=ROUND(,vbTextCompare)
    'MsgBox(“LResult的值为”&LResult)
    如果LResult为0,则
    'c.Formula=“=圆形(&右(c.Formula,Len(c.Formula)-1和',2)”
    c、 公式=“=ROUND(&Right(c.Formula,Len(c.Formula)-1)和“,0)”
    如果结束
    如果结束
    下一个c
    以
    端接头
    
    试试这个

    对于选择中的每个n N.formula=“round(&mid(N.formula,2100)和”,1) 下一个


    我假设您现有公式的长度小于100个字符,灵敏度为1。您可以更改这些值。我改进了Sumit Saha提供的答案,以提供以下功能:

  • 用鼠标选择一个或多个范围
  • 输入所需的位数,而不是编辑代码
  • 输入通过更改iNum的行顺序选择的不同区域的位数,如所述
  • 问候,

        Sub Round_Formula_EREX()
        Dim c As Range
        Dim LResult As Integer
        Dim leftstr As String
        Dim strtemp As String
        Set wSht1 = ActiveSheet
        Dim straddress As Range
        Dim iNum As Integer
    
        Set straddress = Application.Selection
        Set straddress = Application.InputBox("Range", xTitleId, straddress.Address, Type:=8)
        iNum = Application.InputBox("Decimal", xTitleId, Type:=1)
    
        For Each c In straddress
           If c.Value <> 0 Then
        strtemp = c.Formula
    
        LResult = StrComp(Left(strtemp, 7), "=ROUND(", vbTextCompare)
    
        If LResult <> 0 Then
        'If you want to enter different digits for different regions you have selected,
        'activate next line and deactivate previous iNum line.
        'iNum = Application.InputBox("Decimal", xTitleId, Type:=1)
    
         c.Formula = "=ROUND(" & Right(c.Formula, Len(c.Formula) - 1) & "," & iNum & ")"
          End If
         End If
        Next c
    
        End Sub
    
    Sub-Round_-Formula_-EREX()
    调光范围
    Dim LResult作为整数
    Dim leftstr作为字符串
    将strtemp设置为字符串
    设置wSht1=ActiveSheet
    作为靶场的衣服
    作为整数的Dim iNum
    Set=Application.Selection
    设置straddress=Application.InputBox(“范围”,xTitleId,straddress.Address,类型:=8)
    iNum=Application.InputBox(“十进制”,xTitleId,类型:=1)
    每件衣服上的c
    如果c.值为0,则
    strtemp=c.公式
    LResult=StrComp(左(strtemp,7),“=ROUND(,vbTextCompare)
    如果LResult为0,则
    '如果要为所选的不同区域输入不同的数字,
    '激活下一行并停用上一行。
    'iNum=Application.InputBox(“十进制”,xTitleId,类型:=1)
    c、 公式=“=圆形(&Right(c.Formula,Len(c.Formula)-1)和“&iNum&”)
    如果结束
    如果结束
    下一个c
    端接头
    
    @user1050200:如果答案符合您的需要,请不要忘记接受。回答VBA问题需要很多技巧,哈哈。我没有测试您的解决方案,但它看起来很可靠。这更适合您提到的答案的评论部分。