VBA: How to find search value from Sheet “DMR” and then from found search value row copy cell at column A and cell at Column D into Sheet “Search”

前端 未结 3 1280
星月不相逢
星月不相逢 2020-12-04 00:51

This is my first time asking for help on any VBA programming sites. I am very new to VBA programming (had some experience 10 years ago) and am trying to create a document cr

3条回答
  •  时光说笑
    2020-12-04 01:11

    This searches the desired range (G3:EP7002) in a loop to find all instances and will drop it in Sheet(Search) starting at A5:B5. It lacks the error checking of user3578951 but I leave you to figure that out ^_^

    Private Sub CommandButton1_Click()
    
    Dim dmr As Worksheet
    Dim strSearch As String
    Dim f As Variant
    Dim fAddress As String
    Dim fRow As Long
    Dim cellA As Variant
    Dim cellB As Variant
    
    Set dmr = Worksheets("DMR")
    pasteRowIndex = 5
    strSearch = InputBox("Please enter 5 digit document number to search for (e.g. 00002):", "Search Value")
    
    With dmr.Range("G3:EP7002")
        Set f = .Find(strSearch, LookIn:=xlValues)
        If Not f Is Nothing Then
            fAddress = f.Address
            Do
                fRow = f.Row
                cellA = dmr.Cells(fRow, 1).Value
                cellD = dmr.Cells(fRow, 4).Value
                Sheets("SEARCH").Cells(pasteRowIndex, 1) = cellA
                Sheets("SEARCH").Cells(pasteRowIndex, 2) = cellD
                pasteRowIndex = pasteRowIndex + 1
                Set f = .FindNext(f)
            Loop While Not f Is Nothing And f.Address <> fAddress
        End If
    End With
    
    End Sub
    

提交回复
热议问题