Excel Application.VLookup通过,但不';t返回值
我已经用一些代码工作了几个小时,遇到了麻烦。我有代码要通过sub,但它不会返回所选范围的值,它总是返回Excel Application.VLookup通过,但不';t返回值,excel,vba,vlookup,Excel,Vba,Vlookup,我已经用一些代码工作了几个小时,遇到了麻烦。我有代码要通过sub,但它不会返回所选范围的值,它总是返回#N/A。我已经用一个公式完成了vlookup,并让它按预期返回,但我就是不能让它与VBA应用程序一起工作 我确信这是我的一个格式错误,但我无法找出它,因为在我看来,一切都是一样的格式。如果有其他方法来执行此操作,我愿意接受。所有对象都是全局声明的,我认为问题的根源在于不同工作簿中的VL\u Srch\u Rng范围,也许这就是我的错误所在(?) 详细信息:我要查找的值的格式是一个3位数的存储编
#N/A
。我已经用一个公式完成了vlookup,并让它按预期返回,但我就是不能让它与VBA应用程序一起工作
我确信这是我的一个格式错误,但我无法找出它,因为在我看来,一切都是一样的格式。如果有其他方法来执行此操作,我愿意接受。所有对象都是全局声明的,我认为问题的根源在于不同工作簿中的VL\u Srch\u Rng
范围,也许这就是我的错误所在(?)
详细信息:我要查找的值的格式是一个3位数的存储编号a
然后是一个12位数的UPCSource\u Bk
和Dist\u Sht
是结果应该到达的地方,Mvmt\u Bk
和Mvmt\u Sht
范围(“H:I”)是被搜索范围所在的地方。返回值只是一个反映售出单位的数字
Dim Dist_Sht As Worksheet, Dist_LR As Long, Dist_LC As Long, Source_Bk As Workbook, VL_Rslt As Variant, VL_Find As String, VL_Srch_Rng As Range, Mvmt_LR As Long, Mvmt_LC As Long
Mvmt_Bk.Activate
Mvmt_Sht.Activate
With ActiveSheet
Range("H:H").Insert Shift:=xlToLeft
For o = 4 To Cells(Rows.Count, 2).End(xlUp).Row
Range("H" & o).Value = Range("A" & o) & "." & Range("C" & o).Value
Next o
Range("H3").Value = "Lookup Value"
Range("H4").ColumnWidth = 14
Range("H:H").NumberFormat = "000.000000000000"
Range("H:I").Value = Range("H:I").Value
Mvmt_LR = .Cells(Rows.Count, 2).End(xlUp).Row
Mvmt_LC = .Cells(3, Columns.Count).End(xlToLeft).Column
Set VL_Srch_Rng = Mvmt_Sht.Range(Mvmt_Sht.Cells(8, 3), Mvmt_Sht.Cells(Mvmt_LR, Mvmt_LC))
End With
Source_Bk.Activate
Dist_Sht.Activate
With Dist_Sht
For u = Dist_LR To 2 Step -1
If Range("J" & u).Value = "0" Then
Range("J" & u).EntireRow.Delete
lse
For s = 12 To Dist_LC
If Cells(u, s).Value <> "0" Then
Cells(u, s).Value = Mid(Cells(1, s), Len(Cells(1, s)) - 7, 3) & "." & Cells(u, 2)
Cells(u, s).NumberFormat = "000.000000000000"
Cells(u, s).Value = Cells(u, s).Value
End If
Next s
End If
Next u
End With
'Run to here for the examples below
For v = 2 To Dist_LR
For w = 12 To Dist_LC
If Cells(v, w) <> 0 Then
VL_Find = Cells(v, w).Value
VL_Rslt = Application.VLookup(VL_Find, VL_Srch_Rng, 2, False)
Dist_Sht.Cells(v, w) = VL_Rslt
If IsError(VL_Rslt) Then
Dist_Sht.Cells(v, w).Value = "Not Found"
End If
End If
Next w
Next v
Dim Dist Sht作为工作表,Dist LR作为Long,Dist LC作为Long,Source Bk作为工作簿,VL Rslt作为Variant,VL Find作为String,VL Srch Rng作为Range,Mvmt LR作为Long,Mvmt LC作为Long
Mvmt_Bk.Activate
Mvmt_Sht.激活
使用ActiveSheet
范围(“H:H”)。插入移位:=xlToLeft
对于o=4到单元格(Rows.Count,2)。结束(xlUp)。行
范围(“H”&o).值=范围(“A”&o)和“.”&Range(“C”&o).值
下一个o
范围(“H3”).Value=“查找值”
范围(“H4”)。列宽=14
范围(“H:H”).NumberFormat=“000.000000000000”
范围(“H:I”).值=范围(“H:I”).值
Mvmt_LR=.Cells(Rows.Count,2).End(xlUp).Row
Mvmt_LC=.Cells(3,Columns.Count).End(xlToLeft).Column
设置VL\u Srch\u Rng=Mvmt\u Sht.范围(Mvmt\u Sht.单元格(8,3),Mvmt\u Sht.单元格(Mvmt\u LR,Mvmt\u LC))
以
源代码\u Bk.Activate
距离短激活
有距离
对于u=Dist_LR至2步骤-1
如果范围(“J”和u).Value=“0”,则
范围(“J”和“u”).EntireRow.Delete
伦敦证券交易所
对于s=12至距离LC
如果单元格(u,s)。值为“0”,则
单元格(u,s)。值=中间(单元格(1,s)、Len(单元格(1,s))-7,3和“&”单元格(u,2)
单元格(美国).NumberFormat=“000.000000000000”
单元格(u,s).值=单元格(u,s).值
如果结束
下一个s
如果结束
下一个美国
以
'运行到此处查看下面的示例
对于v=2至距离
对于w=12至距离LC
如果单元格(v,w)为0,则
VL_Find=单元格(v,w).值
VL_Rslt=Application.VLookup(VL_Find,VL_Srch_Rng,2,False)
距离短单元(v,w)=VL\U Rslt
如果IsError(VL_Rslt)那么
距离短单元格(v,w).Value=“未找到”
如果结束
如果结束
下一个w
下一个v
我在Dist Sht
的最左边插入了一列,并使用公式=VLOOKUP(M3,“[Planogram-52周移动存储(2).xlsm]Master'!$H$4:$I$51171,2,FALSE)
,它返回预期值
我一直运行代码,直到注释出现。这是Source\u Bk.Dist\u Sht
这是Mvmt\u Bk.Mvmt\u Sht
上的搜索范围(VL\u Srch\u Rng
)的片段。显然,这些示例没有匹配的值,但我向您保证,实际数据中有很多匹配的值。
向@BigBen大声呼喊主要问题是
VL\u Find
需要是变体
,其他问题包括导致跨错误数组设置Mvmt\u srch\u Rng
的打字错误。
Dim VL_查找为变体
Mvmt_Bk.Activate
Mvmt_Sht.Activate
With ActiveSheet
Range("H:H").Insert Shift:=xlToLeft
For o = 4 To Cells(Rows.Count, 2).End(xlUp).Row
Range("H" & o).Value = Range("A" & o) & "." & Range("C" & o).Value
Next o
Range("H3").Value = "Lookup Value"
Range("H4").ColumnWidth = 14
Range("H:H").NumberFormat = "000.000000000000"
Range("H:I").Value = Range("H:I").Value
Mvmt_LR = .Cells(Rows.Count, 2).End(xlUp).Row
Mvmt_LC = .Cells(3, Columns.Count).End(xlToLeft).Column
Set VL_Srch_Rng = Mvmt_Sht.Range(Mvmt_Sht.Cells(4, "H"), Mvmt_Sht.Cells(Mvmt_LR, "I"))
End With
ActiveWindow.WindowState = xlMinimized
Source_Bk.Activate
Dist_Sht.Activate
With ActiveSheet
For u = Dist_LR To 2 Step -1
If Range("J" & u).Value = "0" Then
Range("J" & u).EntireRow.Delete
Else
For s = 12 To Dist_LC
If Cells(u, s).Value <> "0" Then
Cells(u, s).Value = Mid(Cells(1, s), Len(Cells(1, s)) - 7, 3) & "." & Cells(u, 2)
Cells(u, s).NumberFormat = "000.000000000000"
Cells(u, s).Value = Cells(u, s).Value
End If
Next s
End If
Next u
For v = 2 To Dist_LR
For w = 12 To Dist_LC
If Cells(v, w) <> 0 Then
VL_Find = Cells(v, w).Value
VL_Rslt = Application.VLookup(VL_Find, VL_Srch_Rng, 2, False)
Dist_Sht.Cells(v, w) = VL_Rslt
Dist_Sht.Cells(v, w).NumberFormat = 0
If IsError(VL_Rslt) Then
Dist_Sht.Cells(v, w).Value = "Not Found"
End If
End If
Next w
Next v
Mvmt\u Bk.Activate
Mvmt_Sht.激活
使用ActiveSheet
范围(“H:H”)。插入移位:=xlToLeft
对于o=4到单元格(Rows.Count,2)。结束(xlUp)。行
范围(“H”&o).值=范围(“A”&o)和“.”&Range(“C”&o).值
下一个o
范围(“H3”).Value=“查找值”
范围(“H4”)。列宽=14
范围(“H:H”).NumberFormat=“000.000000000000”
范围(“H:I”).值=范围(“H:I”).值
Mvmt_LR=.Cells(Rows.Count,2).End(xlUp).Row
Mvmt_LC=.Cells(3,Columns.Count).End(xlToLeft).Column
设置VL\U Srch\U Rng=Mvmt\U Sht.范围(Mvmt\U Sht.单元格(4,“H”)、Mvmt\U Sht.单元格(Mvmt\U LR,“I”))
以
ActiveWindow.WindowsState=xl最小化
源代码\u Bk.Activate
距离短激活
使用ActiveSheet
对于u=Dist_LR至2步骤-1
如果范围(“J”和u).Value=“0”,则
范围(“J”和“u”).EntireRow.Delete
其他的
对于s=12至距离LC
如果单元格(u,s)。值为“0”,则
单元格(u,s)。值=中间(单元格(1,s)、Len(单元格(1,s))-7,3和“&”单元格(u,2)
单元格(美国).NumberFormat=“000.000000000000”
单元格(u,s).值=单元格(u,s).值
如果结束
下一个s
如果结束
下一个美国
对于v=2至距离
对于w=12至距离LC
如果单元格(v,w)为0,则
VL_Find=单元格(v,w).值
VL_Rslt=Application.VLookup(VL_Find,VL_Srch_Rng,2,False)
距离短单元(v,w)=VL\U Rslt
距离短单元格(v,w).NumberFormat=0
如果IsError(VL_Rslt)那么
距离短单元格(v,w).Value=“未找到”
如果结束
如果结束
下一个w
下一个v
@BigBen-完成。如代码所示,结果应替换Dist\u Sht
L
列中的值。VLOOKUP公式使用M-M3列作为查找值。您的一个屏幕截图是否应该包含M列?您的With Dist_Sht
块的内容不合格。lse
条件:)(应该是Else
)中有一个打字错误,这将阻止代码编译。假设这只是复制/粘贴错误,而不是在代码中。如果您在VL\u Rslt=Application.VLookup(VL\u Find,VL\u Srch\u Rng,2,False)上放置一个断点,并计算该状态