从一个范围创建一个数组,同时忽略空白
Create an array from a range while ignoring blanks
我正在为一些基本的事情苦苦挣扎...
使用ExcelVBA:
需要从一个范围(一维)创建一个数组,但该过程需要删除任何空白。
我的代码不工作...
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
您正在测试它们是否 空白,但什么都不做,试试这个。
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
在我看来,您无法使用范围创建一维数组。
如果您使用范围来分配数组,您将创建二维数组 - 在您的示例中 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
我正在为一些基本的事情苦苦挣扎...
使用ExcelVBA: 需要从一个范围(一维)创建一个数组,但该过程需要删除任何空白。
我的代码不工作...
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
您正在测试它们是否 空白,但什么都不做,试试这个。
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
在我看来,您无法使用范围创建一维数组。
如果您使用范围来分配数组,您将创建二维数组 - 在您的示例中 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