代码无响应

嗨,我是一个新手在vb(又名了解它今天),我写了一个代码,应该算一个列表(在列中列出)有多less相同的序列号,并张贴每个序列号的次数在另一张纸上。 当我运行我的代码时,没有任何反应。 没有错误,在纸上没有新的东西。 我在我的当前代码中将我的列表限制为1-10,因为当它是1-10000 excel崩溃时。 任何人都可以给我指针什么事情发生? 谢谢!

Public Sub count() Dim count As Long Dim i, j, a As Integer Dim skuNames() As Double a = 2 count = 0 ReDim skuNames(1) skuNames(0) = Worksheets("RawBarcodeData").Cells(1, 1).Value 'this checks if an sku matches an existing sku in the array and adds if it does not' For i = 1 To 10 For j = 0 To UBound(skuNames) If Worksheets("RawBarcodeData").Cells(i, 1).Value <> skuNames(j) And j <> UBound(skuNames) Then ElseIf Worksheets("RawBarcodeData").Cells(i, 1).Value <> skuNames(j) And j = UBound(skuNames) Then ReDim Preserve skuNames(0 To UBound(skuNames) + 1) skuNames(UBound(skuNames)) = Worksheets("RawBarcodeData").Cells(i, 1).Value Else End If Next j Next i 'this will count how many of each element of the array is listed and post it' For j = 0 To UBound(skuNames) For i = 1 To 10 If skuNames(j) = Worksheets("RawBarcodeData").Cells(i, 1).Value And i <> 10000 Then count = count + 1 ElseIf skuNames(j) = Worksheets("RawBarcodeData").Cells(i, 1).Value And i = 10000 Then count = count + 1 Worksheets("InventoryReport").Cells(a, 1).Value = skuNames(j) Worksheets("InventoryReport").Cells(a, 3).Value = count a = a + 1 count = 0 ElseIf skuNames(j) <> Worksheets("RawBarcodeData").Cells(i, 1).Value And i <> 10000 Then ElseIf skuNames(j) <> Worksheets("RawBarcodeData").Cells(i, 1).Value And i = 10000 Then Worksheets("InventoryReport").Cells(a, 1).Value = skuNames(j) Worksheets("InventoryReport").Cells(a, 3).Value = count a = a + 1 count = 0 End If Next i Next j End Sub 

也许你的代码没有崩溃,只是进入了一个很长的循环。

原因是因为你没有使用

  application.screenupdating= false application.enableevents=false application.calculation=xlManual 

在代码的开头,然后将其设置为true,并在结尾处自动设置。

另一个原因是你太频繁地询问你的代码来再次读取相同的单元格值(Worksheets(“RawBarcodeData”)。Cells(i,1).Value)

为什么不告诉你的代码在循环开始时记住它?

 CellI1= Worksheets("RawBarcodeData").Cells(i, 1).Value ' for example 

另外,当你使用i和j作为整数时,它会比Long更慢。

  dim i as long, j as long. 

另一件事,在你的代码我是一个变种,而不是一个整数,你可能会想

  dim i , j as integer 

翻译为:

  dim i 'vba by default sets it as variant dim j as integer 

你可以使代码更简单,只需要询问较less的“if”行,但使用更多的if。 首先使用,如果这将更经常发生(如果我> 1000)

所以像这样:

  if i<1000 then if CellI1=Skunames(j) then 'somethinf else 'no need to ask if <> because it's already not = 'something end if else 'here i=1000, no need to test if if CellI1=Skunames(j) then 'somethinf else 'no need to ask if <> because it's already not = 'something end if end if 

其他的事情,使用工作表的variables:

  dim Sh as Worksheet set sh=Worksheets("RawBarcodeData") 

然后sh.cells(i,1).value是更好的写法。

稍后,您甚至可以使用“With”语句。

 with sh a= .cells(i,j).value if skullname(j) <> .cells(i,1).value end with 

这样excel不需要重新计算/重新读取表单(或variables)在每次通过。

最后,另一种在一个范围内查找匹配值相同的方法是使用“匹配”function。 (我不build议“找”function,这很慢)

对于非常大的数据范围,您可以使用数组,而不是循环通过cells.values:

 dim MyArray() as variant 'works only with variant dim Max as Long dim Sh as Worksheet set Sh=thisworkbook.sheets("Test") with sh max = .cells( .rows.count,1).end(xlup).row 'return the last row in first column MyArray = .range ( .cells(1,1) , .cells ( max,1) ).value 'fast way to memorize the whole range 'can also be written = . range ( "A1:A" & max).value ' but is slower end with 

并在稍后,而不是使用单元格(i,1).value,你在MyArray(i,1)中有相同的值,并在这个时候结束没有“.value”不要忘记释放一些内存以此代码结束:

  erase MyArray set sh=nothing 

与VBA玩得开心

将数据传输到其他表单的代码行,即:

  Worksheets("InventoryReport").Cells(a, 1).Value = skuNames(j) Worksheets("InventoryReport").Cells(a, 3).Value = count 

只会在“i = 10000”时执行,运行代码时不符合这个标准,debugging程序是怎么做的,在debugging模式下一步一步地运行(按“F8”键一次执行一行)