300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Excel 如何用VBA实现在sheet1!A1输入一个数字 在sheet2!的A列寻找是否有相同的...

Excel 如何用VBA实现在sheet1!A1输入一个数字 在sheet2!的A列寻找是否有相同的...

时间:2023-02-06 23:53:48

相关推荐

Excel 如何用VBA实现在sheet1!A1输入一个数字 在sheet2!的A列寻找是否有相同的...

如题,想知道如何用VBA实现在sheet1!A1输入一个数字,在sheet2!的A列寻找是否有相同的数字

显示效果为,如果有那么光标停留在那个相同数字的单元格,如果没有则弹出“没有相符数字”

附件如下

Private Sub Worksheet_Change(ByVal Target As Range)

Dim arr, i%, n%, j%

If Target.Address(0, 0) <> 'A1' Then Exit Sub

arr = Sheets(2).Range('A1:A' & Sheets(2).Range('A65536').End(3).Row)

n = 0

For i = 1 To UBound(arr)

If arr(i, 1) = Target.Value Then

n = n + 1

Sheets(2).Activate

Sheets(2).Range('A' & i).Select

End If

Next

If n = 0 Then MsgBox '没有相符数字!'

End Sub

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Row = 1 And Target.Column = 1 Then

Set x = Sheet2.Range('a1:a16').Find(Target.Value, , , xlWhole)

If x Is Nothing Then

MsgBox '没有相符数字': Exit Sub

Else

Sheet2.Select

Sheet2.Range('a' & x.Row).Select

End If

End If

End Sub

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。