簡體   English   中英

VBA如何有效地在數組變量中查找/查找

[英]VBA how to efficiently find/vlookup in array variable

在 Excel 2013 中使用 Microsoft Visual Basic Application Edition 7.1。自學 VBA,之前我只是努力獲得結果(我是供應鏈經理,而不是與 IT 相關的職位)。 現在我也想關心效率問題。
幾個提示列表同意避免使用 Select/Activate 和讀取大塊中的數據。 但是我不知道如何在將其保存在數組變量中后在大塊中進行搜索。

該宏實際上讀取缺失項目的列表(包含在缺失項目文件的“Sheet1”的 A 列中)並將列表划分為三個新表(即缺失項目中的“tornio”、“centro”和“acquisti”)。 item 文件),基於丟失的文件(“Sheet1”的 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

如何在上述數組變量上搜索缺失的項目?

我不確定以下策略是否是實現效率的最佳策略,但定性測量表明,運行宏所需的時間已減少了大約 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)

[...] 然后繼續

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM