How to merge these two codes to work in once code

Ramadan

Board Regular
Joined
Jan 20, 2024
Messages
114
Office Version
  1. 2021
Platform
  1. 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

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?
Untitled.png
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
You have the Option Explicit statement at the beginning of your code, that is why you must explicitly declare all variables

You just have to declare the variable in your variable declaration section, for example after this line:

VBA Code:
Dim myDate as string

Add this line:
VBA Code:
Dim i as Long


:giggle:
 
Last edited:
Upvote 0
Solution
You have the Option Explicit statement at the beginning of your code, that is why you must explicitly declare all variables

You just have to declare the variable in your variable declaration section, for example after this line:

VBA Code:
Dim myDate as string

Add this line:
VBA Code:
Dim i as Long


:giggle:
Wow the perfect simple answer as usual Mr. DanteAmor - it workded - I do appreciate your help you saved my time :)
 
Last edited by a moderator:
Upvote 0
@DanteAmor just one simple issue - I cannot link them to one comand button because they appear in macro list as 2 marcos not one - what should I do
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,226,510
Messages
6,191,456
Members
453,658
Latest member
healmo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top