我的安裝如上圖所示。
邏輯宏的是,如果我在細胞B5
或在Range("B2:B26")
空單元格中輸入一個數字1
則輸出將是這種格式:
B2 3
B3 4
B4 2
B5 1
現在,它給我的輸出,但也有一定的缺點例如
如果我提供輸入8
到同一個單元,那麼它仍然會增加排名。我加入了一個匹配檢查,看看這個值是否存在,但它似乎不工作任何幫助,將不勝感激。
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim KeyCells As Range
Dim i As Long, Cel As Range, sht1 As Worksheet, j As Long, found As Boolean
Set sht1 = Sheet1
Set KeyCells = sht1.Range("B2:C26")
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
If Target.Column = 2 Then
For i = 2 To 26
If sht1.Range("B" & i) <> Empty And sht1.Range("B" & i).Value >= Target.Value And i <> Target.Row Then
sht1.Range("B" & i).Value = sht1.Range("B" & i).Value + 1
Else: End If
Next i
Else: End If
If Target.Column = 3 Then
For i = 2 To 26
If sht1.Range("C" & i) <> Empty And sht1.Range("C" & i).Value >= Target.Value And i <> Target.Row Then
sht1.Range("C" & i).Value = sht1.Range("C" & i).Value + 1
Else: End If
Next i
Else: End If
Else: End If
Call CreateDataLabels
Target.Select
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
我有點困惑。如果你在'B5'中鍵入'1',那麼會發生什麼? 'B3'中你是如何得到'4'的? –
@SiddharthRout if you will remove this part'found = False For i = 2 To 26 If sht1.Range(「B」&i)<> Empty and sht1.Range(「B」&i).Value = Target .Value And i <> Target.Row Then found = True 否則:結束如果 接下來,我會得到它。 – newguy
你能忘記代碼並解釋邏輯嗎? :) –