无法将值存储在数组中-VBA [英] Cannot able to store value in a array - VBA

查看:121
本文介绍了无法将值存储在数组中-VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将文件名存储在数组中,但出现类型不匹配错误.我已经更改了数据类型,但是没有用.请帮助.

I am trying to store a filename in a array but i am getting Type mismatch error. I have changed the data type but it didn't work. Kindly help.

抛出错误的代码块,

Sub Example2()
Dim objFile,objFile1,objFolder,objFolder1 As Object
Dim splitting, counter, filename, filename1, splitting1, counter1,As Variant
Dim myarray() As Variant

For Each objFile In objFolder.Files

splitting = Split(objFile.Name, "\", 9)

counter = UBound(splitting)

filename = splitting(counter)

    For Each objFile1 In objFolder1.Files

    splitting1 = Split(objFile1.Name, "\", 9)

    counter1 = UBound(splitting1)

    filename1 = splitting1(counter1)
    
    If srch1 = srch2 Then
    
    ReDim Preserve myarray(UBound(myarray) + 1)
    
    myarray() = filename1

    End If
    
    Next

    Next 

推荐答案

获取文件路径(到数组)功能

链接

对象
FileSystemObject对象
GetFolder方法
文件对象

代码

Option Explicit

Function getFilePaths(ByVal FolderPath As String, _
                      Optional ByVal FirstIndex As Long = 1) _
         As Variant
    
    Dim fso As Object
    Dim fsoFldr As Object
    Dim fsoFile As Object
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fsoFldr = fso.GetFolder(FolderPath)
    
    Dim LastIndex As Long
    LastIndex = FirstIndex - 1
    Dim Data() As Variant
    
    For Each fsoFile In fsoFldr.Files
        LastIndex = LastIndex + 1
        ReDim Preserve Data(FirstIndex To LastIndex)
        Data(LastIndex) = fsoFile.Path ' or .Name, .ParentFolder ...
    Next fsoFile
    
    getFilePaths = Data

End Function

Sub TESTgetFilePath()
    
    ' Define Folder Path ('fPath').
    Const fPath As String = "F:\Test\2020"
    ' Populate File Paths Array ('Data').
    Dim Data As Variant
    Data = getFilePaths(fPath)
    ' Validate File Paths Array.
    If IsEmpty(Data) Then
        MsgBox "No files found.", vbCritical, "Fail"
        Exit Sub
    End If
    ' Write title to the Immediate window (CTRL+G).
    Debug.Print "The List"
    ' Write values from File Paths Array to a String ('Result').
    Dim Result As String
    Result = Join(Data, vbLf)
    ' Write file paths to the Immediate window (CTRL+G).
    Debug.Print Result

End Sub

Sub Example2()
    
    Const FolderPath As String = "C:\Test"

    Dim fso As Object
    Dim objFolder As Object
    Dim objFile As Object
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objFolder = fso.GetFolder(FolderPath)

    Dim LastIndex As Long
    LastIndex = -1
    Dim MyArray() As Variant
    
    For Each objFile In objFolder.Files
        LastIndex = LastIndex + 1
        ReDim Preserve MyArray(LastIndex)
        MyArray(LastIndex) = objFile.Name
    Next objFile
    
    Dim n As Long
    For n = LBound(MyArray) To UBound(MyArray)
        Debug.Print n, MyArray(n)
    Next n

End Sub

Sub Example3()
    
    ' For a fileformat aaa-bbb-rev*.*, where 'rev' is to be tested if greater.
    ' Two hyphens only.
    
    Const FolderPath As String = "F:\Test\2020\64568450"
    Const fSep As String = "-"
    Dim pSep As String
    pSep = Application.PathSeparator
     
    Dim fso As Object
    Dim objFolder As Object
    Dim objFile As Object
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objFolder = fso.GetFolder(FolderPath)
    
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")
   
    Dim FileParts As Variant ' An array containing the split file name.
    Dim fName As String      ' File part before the 2nd hyphen (minus) '-'
    Dim fRevision As String  ' File part after the 2nd hyphen (minus) '-'
    
    Dim LastIndex As Long
    LastIndex = -1
    Dim MyArray() As Variant
    
    ' Write file paths to array.
    For Each objFile In objFolder.Files
        FileParts = Split(objFile.Name, fSep)
        fName = FileParts(0) & fSep & FileParts(1)
        fRevision = FileParts(2)
        If Not dict.Exists(fName) Then
            dict(fName) = fRevision
        Else
            LastIndex = LastIndex + 1
            ReDim Preserve MyArray(LastIndex)
            If dict(fName) < fRevision Then
                MyArray(LastIndex) = FolderPath & pSep & fName _
                                                & fSep & fRevision
                dict(fName) = fRevision
            Else
                MyArray(LastIndex) = objFile.Path
            End If
        End If
    Next objFile
    
    ' Now 'MyArray' contains the list of file paths of the files to be moved.
    
    Dim n As Long
    For n = LBound(MyArray) To UBound(MyArray)
        Debug.Print n, MyArray(n)
    Next n

End Sub

这篇关于无法将值存储在数组中-VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆