Error (Subindice out of range) in Excel (Macros-Vda)

0

My problem is the following I try to make a button in which 1. Search Excel file 2. When selected, copy only the layers with data of the A range 3.se closing 3.y hit it in the current excel in which I have the macro

Greetings.

We are using this but I'm throwing an error (Subindice out of range) in which I can not solve it

Sub Prueba()

Dim ss As Workbook
Dim archivo As Workbook
Dim nombreArchivo As Variant

Set ss = ActiveWorkbook
nombreArchivo = Application.GetOpenFilename(FileFilter:="Excel Files,*.xl*;*.csv*")

If nombreArchivo = False Then
    Exit Sub
Else
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
End If

Set archivo = Workbooks.Open(nombreArchivo)

Dim AA As String
AA = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

archivo.Sheets("Hoja1").Range("A1:A" & AA).Copy: ss.Sheets("Hoja1").Range("A1").PasteSpecial xlPasteValues

Application.GoTo ss.Sheets("Hoja1").Range("A1")


 For Each celda In Range("A1:A" & AA)
      celda.Value = Replace(celda.Value, "|", ",")
   Next


archivo.Close
End Sub

If I work with this but when it is csv file. the same error comes out but if it is normal normal excel I get it (Subindice out of range) but at the same time it was to change the "|" a "," this does work for me I would also like to change the format of the csv that is general to text as I boot with commas and only works well with text format

    
asked by danny 25.07.2018 в 18:09
source

1 answer

0

Without trying it, I believe that the error you receive is because you close the Book that contains the data to be copied before changing to the source book.

I leave you a code that should work:

Sub Prueba()

Dim ss As Workbook
Dim archivo As Workbook
Dim nombreArchivo As Variant

Set ss = ActiveWorkbook
nombreArchivo = Application.GetOpenFilename(FileFilter:="Excel Files,*.xl*;*.xm*")

If nombreArchivo = False Then
    Exit Sub
Else
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
End If

Set archivo = Workbooks.Open(nombreArchivo)

Dim AA As String
AA = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

archivo.Sheets("Hoja1").Range("A1:A" & AA).Copy: ss.Sheets("Hoja1").Range("A1").PasteSpecial xlPasteValues

Application.GoTo ss.Sheets("Hoja1").Range("A1")
archivo.Close

End Sub
    
answered by 26.07.2018 в 00:07