我正在努力学习使用VBA为Excel创建的程序。该程序的最终目的是基于用户通过用户表单列表框选择其中一个元素,将文本文件的特定元素读入Excel电子表格。请参考下面的代码,该代码与我的程序部分有关,1)使用作业集编号填充列表框,2)从非常大的逗号分隔的文本文件中填充包含所需数据的特定数组:
Private Sub UserForm_Initialize()
'Declare variables
Const CMMData As String = "\\ATSTORE01\CMMData\21064D\21064D-OP400.dat"
Dim strSN() As String
Dim strSet() As String
Dim strFF() As String
Dim strVHCC() As String
Dim strVHCCMID() As String
Dim strVHCVMID() As String
Dim strVHCV() As String
Dim strHWCC() As String
Dim strHWCCMID() As String
Dim strHWCVMID() As String
Dim strHWCV() As String
Dim LineData As String
Dim SplitData() As String
Dim LineIter As Long
'Populate Set Number Listbox
With New Scripting.FileSystemObject
With .OpenTextFile(CMMData, ForReading)
Do Until .AtEndOfStream
LineData = .ReadLine
SplitData = Split(LineData, ",", 154, vbTextCompare)
'Extracting Serial Number
ReDim Preserve strSN(0 To LineIter)
strSN(UBound(strSN)) = SplitData(0)
'Extracting Set Number
ReDim Preserve strSet(0 To LineIter)
strSet(UBound(strSet)) = SplitData(1)
'Extracting Final Flow Area
ReDim Preserve strFF(0 To LineIter)
strFF(UBound(strFF)) = SplitData(14)
'Extracting /V/ To Hook CC
ReDim Preserve strVHCC(0 To LineIter)
strVHCC(UBound(strVHCC)) = SplitData(31)
Set_Select.AddItem SplitData(1)
LineIter = LineIter + 1
Loop
.Close
End With
End With
For LineIter = LBound(strFF) To UBound(strFF)
MsgBox strFF(LineIter)
Next LineIter
End Sub
我遇到问题的上述代码区域如下:
'Extracting /V/ To Hook CC
ReDim Preserve strVHCC(0 To LineIter)
strVHCC(UBound(strVHCC)) = SplitData(31)
代码将拆分数组填充到(30)。对于数组(31)及以上,我得到一个"下标超出范围"错误。我花了相当多的时间来确定错误,但都没有成功。我也很喜欢使用数组,所以我在过去一周里一直在教自己,但还有很多需要学习的东西。
我非常感谢帮助。谢谢。
答案 0 :(得分:0)
一般来说,我建议不要在循环中使用#info_container {
z-index: 500;
position: relative;
top: 0;
left: 0;
width: 80%;
height: 0;
margin: 0 auto;
background-color: #000000;
}
#info_box {
z-index: 500;
position: relative;
top: 50px;
width: 100%;
background-color: #FFFFFF;
text-align: center;
-webkit-box-shadow: 2px 2px 4px #888;
-moz-box-shadow: 2px 2px 4px #888;
box-shadow: 2px 2px 4px #888;
}
#info_box_wrapper {
position: relative;
display: block;
width: 100%;
margin: 0 auto;
text-align: center;
}
#info_box_left {
display: inline-block;
position: relative;
margin: 0 auto;
width: 45%;
min-width: 100px;
}
#info_box_right {
display: inline-block;
position: relative;
margin: 0 auto;
width: 45%;
min-width: 100px;
/* margin-bottom: 20px; */
}
.info_box_sub_wrapper {
position: relative;
display: inline-block;
width: 100%;
margin: 0 auto;
text-align: center;
}
#info_box_right_text {
position: relative;
color: #4D4D4D;
}
#info_box_logo, #info_box_screenshot {
position: relative;
width: 100%;
margin: 0 auto;
background-color: transparent;
background-size: 100%;
background-repeat: no-repeat;
background-position: center;
border-style: transparent;
border-color: #4D4D4D;
border-width: 2px;
cursor: pointer;
}
.info_img {
width: 100px;
margin: 20px;
background-color: #000;
}
,因为这是一个非常昂贵的过程。应用程序必须创建一个与redim一样大的新内存空间,然后将所有数据从旧数组移动到新数组,然后更新指针以查看新数组,然后垃圾收集旧数组。在这种情况下,我建议使用集合对象。
然而,这并没有回答你的问题。
当我运行你的代码时,我不能得到相同的错误,除非我尝试写入未声明的数组或在数组的边界之外。由于ReDim
进程可能你的阵列内存不足,但我的代码中根本没有使用ReDim,因为我发现它导致的问题多于解决的问题。
无论如何,以下是我如何使用集合执行此操作:
ReDim
道歉,如果这不是你想要的。
答案 1 :(得分:0)
我最后添加了一个错误处理程序,以显示一个消息框,说明发生错误的文本文件的行。一旦我开始工作,消息框显示正确的行,我能够看到从第31列开始,有一个重复的条目,从第31列到最后一列有空行值。一旦我用空值删除该行,我的代码工作正常。谢谢你的帮助。