Como solucionar error 1004 vba excel

Estoy desesperado porque me sale error 1004 junto a un mensaje que dice no se guardado el documento o puede que el documento este abierto o que se haya encontrado un error al guardar. Mi macro es para crear pdf de un formulario y es la siguiente:

sub PDF()

Application.ScreenUpdating = False

Dim primera, ultima, empleados As Integer
Dim rdato
Dim n As Range
Dim Mensaje, Estilo, Título, Ayuda, Ctxt, Respuesta, MiCadena
Dim Ruta As String
Dim NombreArchivo As String
Dim hoja As Object
Sheets("Tareo de Semana").Select
Range("B10").Select
Do While ActiveCell <> Empty
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Offset(-1, 0).Select
ActiveCell.Offset(0, -1).Select
empleados = ActiveCell.Value
Sheets("Boletas").Select
primera = Range("R13").Value
ultima = Range("S13").Value

If Range("R13").Value = Empty Then
MsgBox prompt:="Debe ingresar la primera boleta", Buttons:=vbokon1y + vbCritical, Title:="Llene campos vacíos"
Range("R13").Select
Exit Sub
End If
If Range("S13").Value = Empty Then
MsgBox prompt:="Debe ingresar la última boleta", Buttons:=vbokon1y + vbCritical, Title:="llene campo vacío"
Range("S13").Select
Exit Sub
End If
If primera > ultima Then
MsgBox prompt:="Primera de boleta mayor a la ultima boleta", Buttons:=vbokon1y + vbCritical, Title:="Inconsistencia"
Range("R13").Select
Exit Sub
End If
If primera > empleados Then
MsgBox prompt:="Primera boleta fuera de rango", Buttons:=vbokn1y + vbCritical, Title:="Inconsistencia = ERROR"
Range("R13").Select
Exit Sub
End If
If ultima > empleados Then
Range("S13").Value = empleados
MsgBox prompt:="Última boleta no puede ser mayor a " & empleados & " Empleados", Buttons:=vbokon1y + vbCritical, Title:="Inconsistencia = ERROR"
Range("S13").Select
ultima = empleados
Exit Sub
End If
If ultima < primera Then
MsgBox prompt:="Último número es menor al primer nùmero", Buttons:=vbokon1y + vbCritical, Title:="Inconsistencia"
Range("S13").Select
Exit Sub
End If

Mensaje = " ¿Desea GUARDAR Boletas desde la " & primera & " hasta la " & ultima & " ? "
Estilo = vbYesNo + vbQuestion + vbDefaultButton2 'Define los botones.
Título = "Imprime Boletas de Sueldos" 'Define los titulos.
Ayuda = "DEMO.HLP" 'Define los archivos de ayuda.
Ctxt = 1000 'Define el tema.
'el contexto.
'muestra el mensaje.
Respuesta = MsgBox(Mensaje, Estilo, Título, Ayuda, Ctxt)
If Respuesta = vbYes Then 'el usuario eligió el boton Si.
MiCadena = "Si" 'ejecuta una acción.
Else 'el usuario eligió el boton No.
MiCadena = "No" 'ejecuta una acción.
Exit Sub

End If

With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "/"
.Title = " SELECCIONAR CARPETA"
.Show
If .SelectedItems.Count = 0 Then
'Nada
Else

Ruta = .SelectedItems(1)
For i = primera To ultima
Range("P6").FormulaR1C1 = i
NombreArchivo = Range("P3").Value

MsgBox " GUARDANDO ARCHIVO PDF DE " & NombreArchivo, vbInformation, "RAÚL GUTIERREZ"

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Ruta & "/" & NombreArchivo & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next
Range("P6") = ultima
Range("R13").Select
End If

End With
End Sub

1 Respuesta

Respuesta

En principio leyendo tu código podría haber un error si la celda P3 se encuentra vacía o con algún error. Por esta instrucción:

NombreArchivo = Range("P3").Value

Si no es esto, presiona el botón Depurar y envíanos imagen de la línea de color donde se detiene el código.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas