VBA Please help me fix and improve the code.

punnipah

Board Regular
Joined
Nov 3, 2021
Messages
134
Office Version
  1. 2019
Platform
  1. Windows
Hi

I want to edit 3 places of code.
1.Set ws = Workbooks.Open(reportFolder & "Report_for_SC.xlsx").Sheets(1) : I don't want to specify names. Report_for_SC.xlsx Because sometimes it won't be this name, but it will have the word Report every time.
2.pdfFileName = pdfFolder & "Inv SC 12.2023\" & reportValue & "_2023.pdf" : I don't want to specify the pdfFolder names "Inv SC 12.2023 and "2023.pdf", but it always contains "Inv" and .PDF.
3.newPdfFileName = pdfFolder & "Inv SC 12.2023\" & reportValue & "_" & copyValue & ".pdf" : I don't want to specify the newPdfFileName names "Inv SC 12.2023" but it always contains "Inv" and .PDF.

VBA Code:
Sub CompareFilesAndCopy()
    Dim ws As Worksheet
    Dim rng As Range
    Dim cell As Range
    Dim reportFolder As String
    Dim pdfFolder As String
    Dim reportValue As String
    Dim pdfFileName As String
    Dim copyValue As Variant
    Dim newPdfFileName As String
    Dim existingPdfFileName As String
 
    Dim fileNamePattern As String
    Dim file As String
  
  

    reportFolder = "D:\FBB Edit FileName\Macro\Input\"
  
    fileNamePattern = "Report"
 
    file = Dir(reportFolder & fileNamePattern)

    Do While file <> ""

        'MsgBox "Found file: " & reportFolder & file
      
 
        file = Dir
    Loop
  

    pdfFolder = "D:\FBB Edit FileName\Macro\Output\"
  

    Set ws = Workbooks.Open(reportFolder & "Report_for_SC.xlsx").Sheets(1)

    Set rng = ws.Range("H2:H" & ws.Cells(ws.Rows.Count, "H").End(xlUp).row)
  

    For Each cell In rng

        reportValue = cell.Value
      

        pdfFileName = pdfFolder & "Inv SC 12.2023\" & reportValue & "_2023.pdf"
      
   
        If Dir(pdfFileName) <> "" Then
   
            copyValue = ws.Range("C" & cell.row).Value
          
    
            newPdfFileName = pdfFolder & "Inv SC 12.2023\" & reportValue & "_" & copyValue & ".pdf"
          
    
            FileCopy pdfFileName, newPdfFileName
 
            existingPdfFileName = pdfFolder & "Inv SC 12.2023\" & reportValue & "_2023.pdf"
            If Dir(existingPdfFileName) <> "" Then
                Kill existingPdfFileName
            End If
          

            Dim copyPdfFileName As String
            copyPdfFileName = pdfFolder & "Inv SC 12.2023\" & reportValue & "_2023(copy).pdf"
            If Dir(copyPdfFileName) <> "" Then
                Kill copyPdfFileName
            End If
        Else
 
 
            'MsgBox "àÅ¢·Õè " & reportValue & " äÁ辺ã¹ä¿Åì PDF", vbInformation
        End If
    Next cell
  

    ws.Parent.Close False
End Sub
 
Last edited by a moderator:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy, and your thread more likely to attract helpers. My signature block below has more details. I have added the tags for you this time. 😊
 
Upvote 0
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy, and your thread more likely to attract helpers. My signature block below has more details. I have added the tags for you this time. 😊
Please help me
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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