简体   繁体   中英

How to reset variable value to its default value in excel VBA?

VBA code

Sub colourcode()
    Dim tcol As Integer, ro As Integer, co As Integer

    ro = 2
    co = 2

    tcol = 10

    For i = 1 To tcol
    Cells(ro, co).Interior.Color = RGB(0, 0, 0)
    ro = ro + 2
     Next

'I want to reset a variable ro value to its default value which is 2.

    End Sub

If you want to minimize your maintenance issues use a constant for the starting point:

Sub colourcode()

    Const RO_START As Long = 2
    'any other fixed value...

    Dim tcol As Long, ro As Long, co As Long

    ro = RO_START
    co = 2
    tcol = 10

    For i = 1 To tcol
        ActiveSheet.Cells(ro, co).Interior.Color = RGB(0, 0, 0)
        ro = ro + 2
    Next

    ro = RO_START  'reset, avoiding repeating the 2

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM