简体   繁体   中英

Excel macro button to change change cell values

Any ideas on how to get a button to change cell values based on a table. Say cell A1 is the active cell and i want this value changed everytime I click the button based on the values in column B,just going down the list B1, B2,B3 etc...

Private Sub CommandButton1_Click()
    Range("A1").Value = Range("B1:B10").Value  
End Sub

I've assumed that you want to return to the B1 value once you've reached the B10 value and another click is made.

Private Sub CommandButton1_Click()
    If IsError(Application.Match(Range("A1").Value, Range("B1:B10"), 0)) Then
        Range("A1").Value = Range("B1").Value
    ElseIf Application.Match(Range("A1").Value, Range("B1:B10"), 0) = Range("B1:B10").Cells.Count Then
        Range("A1").Value = Range("B1").Value
    Else
        Range("A1").Value = Range("B1").Offset(Application.Match(Range("A1").Value, Range("B1:B10"), 0), 0).Value
    End If
End Sub

If you interested to use a helper cell then you can use following codes.

    Private Sub CommandButton1_Click()
    Dim increment As Long
        increment = Range("XFD1048576").Value
          Range("A1").Value = Range("B1").Offset(increment, 0).Value
        Range("XFD1048576").Value = increment + 1

        If Range("XFD1048576").Value >= Range("B:B").End(xlDown).Row Then
            Range("XFD1048576").Value = 0
        End If
    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