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.
 
Add below code to your input file where sheets namely work bank Total exists. You should not get subscript out of range error if all sheets are there. In order to auto update the changes, you would need fix reference of the file. Here, we create new file containing "Exist" rows.
Code:
Sub CopyRow()
Dim arr() As Variant
Dim lRow As Long, sht As Worksheet, wb As Workbook, eRow As Long




arr = Array("Work", "Bank", "Total")
Set wb = Workbooks.Add
wb.Sheets(1).Name = "Work"


If wb.Sheets.Count = 1 Then
    wb.Sheets.Add
    wb.Sheets.Add
ElseIf wb.Sheets.Count = 2 Then
    wb.Sheets.Add
End If
 
wb.Sheets(2).Name = "Bank"
wb.Sheets(3).Name = "Total"
    


For j = LBound(arr) To UBound(arr)
    Set sht = ThisWorkbook.Sheets(arr(j))
    lRow = sht.Range("A" & Rows.Count).End(xlUp).Row
    eRow = wb.Sheets(1).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

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Add below code to your input file where sheets namely work bank Total exists. You should not get subscript out of range error if all sheets are there. In order to auto update the changes, you would need fix reference of the file. Here, we create new file containing "Exist" rows.
Code:
Sub CopyRow()
Dim arr() As Variant
Dim lRow As Long, sht As Worksheet, wb As Workbook, eRow As Long




arr = Array("Work", "Bank", "Total")
Set wb = Workbooks.Add
wb.Sheets(1).Name = "Work"


If wb.Sheets.Count = 1 Then
    wb.Sheets.Add
    wb.Sheets.Add
ElseIf wb.Sheets.Count = 2 Then
    wb.Sheets.Add
End If
 
wb.Sheets(2).Name = "Bank"
wb.Sheets(3).Name = "Total"
    


For j = LBound(arr) To UBound(arr)
    Set sht = ThisWorkbook.Sheets(arr(j))
    lRow = sht.Range("A" & Rows.Count).End(xlUp).Row
    eRow = wb.Sheets(1).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

Hi
Thank you very much for your help but its still showing error 9
here my file:
http://www.mediafire.com/file/s12azbgqv98vt1q/new.xlsm
 
Upvote 0
Try this mod to NMa91's code
Code:
Sub CopyRow()
Dim arr() As Variant
Dim lRow As Long, sht As Worksheet, wb As Workbook, eRow As Long

arr = Array("Work", "Bank", "Total")
Set wb = Workbooks.Add

If wb.Sheets.Count = 1 Then
    wb.Sheets.Add
    wb.Sheets.Add
ElseIf wb.Sheets.Count = 2 Then
    wb.Sheets.Add
End If

wb.Sheets(1).Name = "Work"
wb.Sheets(2).Name = "Bank"
wb.Sheets(3).Name = "Total"

For j = LBound(arr) To UBound(arr)
    Set sht = ThisWorkbook.Sheets(arr(j))
    lRow = sht.Range("A" & Rows.Count).End(xlUp).Row
    eRow = wb.Sheets(1).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
Try this mod to NMa91's code
Code:
Sub CopyRow()
Dim arr() As Variant
Dim lRow As Long, sht As Worksheet, wb As Workbook, eRow As Long

arr = Array("Work", "Bank", "Total")
Set wb = Workbooks.Add

If wb.Sheets.Count = 1 Then
    wb.Sheets.Add
    wb.Sheets.Add
ElseIf wb.Sheets.Count = 2 Then
    wb.Sheets.Add
End If

wb.Sheets(1).Name = "Work"
wb.Sheets(2).Name = "Bank"
wb.Sheets(3).Name = "Total"

For j = LBound(arr) To UBound(arr)
    Set sht = ThisWorkbook.Sheets(arr(j))
    lRow = sht.Range("A" & Rows.Count).End(xlUp).Row
    eRow = wb.Sheets(1).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 its working but i want the output files to create tables with the same coulmns names
and to be automatically updated every time i open it
like in this file:
http://www.mediafire.com/file/pg5znqmbpj7ogfu/output.xlsx

it is possible to put the vba code on the output file and then it will take the value from the original file?

Thank you very much!
 
Last edited:
Upvote 0
I suggest that instead of creating new file with every code run, let's save output file once and all with above code. Once we run the code, it would open input file from the same drive, pick exist row data and close input file. Whenever you open output file, it can update the data as required.
 
Upvote 0
I suggest that instead of creating new file with every code run, let's save output file once and all with above code. Once we run the code, it would open input file from the same drive, pick exist row data and close input file. Whenever you open output file, it can update the data as required.

So I just copy the code to the output file and save it then every time I open the file it will update automatically?
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,241
Members
449,075
Latest member
staticfluids

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