2
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。 一个

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

4

3 回答 3

0

finally here is the solution.

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

于 2013-06-13T08:59:18.853 回答
0

好的,米希尔。您不能使用 UDF(用户定义函数)来修改其他单元格,然后是使用公式的单元格。但是,可以通过从 sub 调用您的函数来解决此问题。我会告诉你怎么做。
打开一个新工作簿并使您的电子表格如下图所示。
在 Sheet1 的 A 列中填写一些随机数
sheet1 设置
然后打开 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


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


系统将提示您输入要更改的值,所选范围内的所有单元格都将按该值增加/减少。
提示值改变
如果您5在提示框中键入,您的最终结果将如下所示
结果
祝您好运!

于 2013-06-13T07:35:16.013 回答
-1

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

于 2013-06-12T07:40:01.557 回答