从一个范围创build一个数组,同时忽略空白

我正在努力在这个基本的东西…

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

我的代码不工作…

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 

我今天有一天! 任何帮助不胜感激。

数据input将是

 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 

在我看来,你不能使用范围创build1维数组。

如果您使用范围来分配数组,您可以创build2维数组 – 例如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 

你正在testing,如果他们是空白 ,但无所事事,试试这个。

 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