Hi!
Completely new to vba, I’m trying to write a code to copy rows from one workbook to another(s).
The code clears the worksheet of the destination workbook and then copies the rows that began with the desired country.
The two ranged variables Country and cell are country names.
The code clears and paste the correct rows to the destination workbook but when a new country (cell) appears it gives the error 13.
The code is:
Sub Air_emissions()
Set MyRange = Workbooks("4 Ag air emissions.xlsm").Sheets("Sheet1").Range("A1:A1937")
For Each Country In Sheets("Countries").Range("B1:B30")
Workbooks(Country & ".xlsm").Sheets("air_emiss").Cells.Clear
Workbooks("4 Ag air emissions.xlsm").Sheets("Sheet1").Activate
For Each cell In MyRange
If cell.Value2 = "geo" Or cell.Value2 = Country.Value2 Then
cell.EntireRow.Copy
Workbooks(Country & ".xlsm").Sheets("air_emiss").Activate
Range("A" & Rows.Count).End(xlUp).Offset(1, 0).EntireRow.PasteSpecial xlPasteValues
End If
Next
Next Country
End Sub
Could you please help?
Thanks in advance for your attention,
Best regards,
Ricardo
Completely new to vba, I’m trying to write a code to copy rows from one workbook to another(s).
The code clears the worksheet of the destination workbook and then copies the rows that began with the desired country.
The two ranged variables Country and cell are country names.
The code clears and paste the correct rows to the destination workbook but when a new country (cell) appears it gives the error 13.
The code is:
Sub Air_emissions()
Set MyRange = Workbooks("4 Ag air emissions.xlsm").Sheets("Sheet1").Range("A1:A1937")
For Each Country In Sheets("Countries").Range("B1:B30")
Workbooks(Country & ".xlsm").Sheets("air_emiss").Cells.Clear
Workbooks("4 Ag air emissions.xlsm").Sheets("Sheet1").Activate
For Each cell In MyRange
If cell.Value2 = "geo" Or cell.Value2 = Country.Value2 Then
cell.EntireRow.Copy
Workbooks(Country & ".xlsm").Sheets("air_emiss").Activate
Range("A" & Rows.Count).End(xlUp).Offset(1, 0).EntireRow.PasteSpecial xlPasteValues
End If
Next
Next Country
End Sub
Could you please help?
Thanks in advance for your attention,
Best regards,
Ricardo