Application.Match 仅在查找选项卡上工作
Application.Match only working on Lookup tab active
我正在使用 Userforms 并有 2 个主要工作表。 1 (shE) 整理所选信息,2 (shL) 是存储所有可用选项的地方 - 这些是我的查找。
shL 在第 2 行每 4 列有一个团队列表,在第 3 行有每个团队的属性列表。所以结构是AB2 = Team1,AF2 = Team2,AJ2 = Team3。 AB3 = Attribute1, AC3 = Attribute2, AD3 = Attribute3, AE3 = Attribute4, AF3 = Attribute1 等..
Dim shE, shL as Worksheet
Dim FoundTeam as range
Dim c, cc, y, mtc as long
Set shE = Sheets("EnteredData")
Set shL = Sheets("Lookups")
Set FoundTeam = shL.Range("AB2:BX2").Find(what:=shE.Range("D10"))
c = FoundTeam.Column
cc = c + 3
y = shL.Cells(shL.Rows.Count, c).End(xlUp).Row
' the next line of code causes the run-time error 1004 if shL is not the last-active worksheet
mtc = Application.Match(shE.Range("J3"), shL.Range(Cells(4, cc), Cells(y, cc)), 0)
shE.Range("Q3").Value = WorksheetFunction.Index(shL.Range(Cells(4, c), Cells(y, c)), mtc) 'this is actually part of a loop so the shE.Range("Q3") is more like ("Q" & i)
为清楚起见,shE.Range("J3")
存储 Attribute4(可由用户选择)(在每个团队的 shL 第 4 列中找到),shE.Range("D10")
是团队名称。
我遇到的问题是代码可以完美运行 如果 shL 是 Excel 上最后一个活动的工作表。 但是,如果最后一个活动的工作表是 shE(或任何其他工作表),我将得到 运行-time error 1004: Method 'Range' of object '_Worksheet' failed.
我不明白为什么此代码仅在 shL 最后激活时才有效。这将由我组织中的几个不同的人使用,他们不会看到 shL。
我该怎么做才能修复 运行 时间错误,或者有什么方法可以使 lookup/find/pull 信息更好?
这是我找到的解决方法。不理想,但它似乎工作。
在我所做的每一次尝试中(Match
和 Find
都用完了),如果我在 shL
上,代码工作得很好,但如果我在其他地方则不行; Match
无法始终如一地工作。所以我决定在 VBA!
中不这样做
'Variables
Dim shE As Worksheet, shL As Worksheet
Dim ClmnLtrc As String, ClmnLtrcc As String
Dim c As Long, cc As Long, y As Long, Q As Long
Dim FoundTeam As Range, FoundRange As Range
'Define variables
Set shE = Sheets("EnteredData")
Set shL = Sheets("Lookups")
Set FoundTeam = shL.Range("AB2:BX2").Find(what:=shE.Range("D10"))
c = FoundTeam.Column
cc = c + 3
y = shL.Cells(shL.Rows.Count, c).End(xlUp).Row
Q = shE.Range("Q" & Rows.Count).End(xlUp).Row
ClmnLtrc = Split(Cells(1, c).Address, "$")(1)
ClmnLtrcc = Split(Cells(1, cc).Address, "$")(1)
'Write the Index Match into the cell
shE.Range("Q3").Value = "=INDEX(Lookups!$" & ClmnLtrc & "4:$" & ClmnLtrc & y & ",MATCH(EnteredData!$P$" & i & ",Lookups!$" & ClmnLtrcc & ":$" & ClmnLtrcc & y & ",0))"
'Convert the result to text
shE.Range("Q3:Q" & Q).Copy
shE.Range("Q3").PasteSpecial Paste:=xlPasteValues
我希望有一天这能对其他人有所帮助。仅仅因为我们在 VBA 中写入,并不意味着所有内容都必须在 VBA.
中
我正在使用 Userforms 并有 2 个主要工作表。 1 (shE) 整理所选信息,2 (shL) 是存储所有可用选项的地方 - 这些是我的查找。
shL 在第 2 行每 4 列有一个团队列表,在第 3 行有每个团队的属性列表。所以结构是AB2 = Team1,AF2 = Team2,AJ2 = Team3。 AB3 = Attribute1, AC3 = Attribute2, AD3 = Attribute3, AE3 = Attribute4, AF3 = Attribute1 等..
Dim shE, shL as Worksheet
Dim FoundTeam as range
Dim c, cc, y, mtc as long
Set shE = Sheets("EnteredData")
Set shL = Sheets("Lookups")
Set FoundTeam = shL.Range("AB2:BX2").Find(what:=shE.Range("D10"))
c = FoundTeam.Column
cc = c + 3
y = shL.Cells(shL.Rows.Count, c).End(xlUp).Row
' the next line of code causes the run-time error 1004 if shL is not the last-active worksheet
mtc = Application.Match(shE.Range("J3"), shL.Range(Cells(4, cc), Cells(y, cc)), 0)
shE.Range("Q3").Value = WorksheetFunction.Index(shL.Range(Cells(4, c), Cells(y, c)), mtc) 'this is actually part of a loop so the shE.Range("Q3") is more like ("Q" & i)
为清楚起见,shE.Range("J3")
存储 Attribute4(可由用户选择)(在每个团队的 shL 第 4 列中找到),shE.Range("D10")
是团队名称。
我遇到的问题是代码可以完美运行 如果 shL 是 Excel 上最后一个活动的工作表。 但是,如果最后一个活动的工作表是 shE(或任何其他工作表),我将得到 运行-time error 1004: Method 'Range' of object '_Worksheet' failed.
我不明白为什么此代码仅在 shL 最后激活时才有效。这将由我组织中的几个不同的人使用,他们不会看到 shL。
我该怎么做才能修复 运行 时间错误,或者有什么方法可以使 lookup/find/pull 信息更好?
这是我找到的解决方法。不理想,但它似乎工作。
在我所做的每一次尝试中(Match
和 Find
都用完了),如果我在 shL
上,代码工作得很好,但如果我在其他地方则不行; Match
无法始终如一地工作。所以我决定在 VBA!
'Variables
Dim shE As Worksheet, shL As Worksheet
Dim ClmnLtrc As String, ClmnLtrcc As String
Dim c As Long, cc As Long, y As Long, Q As Long
Dim FoundTeam As Range, FoundRange As Range
'Define variables
Set shE = Sheets("EnteredData")
Set shL = Sheets("Lookups")
Set FoundTeam = shL.Range("AB2:BX2").Find(what:=shE.Range("D10"))
c = FoundTeam.Column
cc = c + 3
y = shL.Cells(shL.Rows.Count, c).End(xlUp).Row
Q = shE.Range("Q" & Rows.Count).End(xlUp).Row
ClmnLtrc = Split(Cells(1, c).Address, "$")(1)
ClmnLtrcc = Split(Cells(1, cc).Address, "$")(1)
'Write the Index Match into the cell
shE.Range("Q3").Value = "=INDEX(Lookups!$" & ClmnLtrc & "4:$" & ClmnLtrc & y & ",MATCH(EnteredData!$P$" & i & ",Lookups!$" & ClmnLtrcc & ":$" & ClmnLtrcc & y & ",0))"
'Convert the result to text
shE.Range("Q3:Q" & Q).Copy
shE.Range("Q3").PasteSpecial Paste:=xlPasteValues
我希望有一天这能对其他人有所帮助。仅仅因为我们在 VBA 中写入,并不意味着所有内容都必须在 VBA.
中