vba - Excel VBA Range(Cell).Value=sum 1004 :Application-Defined Or Object-Defined

标签 vba excel user-defined-functions

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 回复,在 Range(Cell2).Value = Sum 行中,Cell2 指向像 $E$6 这样的单元格地址。
a

如果除此之外还有其他选择,请告诉我

最佳答案

好的,米希尔。您不能使用 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

祝你好运!

关于vba - Excel VBA Range(Cell).Value=sum 1004 :Application-Defined Or Object-Defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17059954/

相关文章:

vba - 与访问用户定义类型的字段相比,VBA 类模块属性是否较慢?

c++ - 程序检查以查看用户编号是否存储在 C++ 中的数组中

powershell - 将参数传递给函数

html - VBA获取在线货币

vba - 根据最后一行在excel中选择多行

VBA Excel - IE GetElementByID 不起作用 - 运行时错误 424 需要对象

excel - 我们可以像在 kdb 中那样在 excel 中加入 asof 吗

python - 使用 xlrd 从 http 网站打开 Excel

excel - Conditional Formatting,使用公式根据不同的单元格值范围进行格式化

scala - 合并 Spark 数据框中的列