Excel VBA范围(单元格).Value = sum 1004:应用程序定义或对象定义

时间:2013-06-12 07:30:57

标签: vba excel-vba user-defined-functions excel

Function ChangeColVal(ByVal Rng As Range, ByVal ValueToChange As Integer)
    Dim Cell1, Cell2 As String
    Dim PosOfColon, TotalCell, Sum As Integer
    PosOfColon = InStr(1, Rng.Address, ":")
    Cell1 = Left(Rng.Address, PosOfColon - 1)
    Cell2 = Right(Rng.Address, Len(Rng.Address) - PosOfColon)
    If Left(Cell1, 2) = Left(Cell2, 2) Then
        TotalCell = Rng.Count
        For i = 0 To TotalCell
            If IsNumeric(Range(Cell1).Offset(i, 0).Value) = False Then
                GoTo 112:
            End If
            Cell2 = Range(Cell1).Offset(i, 0).Address
            Sum = Range(Cell2).Cells.Value + ValueToChange
            On Error GoTo 111
            'Here getting error...
            Range(Cell2).Value = Sum
            GoTo 112
111:
            MsgBox (Err.Number & ":" & Err.Description)
112:
        Next i
    Else
        MsgBox ("Select Column only...")
    End If
End Function

我想增加或减少所选范围的单元格值。 我在行Range(Cell2).Value = Sum

中收到错误

修改
响应的thanx,在行范围内(Cell2).Value = Sum,Cell2指向单元格地址,如$ E $ 6。 a

如果有任何其他选择请告诉我

3 个答案:

答案 0 :(得分:0)

好的Mihir。您不能使用UDF(用户定义函数)来修改其他单元格,然后使用公式的单元格。但是,通过从子函数调用函数可以解决此问题。我会告诉你如何做到这一点。
打开一个新工作簿,使您的电子表格如下图所示。
填写Sheet1的A栏中的一些随机数字 sheet1 setup
然后打开VBE并创建一个新模块(Project Explorer> Insert> Module)并粘贴下面的代码

Option Explicit

Sub ChangeColumnValue()

   Dim rng As Range
   Set rng = Selection

   Dim inp As String
   inp = InputBox("Value to change:")

   Call ChangeColVal(rng, CLng(inp))

End Sub

Private Function ChangeColVal(ByRef rng As Range, ByVal ValueToChange As Long)
    Dim Cell1, Cell2 As String
    Dim PosOfColon, TotalCell, Sum As Long
    PosOfColon = InStr(1, rng.Address, ":")
    Cell1 = Left(rng.Address, PosOfColon - 1)
    Cell2 = Right(rng.Address, Len(rng.Address) - PosOfColon)
    If Left(Cell1, 2) = Left(Cell2, 2) Then
        TotalCell = rng.Count
        Dim i&
        For i = 0 To TotalCell - 1
            If Not IsNumeric(Range(Cell1).Offset(i, 0).Value) Then
                GoTo 112:
            End If
            Cell2 = Range(Cell1).Offset(i, 0).Address
            Sum = Range(Cell2).Cells.Value + ValueToChange
            On Error GoTo 111
            Range(Cell2).Value = Sum
            GoTo 112
111:
            MsgBox (Err.Number & ":" & Err.Description)
112:
        Next i
    Else
        MsgBox ("Select Column only...")
    End If
End Function


然后,返回到您的电子表格并使用鼠标选择您的范围,如下所示:
select range with mouse
好的,现在点击ALT + F8View Macros并运行ChangeColumnValue宏。
select macro


您将提示更改值,并且该值将增加/减少所选范围内的所有单元格。
prompt value to change
如果在提示框中输入5,则最终结果将如下所示 result
祝你好运!

答案 1 :(得分:0)

最后这是解决方案。

Sub ChangeRowValues()

Dim rng As Range
Dim R As Range
Dim ValueToChange As Long
Dim Cell1, Cell2 As String
Dim PosOfColon, TotalCell, Sums, i As Integer
Set rng = Selection
ValueToChange = Val(InputBox("Enter Number to Change:", "Enter Number...", 0))
PosOfColon = InStr(1, rng.Address, ":")
Cell1 = Left(rng.Address, PosOfColon - 1)
Cell2 = Right(rng.Address, Len(rng.Address) - PosOfColon)
If UCase(Left(Cell1, 1)) = UCase(Left(Cell2, 1)) Then
    TotalCell = rng.Count
    For i = 0 To TotalCell - 1
        'To Chnage Values of Row....
        Range(Cell1).Offset(0, i).Select

        'To Chnage Values of Colum....
        'Range(Cell1).Offset(i,0).Select

        If IsNumeric(ActiveCell.Value) = True Then
            ActiveCell.Value = Val(ActiveCell.Value) + ValueToChange
        End If
    Next i
Else
    MsgBox ("Select Row only...")
End If

End Sub

答案 2 :(得分:-1)

range()函数输入是单元格地址,例如,范围(a1:b2; g1:h4),你不能在其中输入一些结果。