如何使用宏根据excel文件中的条件将.pdf文件从一个文件夹传输到另一个文件夹?

如何使用宏根据excel文件中的条件将.pdf文件从一个文件夹传输到另一个文件夹?,excel,pdf,vba,Excel,Pdf,Vba,更详细地说: 我有一个excel文件,其中包含一堆零件号。我想制作一个宏,这样如果一列等于“是”,那么就在“Specs”文件夹中搜索规格。与excel工作表中的零件号匹配的pdf,然后将该文件夹中的.pdf复制到目标文件夹或“Dest”文件夹 更新的代码试图理解循环逻辑我正在寻找通过 Sub Rectangle1_Click() Dim ws As Worksheet Dim lRow As Long, i As Long Dim OldPath As String, NewPath As S

更详细地说:

我有一个excel文件,其中包含一堆零件号。我想制作一个宏,这样如果一列等于“是”,那么就在“Specs”文件夹中搜索规格。与excel工作表中的零件号匹配的pdf,然后将该文件夹中的.pdf复制到目标文件夹或“Dest”文件夹

更新的代码试图理解循环逻辑我正在寻找通过

Sub Rectangle1_Click()

Dim ws As Worksheet
Dim lRow As Long, i As Long
Dim OldPath As String, NewPath As String

Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")



'~~> File location
OldPath = "C:\Users\bucklej\Desktop\Spec\"
NewPath = "C:\Users\bucklej\Desktop\Dest\"

'~~> This is the workbook from where the code is running
 Set ws = ThisWorkbook.Sheets("Specification Listing")

'~~> Loop through Col A
    For i = 1 To 1000
       Cells(i, 2).Value = Yes
    Next i

fso.CopyFile(OldPath, Newpath)

 End Sub

无需使用
DIR
然后拆分文件名。这里有一个更快的方法

试试这个(经过尝试和测试)


也许我的措辞很糟糕。
是的,你说了:)请阅读你的问题。我准确地回答了你的问题:我要去健身房了。应该在1.5小时后回来。然后我将再次访问这个问题。我没有看到您在代码中循环搜索零件号?请参阅本节,了解如何循环搜索列中的单元格。只要将其与上面的代码结合起来,就可以很好地工作。试试看,如果你被卡住了,那么把你试过的代码贴出来,然后我们就从那里拿走……乔希,我可以把代码放在盘子里给你,但我不想这样做。这对你没有帮助。你所做的只是从那里复制粘贴代码。我要求您了解for循环是如何工作的。
Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long, i As Long
    Dim OldPath As String, NewPath As String

    OldPath = "C:\Users\bucklej\Desktop\Test1\"
    NewPath = "C:\Users\bucklej\Desktop\Test2\"

    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        For i = 1 To lRow

            '~~> If file doesn't exists then that line will be ignored
            On Error Resume Next
            LoopFile = .Range("A" & i).Value & ".txt"
            Name OldPath & LoopFile As NewPath & LoopFile
            LoopFile = .Range("A" & i).Value & ".docx"
            Name OldPath & LoopFile As NewPath & LoopFile
            LoopFile = .Range("A" & i).Value & ".xlsx"
            Name OldPath & LoopFile As NewPath & LoopFile
            LoopFile = .Range("A" & i).Value & ".xls"
            Name OldPath & LoopFile As NewPath & LoopFile
            LoopFile = .Range("A" & i).Value & ".bmp"
            Name OldPath & LoopFile As NewPath & LoopFile
            On Error GoTo 0

            DoEvents
        Next i
    End With
End Sub