Excel - 使用Range属性选择excel中的绝对范围

时间:2017-10-02 21:14:02

标签: excel-vba vba excel

我正在将文件(单元格列表)从源文件复制到目标文件夹。我需要以($ A1)的形式从excel中的列中选择所有值。

我的代码:

Sub SourcetoDestination()

    Dim rngFile As Range, cel As Range        
    Dim desPath As String, filename As String

    Set rngFile = ThisWorkbook.Sheets("Sheet1").**Range("$A1")** 'assuming file list in ColA

    desPath = "C:Destination\" 

    For Each cel In rngFile

        If Dir(cel) <> "" Then
            filename = Dir(cel) 

            FileCopy cel, desPath & filename 'copy to folder

        End If

    Next

End Sub

问题:

我的代码只复制列表中的第一个值。 如果我指定这样的范围:

ThisWorkbook.Sheets("Sheet1").Range("A1","A3")

它有效,但我想在excel中保存绝对范围。

有人可以指导我。谢谢。

1 个答案:

答案 0 :(得分:1)

这是你需要的吗?:

Sub SourcetoDestination()
    Dim rngFile As Range, cel As Range
    Dim desPath As String, filename As String
    Dim N As Long

    N = Cells(Rows.Count, "A").End(xlUp).Row
    Set rngFile = ThisWorkbook.Sheets("Sheet1").Range("$A1:$A" & N) 'assuming file list in ColA
    desPath = "C:Destination\"
    For Each cel In rngFile
        If Dir(cel) <> "" Then filename = Dir(cel)
            FileCopy cel, desPath & filename 'copy to folder
        End If
    Next
End Sub

<强> UNTESTED