My name is Pedro and I am very beginner in VBA development. I have a question about copy and paste with criteria. I have code that reports to me an error 1004
workbooks open when I run a macro that copy and paste row in another workbooks. How can I fix this error with the following code?
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
End If
Next i
Application.ScreenUpdating = True '
Application.Calculation = xlCalculationAutomatic
MsgBox "Informações inseridas com sucesso", vbInformation
End sub
I also want to explain what my code do it. My code matches a specific condition in "I" column (like Aline, Carol, Karine, Lucas, Thiago) and after that it copies each row and pastes it in another workbook, according to the matched conditions in "I" column. So what do you think would be the problem in this situation? What can i do to fix the error? Or make a new code that copy a row with condition and paste it in another workbooks?
As for your posted code, you've commented your IF block but not the entire thing. The END IF should also be commented.
I've done similar things in the past, so let me offer some code snippets as advice. This should get you started. This is by no means the right or perfect way to do things, but it is simple to understand. After years and years of writing VB, I wouldn't write it this way. :)
Sub Open_SlaveWB()
vFile = Workbooks.Open Filename:="L:\Controle\Assessoria Tecnica\Pessoas\Aline.xlsx"
If TypeName(vFile) = "Boolean" Then Exit Sub
Set UpdateSheet = wbMaster.Sheets("Update") 'Name of the sheet in the master WB to copy into
Set wbSlave = Workbooks.Open(vFile)
Set SlaveSheet = wbSlave.Sheets("Plan1") 'Name of the sheet in the slave WB to copy from
Exit Sub
errMessage:
MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
Resume Next
End Sub
If you're absolutely sure the path and file name will go unchanged use this, otherwise I would advise setting vfile to: Application.GetOpenFilename("Excel-files,*.xlsx", 1, "Select One File To Open", , False)
which will open a file select dialog box.
I've found it more efficient to copy the sheet, to your master workbook, and get the data from there rather than subsequent slave workbooks.
Sub Copy_SlaveToMaster()
LastSlaveRow = SlaveSheet.UsedRange.Rows.Count
LastSlaveColumn = SlaveSheet.UsedRange.Columns.Count
SlaveSheet.Range(Cells(1, 1), Cells(LastSlaveRow, LastSlaveColumn)).Copy
UpdateSheet.Cells(1, 1).PasteSpecial
Exit Sub
errMessage:
MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
Resume Next
End Sub
You obviously don't need the slave WB to stay open.
Sub Close_SlaveWB()
Application.DisplayAlerts = False
wbSlave.Close
Application.DisplayAlerts = True
Exit Sub
errMessage:
MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
Resume Next
End Sub
From there you would just write some code to manipulate the new sheet, use the data from the new sheet as you wish.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.