0

我在这里努力做一些基本的事情......

使用 Excel VBA:需要从一个范围(一维)创建一个数组,但该过程需要删除任何空白。

我的代码不起作用...

Sub ReadFilePaths()

Dim b As Long 'counter
Dim rPATHS As Range 'selected range containing file paths
Dim aTEMP As Variant 'initial array to be cleaned of blanks
Dim aFILEPATHS As Variant 'final array containing File Paths of those to be ReFlagged

Sheets("FILES").Select 'select ws where they are listed
Range("B3:B33").Select 'select range of them (30 files max)
Set rPATHS = Selection 'sets range rPATHS to the selection

aTEMP = rPATHS.Value 'Temp Array set to values in list

For b = LBound(aTEMP) To UBound(aTEMP)
        If aTEMP(b) = "" Then
        End If
     aFILEPATHS = aTEMP(b)
Next b
End Sub

我今天有这样的日子!非常感谢任何帮助。

数据输入将是

Element
C:\Test\myfile1.txt
C:\Test\myfile2.txt

E:\Folder1\Folder2\hisfile1.txt

F:\FolderA\herfile2.txt
C:\FolderC\zfileAV.txt

在数组中输出

C:\Test\myfile1.txt
C:\Test\myfile2.txt
E:\Folder1\Folder2\hisfile1.txt
F:\FolderA\herfile2.txt
C:\FolderC\zfileAV.txt
4

2 回答 2

2

在我看来,您无法使用范围创建一维数组。

如果您使用范围来评估数组,您将创建二维数组 - 在您的示例aTEMP(1 to 31, 1 to 1)中。尝试以下代码并稍作修正:

Sub ReadFilePaths()

Dim b As Long 'counter
Dim rPATHS As Range 'selected range containing file paths
Dim aTEMP() As Variant 'initial array to be cleaned of blanks
Dim aFILEPATHS() As Variant 'final array containing File Paths of those to be ReFlagged
Dim i As Long
Sheets("FILES").Select 'select ws where they are listed
Range("B3:B33").Select 'select range of them (30 files max)
Set rPATHS = Selection 'sets range rPATHS to the selection

aTEMP = rPATHS.Value 'Temp Array set to values in list

For b = LBound(aTEMP) To UBound(aTEMP)
        If aTEMP(b, 1) <> vbNullString Then
        ReDim Preserve aFILEPATHS(i)
        aFILEPATHS(i) = aTEMP(b, 1)
        i = i + 1
        End If
Next b

End Sub
于 2015-04-24T13:20:16.637 回答
0

您正在测试它们是否为空白,但什么也不做,试试这个。

Sub ReadFilePaths()
    Dim b As Long               'counter
    Dim rPATHS As Range         'selected range containing file paths
    Dim aTEMP As Variant        'initial array to be cleaned of blanks
    Dim aFILEPATHS As Variant   'final array containing File Paths of those to be ReFlagged
    Dim tmpStr As String        'Temporary String

    Sheets("FILES").Select      'select ws where they are listed
    Range("B3:B33").Select      'select range of them (30 files max)
    Set rPATHS = Selection      'sets range rPATHS to the selection

    aTEMP = rPATHS.Value        'Temp Array set to values in list

    For b = LBound(aTEMP) To UBound(aTEMP)
        If Len(aTEMP(b) & vbNullString) <> 0 Then
            tmpStr = tmpStr & aTEMP(b) & "#"
        End If
    Next b

    aFILEPATHS = Split(tmpStr, "#")
End Sub
于 2015-04-24T13:13:05.443 回答