如何遍历命名范围

时间:2019-06-26 10:58:07

标签: excel vba loops named-ranges

我有一些导入.bas文件的代码,我需要它通过2个命名范围运行。第一个(MacroName)将是文件名列表(例如,exe.bas,print.bas ...),第二个(ImportDate)将是文件导入的日期。

下面的代码循环通过包含.bas文件的文件夹。然后,它根据ImportDate命名范围检查文件的修改日期。但我想不出一种循环命名范围的方法

For Each objFile In objFSO.GetFolder(szImportPath).Files
    If objFile.Name Like "*Import*" Then GoTo skipImport ' Skips the Import Module
    If (objFSO.GetExtensionName(objFile.Name) = "cls") Or _
        (objFSO.GetExtensionName(objFile.Name) = "frm") Or _
        (objFSO.GetExtensionName(objFile.Name) = "bas") And _
        objFile.DateLastModified > Range("ImportDate") Then

        Dim CurrentModuleName

        CurrentModuleName = Left(objFile.Name, (InStrRev(objFile.Name, ".", -1, vbTextCompare) - 1))
        '^ Gets the file name of the module being imported and removes the extension

        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent

        Set VBProj = ActiveWorkbook.VBProject
        Set VBComp = VBProj.VBComponents(CurrentModuleName)
        VBProj.VBComponents.Remove VBComp
        '^ Removes the module that is to be replaced

        Range("MacroName") = objFile.Name
        Range("ImportDate") = Format(Date, "dd/mm/yyyy") & " " & Format(Time, "hh.nn.ss")
        '^ Keep time as without, it will import the same module throughout the day when opened.

        cmpComponents.Import objFile.path
    End If      
skipImport:
Next objFile

我希望它读取文件名,检查日期,然后如果导入文件的修改日期更大,则删除当前文件并替换。

1 个答案:

答案 0 :(得分:0)

Sub test()
Dim r As Range
For Each r In ThisWorkbook.Names("MacroName").RefersToRange
    Debug.Print r.Address
Next r

End Sub