簡體   English   中英

Excel宏按鈕更改更改單元格值

[英]Excel macro button to change change cell values

關於如何獲取按鈕以基於表格更改單元格值的任何想法。 假設單元格A1是活動單元格,並且我希望每次我根據B列中的值單擊按鈕時都更改此值,只需在列表B1,B2,B3等下單擊即可。

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

我假設您想在達到B10值並再次單擊時返回到B1值。

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

如果您有興趣使用輔助單元,則可以使用以下代碼。

    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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM