【发布时间】:2019-10-06 13:51:19
【问题描述】:
我要做的是遍历所有行和列以查找机器内零件的数量。这是根据货号和设备/机器类型搜索的。如这个截图:
我的问题是我现在运行它的方式非常慢。在上面的屏幕截图中,只有一小部分单元格。它们下降到 +-500,大约等于公式的 22500 倍:
=ifERROR(INDEX(Datasheet!$B$1:$E$100;MATCH(1;(Datasheet!$D:$D=C$1)*(Datasheet!$B:$B=$AY15);0);4);"")
我想通过在所有单元格中提供我的静态值来使用 VBA 加速它。 我已经完成了很大一部分,我将在下面显示。
我几乎完成了(我能感觉到它!),但它不断向我返回类型 13 类型不匹配错误。我在堆栈溢出和互联网上发现了很多线程,但这些修复并不能为我自己解决。
我的代码:
'set all sheets
'----------------------------------------
Dim Isht As Worksheet
Dim Esht As Worksheet
Dim Dsht As Worksheet
Dim Gsht As Worksheet
Set Isht = ThisWorkbook.Worksheets("Instructionsheet")
Set Esht = ThisWorkbook.Worksheets("Exportsheet")
Set Dsht = ThisWorkbook.Worksheets("Datasheet")
Set Gsht = ThisWorkbook.Worksheets("Gathersheet")
'----------------------------------------
Dim EshtLR As Long
Dim EshtLC As Long
Dim DshtLC As Long
Dim DshtLR As Long
Dim OutputRange As Range
Dim SearchRange As Range
Dim MachineMatchCOL As Range
Dim ArticleMatchCOL As Range
Dim MachineType As String
Dim ArticleNumber As String
Dim StartRow As Long
Dim StartCol As Long
StartCol = Dsht.Range("P10").Value
StartRow = Dsht.Range("P11").Value
'Determine Last column in export sheet.
EshtLC = Esht.Cells(14, Columns.count).End(xlToLeft).Column
'Determine Last row in data sheet.
DshtLR = Dsht.Cells(Rows.count, 1).End(xlUp).Row
'Determine Last row in export sheet.
EshtLR = Esht.Cells(Rows.count, 1).End(xlUp).Row
Set OutputRange = Esht.Range(Esht.Cells(StartRow, 3), Esht.Cells(EshtLR, EshtLC - 9))
Set SearchRange = Dsht.Range(Dsht.Cells(1, 2), Dsht.Cells(DshtLR, 5))
Set MachineMatchCOL = Dsht.Range(Dsht.Cells(1, 4), Dsht.Cells(DshtLR, 4))
Set ArticleMatchCOL = Dsht.Range(Dsht.Cells(1, 2), Dsht.Cells(DshtLR, 2))
'=IFERROR(INDEX(Datasheet!$B$1:$E$100;Match(1;(Datasheet!$D:$D=C$1)*(Datasheet!$B:$B=$AY15);0);4);"")
'Datasheet!$B$1:$E$100 = SearchRange
'Datasheet!$D:$D = MachineMatchCOL
'Datasheet!$B:$B = ArticleMatchCOL
'C$1 = MatchineType
'$AY15 = ArticleNumber
j = StartRow
i = StartCol
For Each Row In OutputRange
For Each Column In OutputRange
MachineType = Esht.Range(Esht.Cells(1, i), Esht.Cells(1, i)).Value
ArticleNumber = Esht.Range(Cells(j, EshtLC - 5), Cells(j, EshtLC - 5)).Value
Esht.Cells(j, i).Value = Application.WorksheetFunction _
.IfError(Application.WorksheetFunction _
.Index(SearchRange, Application.WorksheetFunction _
.Match(1, (MachineMatchCOL = MachineType) * (ArticleMatchCOL = ArticleNumber), 0), 4), "")
i = i + 1
Next Column
j = j + 1
Next Row
这与一个范围不能等于一个值的事实有关,但我尝试了很长时间,无法弄清楚。
还请注意,循环可能不起作用,但这是要处理的下一个问题:-)。
我不希望您完全创建所有内容,但再次感谢您的友好推动。
更新:出现错误的行是:
Esht.Cells(j, i).Value = Application.WorksheetFunction _
.IfError(Application.WorksheetFunction _
.Index(SearchRange, Application.WorksheetFunction _
.Match(1, (MachineMatchCOL = MachineType) * (ArticleMatchCOL = ArticleNumber), 0), 4), "")
【问题讨论】:
-
它给出的错误是哪一行?
-
Esht.Cells(j, i).Value = Application.WorksheetFunction _ .IfError(Application.WorksheetFunction _ .Index(Range1, Application.WorksheetFunction _ .Match(1, (MachineMatchCOL = MachineType) * (ArticleMatchCOL = ArticleNumber), 0), 4), "") -
.Index(Range1, Application.WorksheetFunction _...嗯,你在哪里设置Range1?。在代码顶部使用Option Explicit,它将有助于识别未声明的变量。我猜你打算改用SearchRange? -
另外,据我所知,
Matchfot 3 参数。第一个是值,第二个是范围,第三个是布尔值(可选)。但是在你的代码中你输入了Match(1, (MachineMatchCOL = MachineType) * (ArticleMatchCOL = ArticleNumber), 0)(MachineMatchCOL = MachineType) * (ArticleMatchCOL = ArticleNumber)部分将只返回1或0,在任何情况下都是范围类型,所以Match没有地方搜索。 -
编辑了我的帖子。
Range1应该是SearchRange。
标签: excel vba loops indexing match