Ramadan
Board Regular
- Joined
- Jan 20, 2024
- Messages
- 114
- Office Version
- 2021
- Platform
- Windows
I have two different codes and they are working perfectly individually but when I merge them toghter I get error in the second code which never appeared before when running this code separately and the error disappear when I delete the first code and run it individually
this first code is to delete all files in the folder and second one is to save new pdfs based on cycling drop down list
here are the tow codes together and screenshot from the error
Any suggestions please?
this first code is to delete all files in the folder and second one is to save new pdfs based on cycling drop down list
here are the tow codes together and screenshot from the error
VBA Code:
Option Explicit
Sub DeleteFiles()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim sourceFolder As Object
Set sourceFolder = fso.GetFolder("D:\Desktop\Docs\") 'change the path accordingly
Dim currentFile As Object
For Each currentFile In sourceFolder.Files
Select Case fso.GetExtensionName(currentFile)
Case "pdf", "csv", "xlsx", "xlsm" 'add and/or change the file extensions as desired
fso.DeleteFile currentFile.Path
End Select
Next currentFile
Set currentFile = Nothing
Set sourceFolder = Nothing
Set fso = Nothing
End Sub
Sub myFiles()
Dim wb As Workbook
Dim ws As Worksheet
Dim nwb As Workbook
Dim nws As Worksheet
Dim rng As Range
Dim Path As String
Dim myDate As String
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Print")
Set rng = ws.Range("B2")
Path = "D:\Desktop\Docs\"
myDate = Format(Now(), "DD-MM-YYYY")
For i = 1 To 5
rng = ws.Range("A" & i)
ws.Copy
Set nwb = ActiveWorkbook
Set nws = nwb.Worksheets("Print")
With nws
Cells.Copy
Cells.PasteSpecial (xlPasteValues)
End With
Application.DisplayAlerts = False
nwb.ExportAsFixedFormat Type:=xlTypePDF, filename:=Path & ws.Range("c2") & " " & myDate
nwb.Close
Application.DisplayAlerts = True
Next i
End Sub
Any suggestions please?