Tengo 1macro que abre archivos .csv en excel, quiero copiarlos en la hoja 2 del libro de la macro

Lo que quiero hacer es:

Abrir varios .csv en archivos excel.
Copiar los datos de estos archivos que acabo de abrir y pegarlos en la hoja 2 del libro donde está la macro. Solo hay datos en la primera columna, y me gustaría que los pegara en la primera celda en blanco.

No tengo mucha idea de VBA. Con lo que he hecho consigo copiar todos los archivos abiertos en la hoja 2 excepto el último, no encuentro el error. ¿Podrías ayudarme?

Te mando el código y muchas gracias por adelantado.

Dim filenames As Variant
' set the array to a variable and the True is for multi-select
filenames = Application.GetOpenFilename(, , , , True)
counter = 1
' ubound determines how many items in the array
While counter <= UBound(filenames)
'Opens the selected files
Workbooks.Open filenames(counter)
' displays file name in a message box
MsgBox filenames(counter)
'Select the excel with the data
Application.Workbooks(counter).Activate
'Select cells with the data (the first column)
Application.Range("A1", Range("A1").End(xlDown)).Select
'copy selected range
Application.Selection.Copy
'Select the active book which is where I want to paste the data
Application.ThisWorkbook.Activate
'Go to sheet 2
Application.Sheets(2).Select
If IsEmpty(ActiveCell) Then
ActiveSheet.Paste
ElseIf IsEmpty(ActiveCell.Offset(1, 0)) Then
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Else
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
End If
'cerramos excel
Application.Workbooks(counter).Close
'increment counter
counter = counter + 1
Wend

Añade tu respuesta

Haz clic para o