我想要一个宏按钮,按下该按钮将读取数据输入单元格中的值,搜索具有完全匹配的行,然后编辑该行中的单元格,但是在不同的列中。
+--------+-------------------------+--------+
| Number | Name | Status |
+--------+-------------------------+--------+
| 0 | Panini Special Sticker | Got |
| 1 | Fifa Fair Play | Need |
| 2 | Logo/1 | Got |
| 3 | Logo/2 | Need |
| 4 | Mascot/1 | Got |
| 5 | Mascot/2 | Got |
| 6 | Trophy | Need |
| 7 | Official Ball | Got |
| 8 | Stadium Belo Horizonte1 | Got |
| 9 | Stadium Belo Horizonte2 | Need |
| 10 | Stadium Brasília1 | Got |
+--------+-------------------------+--------+
因此,如果我在数据输入框中输入“7”然后单击按钮,它将找到并选择第9行,然后将单元格C9的值更改为“交换”
答案 0 :(得分:1)
好的,如果我理解正确的话,您需要从单元格中获取输入(可以更改或特别是在单击按钮时?),在列A中找到具有不同工作表上匹配值的行,然后将该行的C列值更改为“Swap”。
尝试此操作(当数据输入单元格发生更改时会触发):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim entryCell As Range: Set entryCell = Me.Range("A1") '' change this to the address of your data entry cell
If Not Intersect(Target, entryCell) Is Nothing Then FindMatch (entryCell.Value)
End Sub
Private Sub FindMatch(ByVal cellInput As String)
Dim searchSheet As Worksheet: Set searchSheet = ThisWorkbook.Sheets("Sheet2") '' change this to the name of the sheet you search for the value on
Dim searchRange As Range: Set searchRange = searchSheet.Range("A1:A" & searchSheet.Range("A" & searchSheet.Rows.Count).End(xlUp).Row)
Dim found As Range
Set found = searchRange.Find(cellInput, LookIn:=xlValues, SearchDirection:=xlNext, MatchCase:=False)
If Not found Is Nothing Then
found.Offset(, 2).Value = "Swap"
Else
MsgBox "Value not found in 'searchRange'"
End If
End Sub
您可以将其放在数据输入单元所在的工作表模块中。这可能是最好的方法,但是如果需要将宏绑定到命令按钮,可以将Worksheet_Change子例程更改为:
Private Sub ButtonClick()
Dim entryCell As Range: Set entryCell = Me.Range("A1") '' change this to the address of your data entry cell
FindMatch (entryCell.Value)
End Sub
然后将按钮的点击事件与ButtonClick()