Vba 从单行到列表(循环)

Vba 从单行到列表(循环),vba,excel,Vba,Excel,我正在尝试重新配置VBA代码,以便它通过列表而不是列表中的一行 目前,代码的工作方式完全符合我的要求,但我很难找出循环,以便它为我处理整个列表 你有什么建议吗?代码如下 编辑: Sub sbCopyingAFileReadFromSheet() 'Declaration Dim FSO Dim sFile As String Dim sSFolder As String Dim sDFolder As String Dim sFilenew As String 'This is Your

我正在尝试重新配置VBA代码,以便它通过列表而不是列表中的一行

目前,代码的工作方式完全符合我的要求,但我很难找出循环,以便它为我处理整个列表

你有什么建议吗?代码如下

编辑:

Sub sbCopyingAFileReadFromSheet()

'Declaration
Dim FSO
Dim sFile As String
Dim sSFolder As String
Dim sDFolder As String
Dim sFilenew As String


'This is Your File Name which you want to Copy.You can change File name at B5.
sFile = Sheets("Main").Range("F5")

'Change to match the source folder path. You can change Source Folder name at B6.
sSFolder = Sheets("Main").Range("B5")

'Change to match the destination folder path. You can change Destination Folder name at B6.
sDFolder = Sheets("Main").Range("C5")

'Change name to new file name.
sFilenew = Sheets("Main").Range("D5")

'Create Object for File System
Set FSO = CreateObject("Scripting.FileSystemObject")

'Checking If File Is Located in the Source Folder
If Not FSO.FileExists(sSFolder & sFile) Then
    MsgBox "Specified File Not Found in Source Folder Error 2", vbInformation, "Not Found"

'Copying If the Same File is Not Located in the Destination Folder
ElseIf Not FSO.FileExists(sDFolder & sFile) Then
    FSO.CopyFile (sSFolder & sFile), (sDFolder & sFilenew), True
    MsgBox "Specified File Copied to Destination Folder Successfully", vbInformation, "Done!"
Else
    MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"
End If

End Sub
尝试:


使用Do直到I空循环,易于理解

    Set FSO = CreateObject("Scripting.FileSystemObject")

    iRow = 5 ' start from row 5
    With Worksheets("Sheet1") '<-- update sheet name

        Do Until IsEmpty(.Cells(iRow, 6)) ' (Row, Column)
            sFile = .Cells(iRow, 6).Value
            sSFolder = .Cells(iRow, 2).Value
            sDFolder = .Cells(iRow, 3).Value
            sFilenew = .Cells(iRow, 4).Value

            'Checking If File Is Located in the Source Folder
            If Not FSO.FileExists(sSFolder & sFile) Then
                Debug.Print "Specified File Not Found in Source Folder Error 2", vbInformation, "Not Found" 'Print on Immediate
'                MsgBox "Specified File Not Found in Source Folder Error 2", vbInformation, "Not Found"

            'Copying If the Same File is Not Located in the Destination Folder
            ElseIf Not FSO.FileExists(sDFolder & sFile) Then
                FSO.CopyFile (sSFolder & sFile), (sDFolder & sFilenew), True
'                MsgBox "Specified File Copied to Destination Folder Successfully", vbInformation, "Done!"
                Debug.Print "Specified File Copied to Destination Folder Successfully", vbInformation, "Done!"  'Print on Immediate
            Else
'                MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"
                Debug.Print "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"  'Print on Immediate
            End If

            iRow = iRow + 1
        Loop
    End With
Set FSO=CreateObject(“Scripting.FileSystemObject”)
iRow=5'从第5行开始

拿着工作表(“Sheet1”)“认真地拍一张照片,你为什么要对我们施加更大的压力来帮助你?”out@0m3r很好,对新手的错误表示抱歉。我已经用实际的文本代码更新了它。这更合适吗?你想做什么需要一个循环?如果您正在讨论整个子例程,请将所有代码放在While/Wend循环中,条件可能为:While sFile“”,并将sFile设置为循环外的某个值,以便它执行一次。无关:您键入的每种类型
sFile=Sheets(“Main”).Range(“F5”)
都使用了一个隐含的
.Value
属性。为了明确指定值而不是范围对象,请尝试将其包括在内,如
sFile=Sheets(“Main”).range(“F5”).value
。最好使用处理日期更好的
.Value2
。最好设置sFile=“x”,然后使用While sFile”“(在此处插入代码)Wend。每次您决定需要不同的迭代次数时,使用For循环都需要进行更改。谢谢@PKatona,如果需要,我们只需添加
if sFile=“”,然后退出For
Hey@0m3r-我已经运行了该代码,但由于这些错误,我无法让它工作。我知道它们是基本的错误,但到目前为止,我的阅读并没有帮助我调试它们。我得到了一个预期的结束子文件,因为我在顶部放了一个子文件sbCopyingAFIle(),这样我就可以命名函数了。它说编译错误“预期的结束子文件,然后突出显示第一行,其中有子文件sbCopyingAFIle()。再次抱歉,我想答案是相当基本的。0m3r-非常感谢您的帮助。它工作得非常完美!
    Set FSO = CreateObject("Scripting.FileSystemObject")

    iRow = 5 ' start from row 5
    With Worksheets("Sheet1") '<-- update sheet name

        Do Until IsEmpty(.Cells(iRow, 6)) ' (Row, Column)
            sFile = .Cells(iRow, 6).Value
            sSFolder = .Cells(iRow, 2).Value
            sDFolder = .Cells(iRow, 3).Value
            sFilenew = .Cells(iRow, 4).Value

            'Checking If File Is Located in the Source Folder
            If Not FSO.FileExists(sSFolder & sFile) Then
                Debug.Print "Specified File Not Found in Source Folder Error 2", vbInformation, "Not Found" 'Print on Immediate
'                MsgBox "Specified File Not Found in Source Folder Error 2", vbInformation, "Not Found"

            'Copying If the Same File is Not Located in the Destination Folder
            ElseIf Not FSO.FileExists(sDFolder & sFile) Then
                FSO.CopyFile (sSFolder & sFile), (sDFolder & sFilenew), True
'                MsgBox "Specified File Copied to Destination Folder Successfully", vbInformation, "Done!"
                Debug.Print "Specified File Copied to Destination Folder Successfully", vbInformation, "Done!"  'Print on Immediate
            Else
'                MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"
                Debug.Print "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"  'Print on Immediate
            End If

            iRow = iRow + 1
        Loop
    End With