Excle VBA代码,用于在sheet2中搜索现有数据

时间:2019-01-06 09:50:26

标签: excel vba

我需要以下物品的代码

如果 在sheet2的A2:A5000列中存在在sheet1的单元格B9中输入的数据的值, 识别并替换现有记录

其他 创建新行并更新详细信息

1 个答案:

答案 0 :(得分:0)

我已经为您启动了此操作,您只需完成替换代码。您可以在msgboxes中使用我的提示。

Option Explicit
Sub find_and_replace()
Dim whSrc           As Worksheet: Set whSrc = Worksheets("Sheet1")
Dim whDest          As Worksheet: Set whDest = Worksheets("Sheet2")
Dim strToFind       As String: strToFind = whSrc.Range("B9").Value
Dim RngToLookIn     As Range: Set RngToLookIn = whDest.Range("A2:A5000")
Dim rngFoundRange As Range


Set rngFoundRange = RngToLookIn.Find(What:=strToFind, SearchDirection:=xlNext)

If Not rngFoundRange Is Nothing Then
    'Your replace code
    MsgBox ("Found in Row: " & rngFoundRange.Row)
Else
    'not found, your new row code goes here
    MsgBox ("Last row: " & RngToLookIn.Find(What:="*", SearchDirection:=xlPrevious).Row)
End If

End Sub

ps。下次不要以“我需要”开头。显示,您已经编码了什么,有什么问题等。