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 1263
星月不相逢
星月不相逢 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:03

    Since you're just searching if a value exists, you can shorten that code using the "Find" feature:

    Private Sub CommandButton1_Click()
    Dim rngCell As Range
    Dim dmrWS As Worksheet, searchWS As Worksheet
    Dim lngLstRow As Long, strSearchRow As Long, lngLstCol As Long
    Dim strSearch As String
    Dim r As Long
    Dim x As Variant
    Dim searchNewRow As Integer
    
    Set dmrWS = Sheets("DMR")
    Set searchWS = Sheets("SEARCH")
    
    strSearch = InputBox("Please enter 5 digit document number to search for (e.g. 00002):", "Search Value")
    
    
    With dmrWS
        On Error GoTo ErrorHandler
        strSearchRow = .Cells.Find(what:=strSearch, LookAt:=xlWhole).Row
    End With
    
    If strSearchRow > 0 Then 'If there was a value found
        searchNewRow = searchWS.UsedRange.Rows.Count
        With searchWS
            .Range(.Cells(searchNewRow, 1), .Cells(searchNewRow, 4)).Value = dmrWS.Range(dmrWS.Cells(strSearchRow, 1), dmrWS.Cells(strSearchRow, 4)).Value
        End With
    End If
    
    ErrorHandler:
    MsgBox (strSearch & " was not found.")
    
    End Sub
    

    I think that does what you want. If the string is found in "DMR" sheet, say on row 9, it'll copy A9:D9 to the next empty row in "Search" sheet. Please let me know if this isn't quite what you're looking for.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-04 01:16

    Final answer to my request and this works great!

    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
    
    Worksheets("SEARCH").Range("A5:B200").ClearContents
    
    Set dmr = Worksheets("DMR")
    pasteRowIndex = 5
    strSearch = InputBox("Please enter 5 digit document number to search for (e.g. 00002):", "Search Value")
    
    If strSearch = vbNullString Then
    MsgBox ("User canceled, or did not enter a value.")
    Exit Sub
    End If
    
    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
    
    If f Is Nothing Then
    MsgBox ("The document number you've entered either does not appear in this tool, or is not cross referenced in any other document.")
    Exit Sub
    End If
    End With
    End Sub
    
    0 讨论(0)
提交回复
热议问题