在Excel中使用Microsoft Visual Basic Application Edition 7.1 在VBA上自学成才,之前我只是努力获得结果(我是供应链经理,而不是IT相关职位)。现在我也想关注效率问题 几个提示列表同意避免使用Select / Activate和大块读取数据。但是我不知道如何在将数据保存在数组变量中之后搜索大块。
宏实际上读取了缺失项目的列表(包含在"第1页和第34页的A列;缺少的项目文件中)并将列表分成三个新表格(即" tornio&#34 ," centro"和" acquisti"在缺失的项目文件中),基于丢失文件中包含的信息(" Sheet1&#34的C列; )和生产图表文件。缺少报告的项目"产品"在C列上将复制在" tornio"或者" centro"基于生产图表文件的内容的工作表或两者;丢失的项目没有报告"产品"必须将C列复制到" acquisti"片。
我效率低下的代码是:
Dim errore(1 To 10)
'create a variable to store problems (i.e. missing items which are not on the production chart file)
Do
'start the cycle to read the missing items list which is stored in Column A
If Cells(i, "A") <> vbNullString Then
If Cells(i, "C") = "Prod." Then
'based on information on the missing file, prepare for division
sl = Cells(i, "A")
'store the missing item code
Windows("cicli.xls").Activate
'activate the production chart file
Set d = Range("C:C").Find(sl)
'search the missing item on the production chart file
If Not d Is Nothing Then
'if you find the missing item on the production chart file
j = 1 'integer
centro = False 'dummy
tornio = False 'dummy
Do
'start the cycle to read the production chart file
If Cells(d.Row + j, "C") = 0 Then
'continue as long as you find zeros (see image)
Select Case Left(Cells(d.Row + j, "K"), 3)
'based on the machine type, prepare for division
Case "CLO"
If centro = False Then
'if machine type is CLO then return to the missing item file and copy the current row the centro sheet
centro = True
Windows(ma).Activate
Sheets("Sheet1").Rows(i).Copy Destination:=Sheets("centro").Rows(c)
c = c + 1
Windows("cicli.xls").Activate
End If
Case "TCN", "TPA"
'if machine type is TCN or TPA then return to the missing item file and copy the current row the tornio sheet
If tornio = False Then
tornio = True
Windows(ma).Activate
Sheets("Sheet1").Rows(i).Copy Destination:=Sheets("tornio").Rows(t)
t = t + 1
Windows("cicli.xls").Activate
End If
End Select
j = j + 1
End If
Loop Until Cells(d.Row + j, "C") <> 0 Or Cells(d.Row + j, "C") = vbNullString
'close the cycle to read the production chart
Else:
'if you don't find the missing item on the production chart, please store the missing code
errore(e) = sl
e = e + 1
End If
Else:
'based on information on the missing file, prepare for division
Rows(i).Copy Destination:=Sheets("acquisti").Rows(a)
a = a + 1
End If
End If
Windows(ma).Activate
'return on the missing list
Sheets("Sheet1").Select
i = i + 1
Loop Until Cells(i, "B") = vbNullString
'close the cycle to read the missing items list
现在假设我将生产图表保存在数组变量
上 Dim cicli as Variant
Windows("cicli.xls").Activate
cicli = Union(Columns("C:C"), Columns("K:K")).Value
如何在上面搜索此类数组变量上缺少的项目?
答案 0 :(得分:0)
我不确定以下是实现效率的最佳策略,但定性测量表明宏运行所需的时间减少了大约44%
Workbooks.Open (laura & "pianificazione produzione\cicli.xls")
Set ci = Range(Cells(1, "C"), Cells(Cells(Rows.Count, "C").End(xlUp).Row, "C"))
Set ka = Range(Cells(1, "K"), Cells(Cells(Rows.Count, "K").End(xlUp).Row, "K"))
然后启动Do循环
Dim errore(1 To 10)
If Cells(i, "A") <> vbNullString Then
If Cells(i, "C") = "Prod." Then
sl = Cells(i, "A")
Set d = ci.Find(sl)
'now search on saved range ci rather than on production chart file
If Not d Is Nothing Then
j = 1 'integer
centro = False 'dummy
tornio = False 'dummy
Do
If ci(d.Row + j, 1) = 0 Then
'now continue on saved range ci rather than on production chart file
Select Case Left(ka(d.Row + j, 1), 3)
[...]然后继续