Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

vba - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...