Visual Basic 2007添加值

首先,我不是一个聪明人。 所以我试图合并两个值,并在第三个显示它们(这很容易),但我也希望它重复无限次(即它对每一行都是一样的,假设我把结果放在I5中,我也想在它下面的每一个(I6,I7,I8等等)

应该是:

Private Sub Worksheet_Change() if IsNumeric(Range("B1").sort) And IsNumeric(Range("B2").sort) then Dim value1 As Single Dim value2 As Single range(I5).sort = value+1 + value2 End Sub 

还是因为我觉得我很可怕?

您正在使用Range.Sort属性,您应该使用.Value

有几种方法可以实现你想要做的事情。 第一个选项是遍历范围,并将相关的值添加到每个单元格,如下所示:

 Public Sub addCells() Dim rng As Range, cell As Range 'Set the sheet name With ThisWorkbook.Worksheets("Sheet_Name") 'Set the range below: Set rng = .Range("I1:I10") 'Loop through range and add cells together For Each cell In rng cell.Value = cell.Offset(0, 2) + cell.Offset(0, 1) Next cell End Sub 

另一种方式来做到这一点,如果要添加的值是在例如列ABsorting:

 Public Sub addCells() Dim rng As Range, cell As Range 'Set the sheet name With ThisWorkbook.Worksheets("Sheet1") 'Add the first formula to the sheet .Range("C1").Value = "=SUM(A1+B1)" 'Change the range below to the range to be filled .Range("C1").AutoFill Destination:=.Range("C1:C10") End With End Sub