hello every one i am new to VBA the attach file vba code which works fine but i want to modify it for other workbook it search on only one sheet and only the first occurrence ( i want it to search 8 sheets in the same workbook) it paste the data over the old searched data ( it should find the last row and paste the new data) any kind of help is appreciated thanks in advance
VBA Code:
Sub test()
Dim sheetPaste As Worksheet
Dim sheetTarget As Worksheet
Dim sheetToSearch As Worksheet
Dim x As String
Dim columnValue As String: columnValue = "A"
Dim rowValue As Integer: rowValue = 1
Dim LTargetRow As Long
Dim maxRowToTarget As Long: maxRowToTarget = 1000
Dim columnToSearch As String: columnToSearch = "V"
Dim iniRowToSearch As Integer: iniRowToSearch = 1
Dim LSearchRow As Long
Dim maxRowToSearch As Long: maxRowToSearch = 1000
LCopyToRow = 1
Set sheetPaste = ThisWorkbook.Worksheets("sheetPaste")
Set sheetTarget = ThisWorkbook.Worksheets("sheetTarget")
Set sheetToSearch = ThisWorkbook.Worksheets("1")
'MsgBox sheetTarget.Cells(Rows.Count, 20).End(xlUp).Row
'finds the last row with a value in it in column T of sheetTarget
For LTargetRow = rowValue To sheetTarget.Cells(Rows.Count, 20).End(xlUp).Row
'targetCell = columValue & CStr(LTargetRow)
'must set x = , not the value in the column = to x (which is not initialize to it is "")
If sheetTarget.Range(columnValue & CStr(LTargetRow)).Text <> "" Then
x = sheetTarget.Range(columnValue & CStr(LTargetRow)).Text
'finds the last row with a value in it in column A of sheetToSearch
For LSearchRow = iniRowToSearch To sheetToSearch.Cells(Rows.Count, 1).End(xlUp).Row
If sheetToSearch.Range(columnToSearch & CStr(LSearchRow)).Value = x Then
sheetToSearch.Rows(LSearchRow).Copy
sheetPaste.Rows(LCopyToRow).PasteSpecial Paste:=xlPasteValues
LCopyToRow = LCopyToRow + 1
Exit For
End If
'dont need this anymore now that we know that last row with data in it.
' If (LSearchRow >= maxRowToSearch) Then
' Exit For
' End If
Next LSearchRow
End If
'If (LTargetRow >= maxRowToTarget) Then
' Exit For
'End If
Next LTargetRow
'Sheets("data").Cells(Rows.Count, 1).End(xlUp).Row
End Sub
Last edited by a moderator: