Copy row if cell contains specific word

asaf27064

New Member
Joined
Jul 25, 2017
Messages
31
Heĺlo
I have 2 sheets with one table in each of them. The sheets names is "Work" and "Bank".
In each table I want to copy the whole row in the table if cell in column B contain the word "Exist" and move it to a sheet in a new excel file (workbook) under the name of the relevant sheet.
 
Please place below code to output file. First in Workbook open event and second to module.

Code:
Private Sub Workbook_Open()
CopyRow
End Sub

Sub CopyRow()
Dim arr() As Variant
Dim lRow As Long, sht As Worksheet, wb As Workbook, eRow As Long, inp As Workbook
Dim fPath As String
arr = Array("Work", "Bank", "Total")
Set wb = ThisWorkbook
fPath = ThisWorkbook.Path & "\Input.xlsx"
Set inp = Workbooks.Open(fPath, False)
For j = LBound(arr) To UBound(arr)
    Set sht = inp.Sheets(arr(j))
    lRow = sht.Range("A" & Rows.Count).End(xlUp).Row
    eRow = wb.Sheets(arr(j)).Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row
    For i = 1 To lRow
        If sht.Range("B" & i) = "Exist" Then sht.Range("B" & i).EntireRow.Copy wb.Sheets(arr(j)).Range("A" & eRow)
        lRow = sht.Range("A" & Rows.Count).End(xlUp).Row
        eRow = wb.Sheets(arr(j)).Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row
    Next i
Next j
End Sub
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Please place below code to output file. First in Workbook open event and second to module.

Code:
Private Sub Workbook_Open()
CopyRow
End Sub

Sub CopyRow()
Dim arr() As Variant
Dim lRow As Long, sht As Worksheet, wb As Workbook, eRow As Long, inp As Workbook
Dim fPath As String
arr = Array("Work", "Bank", "Total")
Set wb = ThisWorkbook
fPath = ThisWorkbook.Path & "\Input.xlsx"
Set inp = Workbooks.Open(fPath, False)
For j = LBound(arr) To UBound(arr)
    Set sht = inp.Sheets(arr(j))
    lRow = sht.Range("A" & Rows.Count).End(xlUp).Row
    eRow = wb.Sheets(arr(j)).Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row
    For i = 1 To lRow
        If sht.Range("B" & i) = "Exist" Then sht.Range("B" & i).EntireRow.Copy wb.Sheets(arr(j)).Range("A" & eRow)
        lRow = sht.Range("A" & Rows.Count).End(xlUp).Row
        eRow = wb.Sheets(arr(j)).Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row
    Next i
Next j
End Sub
-Thank you very much buddy but it gives me "Runtime error 9 subscript out of range" What can i do?
 
Upvote 0
Ensure to have Work, Bank,Total sheets created in input & output file. Also, please confirm on which line runtime error occurs.
 
Upvote 0
Ensure to have Work, Bank,Total sheets created in input & output file. Also, please confirm on which line runtime error occurs.

now its working but not automatic and the problem is that it doesnt copy the columns and create tables from the input file as well
and every time i launch the macro it add the same data below the existing data
 
Upvote 0
Its working but not updating automatically and every time i run it,its add more rows with the same data and not updating the data on the output file
all i want is that when i open the output file it will auto update with all the rows the contains exist in column b but i want it to copy it with the columns from the input file meaning it will create tables with values.
 
Upvote 0
Double click thisworkbook and add it to that module. So that it will auto update.
Code:
Private Sub Workbook_Open()
CopyRow
End Sub
 
Upvote 0

Hi.im really sorry to bother you again but i would like to get help with this please
thank you very much!
 
Upvote 0

Forum statistics

Threads
1,215,135
Messages
6,123,238
Members
449,093
Latest member
Vincent Khandagale

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top