我已经使用了一些代码几个小时,并且碰壁了。我已经有了通过子传递的代码,但是它不返回所选范围内的值,而是始终返回#N/A
。我已经使用公式完成了vlookup并使其按预期返回,但是我无法使其与VBA应用程序一起使用。
我确定这是我自己的格式化错误,但我无法弄清楚,因为在我看来,所有格式都相同。如果还有其他方法可以执行此操作,那么我可以接受。所有对象都是全局声明的,我认为问题根源在另一本工作簿中的VL_Srch_Rng
范围内,也许这就是我的错误所在??
详细信息::我要查找的值的格式是3位数字的商店编号.
,然后是12位的UPC。 Source_Bk
和Dist_Sht
是结果应到达的位置,而Mvmt_Bk
和Mvmt_Sht
range("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
我在Dist_Sht
的最左边插入一列,并使用公式=VLOOKUP(M3,'[Planogram - 52 Weeks Movement by store (2).xlsm]Master'!$H$4:$I$51171,2,FALSE)
,它返回了期望值。
我一直运行代码直到发表评论。这是来自Source_Bk.Dist_Sht
这是VL_Srch_Rng
上搜索范围(Mvmt_Bk.Mvmt_Sht
)的片段。显然,这些示例没有匹配的值,但我向您保证实际数据中有很多。
答案 0 :(得分:1)
向@BigBen大声疾呼主要问题是VL_Find
必须是variant
,其他问题包括错字,这会导致在错误的数组上设置Mvmt_srch_Rng
。
变暗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