转到后续专栏

时间:2016-12-12 19:50:40

标签: excel vba excel-vba

我有一个计算,我将前一张纸上的单元格与后页面上的同一单元格进行比较。如果之前的时间少于之后,我会从另一个中减去一个并将这些结果存储在摘要页面中。  我需要为工作表的每一列执行此操作,并且很难确定如何在后续列中执行计算。列Y到BM有不同的类别。到目前为止,这是我的代码,对于y列工作正常,并在摘要表的第a列中保持运行总计。  任何人都可以帮我设置一个列范围并向前推进吗?谢谢

 Sub TryAgain3()
'evaluates differences in column y (appraisal) in before to column y in after.  if before is less than after (we underdisclosed) then it calculates the difference and sticks it in summary
Dim A() As Integer '~ I will put the result in this column, A
Dim i As Integer
Dim j As Integer
Dim lastrow As Integer
lastrow = Range("y3").End(xlDown).Row 
ReDim A(lastrow)
For i = 1 To lastrow
    If ThisWorkbook.Sheets("Before").Cells(i + 1, 25).Value < ThisWorkbook.Sheets("After").Cells(i + 1, 25) Then
    ThisWorkbook.Sheets("summary").Cells(i + 1, 1).Value = ThisWorkbook.Sheets("Before").Cells(i + 1, 25).Value - ThisWorkbook.Sheets("After").Cells(i + 1, 25).Value
    End If

Next I

Next
End Sub

1 个答案:

答案 0 :(得分:0)

正如Scott Craner在评论中所说,只需添加另一个循环,在当前循环之外循环通过列。

Sub TryAgain3()
    'evaluates differences in column y (appraisal) in before to column y in after.  if before is less than after (we underdisclosed) then it calculates the difference and sticks it in summary
    Dim i As Integer
    Dim j As Integer
    Dim lastrow As Integer
    lastrow = Range("y3").End(xlDown).Row 
    For j = 25 To 65  ' columns Y to BM
        For i = 1 To lastrow
            If ThisWorkbook.Sheets("Before").Cells(i + 1, j).Value < ThisWorkbook.Sheets("After").Cells(i + 1, j) Then
                ThisWorkbook.Sheets("summary").Cells(i + 1, j - 24).Value = ThisWorkbook.Sheets("Before").Cells(i + 1, j).Value - ThisWorkbook.Sheets("After").Cells(i + 1, j).Value
            End If
        Next I
    Next
End Sub