代码没有响应

时间:2014-03-16 00:40:38

标签: vb.net excel vba excel-vba

嗨,我是vb的新手(今天又学会了),我写了一个代码,该代码应该计算列表中列出的相同序列号(列在列中),并发布每个序列号的编号它被列在另一张纸上。 当我按原样运行我的代码时,没有任何反应。没有错误,表格上没有新内容。我在当前代码中将列表限制为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

2 个答案:

答案 0 :(得分:3)

也许你的代码没有崩溃,只是在长循环中输入。

原因是因为你没有使用

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

在代码的开头,然后将其设置为true,最后设置为xlautomatic。

另一个原因是您经常要求您的代码再次读取相同的单元格值 (工作表(“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

你可以通过询问不太长的“if”行来使代码更简单,但是如果使用更多if。 首先使用if将更频繁地发生(如果i&lt;&gt; 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

其他方面,使用工作表变量:

 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不需要在每次传递时重新计算/重读表格(或变量)。

最后,在一个范围内找到相同值匹配的另一种方法是使用“匹配”功能。 (我不推荐“查找”功能,因为它比较慢)

对于非常大的数据范围,您可以使用数组,而不是循环遍历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的乐趣

答案 1 :(得分:0)

将数据传输到其他工作表的代码行,即以下行:

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

只会在“i = 10000”时执行,运行代码时不符合此条件,调试程序的运行方式,在调试模式下运行,一步一步(按“F8”键执行一行一次)