0
votes

Sheet(LIST2) has 8 columns.Column A of Sheet(LIST2) contains id numbers.same ID number is repeated many times in many rows of column A.Column B to H contains other data. In sheets(Sheet1) A1 we type an ID number which find matches in Sheets(LIST2) column A and copy each math row from A to H

I found a code to copy the entire rows but what I want is only rows A to H only

Sub SearchForString()

Dim LCopyToRow As Integer


On Error GoTo Err_Execute


'Start copying data to row 2 in Sheet2 (row counter variable)
LCopyToRow = 3

Dim sheetTarget As String: sheetTarget = "sheet1"
Dim sheetToSearch As String: sheetToSearch = "LIST2"
Dim targetValue As String: targetValue = Sheets(sheetTarget).Range("A1").Value  'Value in sheet2!A1 to be searched in sheet1
Dim columnToSearch As String: columnToSearch = "A"
Dim iniRowToSearch As Integer: iniRowToSearch = 2
Dim LSearchRow As Long 'As far as it is not clear the number of rows you will be considering, better relying on the long type
Dim maxRowToSearch As Long: maxRowToSearch = 2000 'There are lots of rows, so better setting a max. limit

If (Not IsEmpty(targetValue)) Then
    For LSearchRow = iniRowToSearch To Sheets(sheetToSearch).Rows.Count

        'If value in the current row (in columnToSearch in sheetToSearch) equals targetValue, copy entire row to LCopyToRow in sheetTarget
        If Sheets(sheetToSearch).Range(columnToSearch & CStr(LSearchRow)).Value = targetValue Then

            'Select row in Sheet1 to copy
            Sheets(sheetToSearch).Rows(LSearchRow).Copy

            'Paste row into Sheet2 in next row
            Sheets(sheetTarget).Rows(LCopyToRow).PasteSpecial Paste:=xlPasteValues
            Sheets(sheetTarget).Rows(LCopyToRow).PasteSpecial Paste:=xlFormats
            'Move counter to next row
            LCopyToRow = LCopyToRow + 1
        End If

        If (LSearchRow >= maxRowToSearch) Then
            Exit For
        End If

    Next LSearchRow

    'Position on cell A3
    Application.CutCopyMode = False
    Range("A3").Select


End If

Exit Sub

Err_Execute:

End Sub

I like to copy and paste each row from column A to column H

1

1 Answers

0
votes

You need to change the range you are copying , so instead copying the full row you should just copy the columns you want

Could you try with this lines?


Sheets(sheetToSearch).Range("a" & LSearchRow, "h" & LSearchRow).Copy
'Paste row into Sheet2 in next row
Sheets(sheetTarget).Range("a" & LCopyToRow).PasteSpecial Paste:=xlPasteValues
Sheets(sheetTarget).Range("a" & LCopyToRow).PasteSpecial Paste:=xlFormats

To avoid overwriting "A3" when changing "ID" , could you try replacing the beguinning of the "sub" by this?


Sub matchandcopy()

Dim LCopyToRow As Integer
Dim sheetTarget As String: sheetTarget = "sheet1"
Dim sheetToSearch As String: sheetToSearch = "LIST2"
Dim targetValue As String: targetValue = Sheets(sheetTarget).Range("A1").Value  'Value in sheet2!A1 to be searched in sheet1
Dim columnToSearch As String: columnToSearch = "A"
Dim iniRowToSearch As Integer: iniRowToSearch = 2
Dim LSearchRow As Long 'As far as it is not clear the number of rows you will be considering, better relying on the long type
Dim maxRowToSearch As Long: maxRowToSearch = 2000 'There are lots of rows, so better setting a max. limit


LCopyToRow = Sheets(sheetTarget).Range("a1").End(xlDown).Row + 1
If LCopyToRow > 100000 Then LCopyToRow = 3

If (Not IsEmpty(targetValue)) Then 'here goes the rest of the sub with no changes  ....