我该如何在Excel中执行此操作。我用它来处理A列,但我也想做其他列

时间:2015-07-20 20:31:46

标签: excel excel-vba vba

如何在Excel中执行此操作?我用它来处理A列,但我也想做其他列

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim T As Range, r As Range
    Set T = Intersect(Target, Range("A:A"))
    If T Is Nothing Then Exit Sub
    Application.EnableEvents = False
    For Each r In T
        With r
            .Offset(0, 1).Value = .Offset(0, 1).Value + .Value
            .ClearContents
        End With
    Next r
    Application.EnableEvents = True
End Sub

1 个答案:

答案 0 :(得分:0)

试试这个:

    Private Sub Worksheet_Change(ByVal Target As Range)

Dim T As Range, r As Range
Dim columnArray() As String, columnsToCopy As String
Dim i As Integer

columnsToCopy = InputBox("What columns (A,B,C, etc.) would you like to copy the data of?  Use SPACES, to separate columns")
columnArray() = Split(columnsToCopy)

For i = LBound(columnArray) To UBound(columnArray)
    Set T = Intersect(Target, Range("" & columnArray(i) & ":" & columnArray(i) & "")) 'Columns(columnArray(i)) & ":" & Columns(columnArray(i))))
    If T Is Nothing Then Exit Sub
    Application.EnableEvents = False
    For Each r In T
        With r
            .Offset(0, 1).Value = .Offset(0, 1).Value + .Value
            .ClearContents
        End With
    Next r
Next i
Application.EnableEvents = True
End Sub

这将创建一个弹出窗口,询问您要运行此列的列。每次细胞变化时运行它的想法是什么?这将是很多弹出窗口,等等。但是,如果这不起作用或有一些错误,请告诉我。