我正在研究一个长宏以使过程自动化。不幸的是,部分代码根本无法正常工作-我在说的是查询部分。我在While循环中尝试了vlookup和Index / match。
到目前为止,我在许多其他宏中使用了基本上相同的循环,而没有任何问题。我尝试调整格式(即使使用Format painter)也没有成功。
我将查找的值隔离在一个新文件中,只是执行了查找部分,但仍然无法正常工作。
注意,当我执行Vlookup或index.Match在Excel中手动匹配时,它没有问题。
代码:
Sub lookup()
'Variables for the Vlookups
Dim newRow As Long
Dim newCL As String
newRow = 7
Dim rn As Range
Set rn = Worksheets("trysheet").Range("C:C")
Dim mrange As Range
Set mrange = Worksheets("trysheet").Range("A:A")
On Error Resume Next
'lookups
Do While Worksheets("test").Cells(newRow, 4).Value <> ""
newCL = Worksheets("test").Cells(newRow, 4).Value
Worksheets("test").Cells(newRow, 5) = Application.Index(rn,
Application.Match(newCL, mrange, 0))
newRow = newRow + 1
Loop
End Sub
我尝试使用Application.WorksheetFunction而不只是Application。但是然后我要么收到1004错误-“无法获取WorksheetFunction类的Match / Vlookup属性”,要么是应用程序定义的错误。
我将非常感谢您的帮助,我无法解决问题,这很愚蠢...
如果有人想看一下,我也可以发送Excel文件,因为我不能在这里发布它。
编辑: 顺便说一下,我使用传统的vlookup函数(借助宏录制和修改循环)达到了所需的结果:
Sub lookup()
'Variables for the Vlookups
Dim newRow As Long
newRow = 7
Dim rn As Range
Set rn = Worksheets("trysheet").Range("C:C")
Dim mrange As Range
Set mrange = Worksheets("trysheet").Range("A:A")
On Error Resume Next
'
'Get Employee
Do While Worksheets("test").Cells(newRow, 4).Value <> ""
Worksheets("test").Cells(newRow, 5).FormulaR1C1 = "=VLOOKUP(RC[-1], trysheet!C[-4]:C[-2],3,0)"
newRow = newRow + 1
Loop
End Sub
我通常尝试避免使用此方法,而是坚持使用application.WorksheetFunction或application.Vlookup(example),因为我发现它更干净并自动删除了公式,但在这种情况下可以完成工作。仍然有人可以帮助我理解为什么应用程序方法在这里不起作用的原因,我将不胜感激,因为这是我第一次遇到这种情况。
yytsunamiyy在下面有一些不错的建议,但仍不完整。
答案 0 :(得分:0)
您的索引公式缺少列引用,请尝试:
Worksheets("test").Cells(newRow, 5) = Application.Index(rn, Application.Match(newCL, mrange, 0),1)
以下是我的解决方案:
您的工作表引用及其范围似乎不正确。最佳实践是使用图纸的代号而不是参考表的显示名称。这是一个如何执行此操作以及如何在过程中使用它的示例:
Public shTest As Worksheet
Public shTrySheet As Worksheet
Sub run_lookups()
Call defineVars
Call lookup_newrow
End Sub
Sub defineVars()
Dim sh As Worksheet
For Each sh In ThisWorkbook.Sheets
Select Case sh.CodeName
Case "Test"
Set shTest = sh
Case "TrySheet"
Set shTrySheet = sh
End Select
Next sh
End Sub
Sub lookup_newrow()
'Variables for the Vlookups
Dim newRow As Long
Dim newCL As String
newRow = 7
Dim rn As Range
Set rn = shTrySheet.Range("C:C")
Dim mrange As Range
Set mrange = shTrySheet.Range("A:A")
'On Error Resume Next
'lookups
Do While shTest.Cells(newRow, 4).Value <> ""
newCL = shTest.Cells(newRow, 4).Value
shTest.Cells(newRow, 5).Value = Application.WorksheetFunction.Index(rn, Application.Match(newCL, mrange, 0), 1)
newRow = newRow + 1
Loop
End Sub
将R1C1格式的正确公式简单地写入范围的解决方案更为优雅。如果需要固定值,只需复制并粘贴特殊内容即可。
Public shTest As Worksheet
Public shTrySheet As Worksheet
Sub run_lookups2()
Call defineVars2
Call lookup_values
End Sub
Sub defineVars2()
Dim sh As Worksheet
For Each sh In ThisWorkbook.Sheets
Select Case sh.CodeName
Case "Test"
Set shTest = sh
Case "TrySheet"
Set shTrySheet = sh
End Select
Next sh
End Sub
Sub lookup_values()
Dim rLookupResults As Range
Dim sName As String
sName = shTrySheet.Name 'Sheet is referenced through codename, which is unlikely to get changed, but for fomula displayed sheetname is used
With shTest
Set rLookupResults = .Range(.Cells(7, 5), .Cells(.Cells(.Rows.Count, 4).End(xlUp).Row, 5))
'enter LOOKUP() Formula in Lookup Rsult Rang
rLookupResults.FormulaR1C1 = "=IFERROR(LOOKUP(RC[-1]," & sName & "!C[-4]," & sName & "!C[-2]),"""")"
'if fixed values are need copy and pastespecial
rLookupResults.Copy
rLookupResults.PasteSpecial xlPasteValues
End With
End Sub
答案 1 :(得分:0)
如果只想将公式转换为值,则可以执行以下操作。
Do While Worksheets("test").Cells(newRow, 4).Value <> ""
Worksheets("test").Cells(newRow, 5).FormulaR1C1 = "=VLOOKUP(RC[-1], trysheet!C[-4]:C[-2],3,0)"
Worksheets("test").Cells(newRow, 5).Value = Worksheets("test").Cells(newRow, 5).Value
newRow = newRow + 1
Loop
其他方式
Sub lookup()
'Variables for the Vlookups
Dim newRow As Long
Dim newCL As String
Dim Mrange As Range
Dim rn As Range
newRow = 7
Set rn = Worksheets("trysheet").Range("C:C")
Set Mrange = Worksheets("trysheet").Range("A:C") '<~~ A ~ C
With Worksheets("test")
Do While .Cells(newRow, 4).Value <> ""
newCL = .Cells(newRow, 4)
.Cells(newRow, 5).Value = WorksheetFunction.VLookup(newCL, Mrange, 3, 0)
newRow = newRow + 1
Loop
End With
End Sub