我在使用VBA for excel时出现问题。我使用这个宏进入其他存储的excel工作簿,扫描所有工作表,查看某个类开始某个区域的日期。然后它会将所有信息编译成一个工作表并按开始日期排序。在我扩展了它正在寻找的物品之后,我达到了尺寸限制并且不得不将它分成两个潜艇。现在的问题是第一个子运行正常,但是当第二个子运行时,它会覆盖第一个子数据中的数据。我试图插入以下错误。
iRow = Ws.Cells(Ws.Rows.Count, 1).End(xlUp).Row + 1
我可以通过添加以下
来获取现在运行的代码Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Board")
我已将此代码放在宏的主要部分和子部分中。问题仍在继续,我的第一个子被另一个子覆盖。任何帮助或想法都会很棒。
谢谢
修改
评论代码:
Set appExcel = Nothing
intcounter = intcounter + 1
strClassList = Right(strClassList, Len(strClassList) - 2)
strClassDateList = Right(strClassDateList, Len(strClassDateList) - 2)
strClassHourList = Right(strClassHourList, Len(strClassHourList) - 2)
strClassStopList = Right(strClassStopList, Len(strClassStopList) - 2)
intMainCounterTop = (intMainCounter + (Len(strClassList) - Len(Replace(strClassList, Chr(13), "", 1, , vbBinaryCompare)))) '- 1
For i = 2 To intMainCounterTop
Worksheets("Board").Cells(i, 1) = "15U3 " & Left(strClassList, InStr(1, strClassList, Chr(13), vbBinaryCompare) - 1)
Worksheets("Board").Cells(i, 2) = "AF"
Worksheets("Board").Cells(i, 3) = Left(strClassDateList, InStr(1, strClassDateList, Chr(13), vbBinaryCompare) - 1)
Worksheets("Board").Cells(i, 4) = Left(strClassHourList, InStr(1, strClassHourList, Chr(13), vbBinaryCompare) - 1)
Worksheets("Board").Cells(i, 5) = Left(strClassStopList, InStr(1, strClassStopList, Chr(13), vbBinaryCompare) - 1)
strClassList = Right(strClassList, Len(strClassList) - InStr(1, strClassList, Chr(13), vbBinaryCompare) - 1)
strClassDateList = Right(strClassDateList, Len(strClassDateList) - InStr(1, strClassDateList, Chr(13), vbBinaryCompare) - 1)
strClassHourList = Right(strClassHourList, Len(strClassHourList) - InStr(1, strClassHourList, Chr(13), vbBinaryCompare) - 1)
strClassStopList = Right(strClassStopList, Len(strClassStopList) - InStr(1, strClassStopList, Chr(13), vbBinaryCompare) - 1)
Next i
Worksheets("Board").Cells(i, 1) = "15U3 " & strClassList
Worksheets("Board").Cells(i, 2) = "AF"
Worksheets("Board").Cells(i, 3) = strClassDateList
Worksheets("Board").Cells(i, 4) = strClassHourList
Worksheets("Board").Cells(i, 5) = strClassStopList
答案 0 :(得分:0)
这不是一个答案,而是指向如何改进代码的指针。
从您发布的代码看来,您有许多字符串,这些字符串实际上是由Chr(13)
分隔的项目列表,您正在处理这些字符串以提取各个部分。
如果这实际上是你正在做的事情,那么如果你第一次使用
那么代码会更少(并且更容易管理)arrStrings = Split(bigStringHere, Chr(13))
将每个字符串转换为数组。然后,您可以使用更少的编码开销来遍历这些数组。
示例:
Sub StringsToArray()
Dim strClasses, arrClasses, n
strClasses = "Class1" & Chr(13) & "Class2" & Chr(13) & "Class3"
arrClasses = Split(strClasses, Chr(13))
'arrClasses is now an array containing all the
' items from "strClasses" which were separated by Chr(13)
' You can use a counter to loop through the array
For n = LBound(arrClasses) To UBound(arrClasses)
Debug.Print n, arrClasses(n)
Next n
End Sub