嗨,我是 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
最佳答案
也许你的代码没有崩溃,只是进入了一个长循环。
原因是因为你没有使用
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.
另一件事,在你的代码中 i 是一个变体,而不是你想象的整数
dim i , j as integer
翻译为:
dim i 'vba by default sets it as variant
dim j as integer
您可以通过询问更少的“if”行来使代码更简单,但使用更多的 if。 首先使用 if 会更频繁地出现(if i<>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 就不需要在每次传递时重新计算/重新读取工作表(或变量)。
最后,在范围内查找相同值匹配的另一种方法是使用“match”函数。 (我不推荐“查找”功能,它速度较慢)
对于非常大范围的数据,您可以使用数组,而不是循环访问 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
稍后,您不再使用 cell(i,1).value ,而是在 MyArray (i,1) 中具有相同的值,并且这次末尾没有“.value” 不要忘记使用以下代码在最后释放一些内存:
erase MyArray
set sh=nothing
享受 VBA 的乐趣
关于vb.net - 代码无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22431578/