Saltar al contenido
View in the app

A better way to browse. Learn more.

Ayuda Excel

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

¿Como omitir un error correctamente?

publicado

Estimados, muy buen dia.

Les comento que estoy trabajando a una macro, la cual consiste en buscar datos de otro libro de excel, mediante el comando ActiveWorkbook.Path.

Necesito que busque 5 archivos distintos, los cuales están nombrados con números del 1 al 5. Para hacer esto, copie 5 veces el código ActiveWorkbook. El problema es que si un día solo tengo 3 archivos, la macro arroja un error al no encontrar el cuarto archivo. 

Probe utilizando el comando On Error GoTo "primero" , y al final primero:

Funcionó con el primer codigo, pero el segundo utilice On Error GoTo "segundo, y no lo considera.

Sub TEST()
'
' TEST Macro


Dim wbDestino As Workbook, _
    wsOrigen As Excel.Worksheet, _
    wsDestino As Excel.Worksheet, _
    rngOrigen As Excel.Range, _
    rngDestino As Excel.Range
     
Sheets("DATA").Select
Range("A1").Select
    
On Error GoTo primero
Set wbDestino = Workbooks.Open(ActiveWorkbook.Path & "/EXCEL/1", Local:=True)
     Set wsDestino = wbDestino.Worksheets("1")
Set wsOrigen = Worksheets("1")
Const celdaOrigen = "A1"
Const celdaDestino = "A1"
Set rngOrigen = wsOrigen.Range(celdaOrigen)
Set rngDestino = wsDestino.Range(celdaDestino)
rngOrigen.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ThisWorkbook.Activate
    ActiveSheet.Paste
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    wbDestino.Close
    
primero:

'SEGUNDO ARCHIVO'

On Error GoTo segundo
Set wbDestino = Workbooks.Open(ActiveWorkbook.Path & "/EXCEL/2", Local:=True)
    Set wsDestino = wbDestino.Worksheets("2")
Set wsOrigen = Worksheets("2")
Set rngOrigen = wsOrigen.Range(celdaOrigen)
Set rngDestino = wsDestino.Range(celdaDestino)
rngOrigen.Select
ActiveCell.Offset(1, 0).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ThisWorkbook.Activate
    ActiveSheet.Paste
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    wbDestino.Close
        
segundo:

'TERCER ARCHIVO'

On Error GoTo tercero
Set wbDestino = Workbooks.Open(ActiveWorkbook.Path & "/EXCEL/3", Local:=True)
    Set wsDestino = wbDestino.Worksheets("3")
Set wsOrigen = Worksheets("3")
Set rngOrigen = wsOrigen.Range(celdaOrigen)
Set rngDestino = wsDestino.Range(celdaDestino)
rngOrigen.Select
ActiveCell.Offset(1, 0).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ThisWorkbook.Activate
    ActiveSheet.Paste
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    wbDestino.Close
    
tercero:

End Sub

 

Saludos!

 

 

 

Featured Replies

publicado

@manzano

 , solo tenías que cambiar lo siguiente... si es que lo que ponnes en tu código es lo que quieres...

For i = 1 To 5
Fichero = Dir(ThisWorkbook.Path & "\EXCEL\" & i & ".csv", vbArchive)
    If Fichero <> "" Then
        Set wbDestino = Workbooks.Open("ThisWorkbook.Path & "\EXCEL\" & Fichero)
publicado

Otra vez el "#$% foro dejándote a medias editando un post ?

Y digo si es lo que quieres, porque al ser un fichero de texto (csv), lo puedes importar en excel mediante:

Pestaña "Datos"-->Obtener Datos Externos-->Desde un Archivo

Una vez en el cuadro de diiálogo de importación, en tu caso debes elegir "Mis datos tienen encabezados" y "Separados por comas". Te dejo un resultado

 

 

 

Libro1.xlsx

Editado el por Haplox

Archivado

Este tema está ahora archivado y está cerrado a más respuestas.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.