[英]Macro button to change cell values
我在Stack Overflow上找到了此鏈接,它確實有很大幫助: 使用Macro更改單元格值 。 我使用了@Jeeped提供的答案。
通過按宏按鈕可以將單元格值從1更改為7。 當達到7時,它將再次從1開始。
但是,我想使用兩個按鈕,一個向下計數,另一個向上計數,當達到1時,按下向下計數按鈕不再影響數字,要進行更改,您必須按UP按鈕才能從向上計數1號。
我應該怎么做?
我假設您要模仿另一個問題的行為,因為這些按鈕通過范圍內找到的值列表來移動目標值。 顯然,這與簡單地遞增或遞減值不同。
注意:與其他問題一樣,這要求列表中的值是唯一的。
以下內容與另一個問題的代碼相似。 它在桌子下移動。 區別:它在最后一個值處停止(不返回到開始)。
Sub ButtonDown_Click()
Dim targetCell As Range
Set targetCell = Range("A1")
Dim tableRange As Range
Set tableRange = Range("B1:B10")
If IsError(Application.Match(targetCell.Value, tableRange, 0)) Then
'if value in the target cell is not in the table then reset to the first value in table
targetCell.Value = tableRange.Cells(1).Value
ElseIf Application.Match(targetCell.Value, tableRange, 0) < tableRange.Cells.Count Then
'if value in the target cell is NOT the last value in the table then move to the next value in the table
targetCell.Value = tableRange.Cells(1).Offset(Application.Match(targetCell.Value, tableRange, 0), 0).Value
End If
End Sub
以下是另一個在表格中向上移動的按鈕的代碼。 到達第一個值時停止。
Sub ButtonUp_Click()
Dim targetCell As Range
Set targetCell = Range("A1")
Dim tableRange As Range
Set tableRange = Range("B1:B10")
If IsError(Application.Match(targetCell.Value, tableRange, 0)) Then
'if value in the target cell is not in the table then reset to the first value in table
targetCell.Value = tableRange.Cells(1).Value
ElseIf Application.Match(targetCell.Value, tableRange, 0) > 1 Then
'if value in the target cell is NOT the first value in the table then move to the previous value in the table
targetCell.Value = tableRange.Cells(1).Offset(Application.Match(targetCell.Value, tableRange, 0) - 2, 0).Value
End If
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.