excel vba按钮循环范围并增加值

时间:2017-09-16 04:34:32

标签: excel vba

需要帮助,任何人都可以教我如何在循环或到达最后一个按钮后恢复原始值?例如。我将循环值设置为1到10,在10之后它应该返回到1。

Sub LOOP_BUTTON()
    Sheet17.[L2] = Sheet17.[L2].Value + 1
End Sub

谢谢

2 个答案:

答案 0 :(得分:0)

编辑:1

Sub Demo()
    If Sheet17.[L2].Value = 10 Then
        Sheet17.[L2].Value = 1
    Else
        Sheet17.[L2] = Sheet1.[L2] + 1
    End If
End Sub

编辑:2 如果您希望结果为1,2,3,4,5,6,5,4,3,2,1

Public IsDecrease As Boolean

Sub Demo()
    If IsDecrease Then
        If Sheet17.[L2].Value = 1 Then
            IsDecrease = False
            Sheet17.[L2].Value = 2
        Else
            Sheet17.[L2] = Sheet17.[L2] - 1
        End If
    Else
        If Sheet17.[L2].Value = 6 Then
            IsDecrease = True
            Sheet17.[L2].Value = 5
        Else
            Sheet17.[L2] = Sheet17.[L2] + 1
        End If
    End If
End Sub

答案 1 :(得分:0)

使用Mod

Sub LOOP_BUTTON()
    Sheet17.[L2] = (Sheet17.[L2].Value Mod 10) + 1
End Sub