如何重置一个variables,以便另一个If语句可以使用它?

所以我有来自每年的每个月的数据,每个月分为两个15天的工作表。

我试图总结两个工作表,然后把这个值放到一个编译工作表中,然后移到下个月。

从现在开始,它只是从一月份开始添加所有东西,我已经尝试将其重置为0,但是这只会导致一切都为零。

我可以很容易地在代码的末尾应用一个公式来应用以前的单元格的减法,但是我认为最好是学习如何在VBA中进行。 也许我只是没有在谷歌中使用正确的话。

谢谢您的帮助。

Sub Sum() Dim DestSh As Worksheet Dim Sh As Worksheet Dim lastcol As Long Dim lastrow As Long Dim destrow As Long Dim prevval As Double Dim nextval As Double With Application .ScreenUpdating = False .EnableEvents = False End With Application.DisplayAlerts = False 'Deletes Compilation worksheet if it exists On Error Resume Next ActiveWorkbook.Worksheets("Compilation").Delete On Error GoTo 0 Application.DisplayAlerts = True 'Add a worksheet with the name "Compilation" Set DestSh = ActiveWorkbook.Worksheets.Add 'loop through all worksheets For Each Sh In ActiveWorkbook.Worksheets 'Loop through all worksheets except Compilation If IsError(Application.Match(Sh.Name, _ Array(DestSh.Name), 0)) Then 'Check for the last column on row 6 lastcol = Sh.Cells(6, Columns.Count).End(xlToLeft).Column 'Check for the last row that the last column above has lastrow = Sh.Cells(Rows.Count, lastcol).End(xlUp).Row 'Checks if Cell B3, contains JAN and then enters the contents to DestSh If Sh.Cells(3, 2).Value Like "*JAN*" Then destrow = DestSh.Cells(3, 2).End(xlUp).Row prevval = prevval nextval = prevval + Sh.Cells(lastrow, lastcol).Value DestSh.Cells(destrow + 2, 2).Value = nextval prevval = nextval End If If Sh.Cells(3, 2).Value Like "*FEB*" Then destrow = DestSh.Cells(3, 3).End(xlUp).Row prevval = prevval nextval = prevval + Sh.Cells(lastrow, lastcol).Value DestSh.Cells(destrow + 2, 3).Value = nextval prevval = nextval End If End If Next With Application .ScreenUpdating = True .EnableEvents = True End With End Sub 

那么根据我的新手近视,我并不十分明显。 谢谢@TimWilliams

将格式更改为if,然后将语句更改为:

 If Sh.Cells(3, 2).Value Like "*JAN*" Then destrow = DestSh.Cells(3, 2).Row DestSh.Cells(destrow, 2).Value = DestSh.Cells(destrow, 2).Value _ + Sh.Cells(lastrow, lastcol).Value End If