VLookup结果未显示

时间:2018-09-21 09:04:32

标签: vba

我有一个工作表,其中使用了Instr函数从字符串中切出所需的文本。现在,我正在尝试使用VLookup 使用放置数据库的其他工作簿找出该文本的实际/实际名称。 但是VLookup结果不在必填列中。

任何人都可以建议为此采取哪些行动。 下面提到了整个代码,以供参考。

    Dim lastrow As Long, Text As String, DESPOSITION As String, i As Integer
lastrow = x.Sheets("Sheet1").Range("A" & Rows.count).End(xlUp).Row
For i = 2 To lastrow
    Text = Cells(i, 5).Value
    DESPOSITION = InStr(Text, "DES")
    Cells(i, 6).Value = Left(Text, DESPOSITION - 2)
Next i

Range("G2").Select
ActiveCell.FormulaR1C1 = "=TRIM(RC[-1])"
Range("G2").Select
Selection.AutoFill Destination:=Range("G2:G2000"), Type:=xlFillDefault
Range("G:G").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Application.CutCopyMode = False
Columns("G:G").Select
Selection.Cut
Range("F1").Select
ActiveSheet.Paste
ActiveWorkbook.Save

'Do VLookup to rename Customer name
Dim y As Workbook
Set y = Workbooks.Open("C:\Users\arisarka\Desktop\DATABASE\RemitterVsCustomer.xlsx")
Dim y_sheet As Worksheet
Set y_sheet = y.Sheets("Sheet1")
Dim endrow As Long
endrow = x.Sheets("Sheet1").Range("A" & Rows.count).End(xlUp).Row

Set myrange = y_sheet.Range("A2:B86")
For ii = 2 To endrow
      Cells(ii, 7).Value = Application.WorksheetFunction.VLookup(Cells(ii, 6), myrange, 2, False)
Next ii

1 个答案:

答案 0 :(得分:0)

要解决主要问题,我认为存在范围未完全限定和/或设置不正确的问题。

当您打开工作簿y时,它将成为活动工作簿。此时,Cells(ii,7)指的是活动工作簿(y),因为它不完全合格,我想您打算将其放入工作簿x中:

For ii = 2 To endrow
      x.Sheets("Sheet1").Cells(ii, 7).Value = Application.WorksheetFunction.VLookup(x.Sheets("Sheet1").Cells(ii, 6), myrange, 2, False)
Next ii

下一个问题将是VLOOKUP()引发错误。您可以通过将vlookup放在单独的函数中来解决此问题:

Private Function vlookup(value As Variant, rng As Range, col As Integer, mode As Boolean)
    On Error GoTo uhoh:
    vlookup = Application.WorksheetFunction.vlookup(value, rng, col, mode)
    Exit Function
uhoh:
    vlookup = ""
End Function

然后从循环中调用该函数,如果失败,您将得到一个空字符串而不是错误:

For ii = 2 To endrow
      x.Sheets("Sheet1").Cells(ii, 7).value = vlookup(x.Sheets("Sheet1").Cells(ii, 6), myrange, 2, False)
Next ii

我还要清理其他几件事:

首先,lastrowendrow指的是同一范围,并且您尚未更改此范围的大小:

lastrow = x.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
endrow = x.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row

也许endrow打算放在工作簿y中?如果没有,那么您可以重复使用lastrow

由于未声明的变量(myRange,x),我遇到了一些问题,也许它们包含在您未提供的代码中?我认为x是活动工作簿。

我还必须将DESPOSITION更改为Integer。您将其设置为String,但是将其设置为INSTR()类型的Integer的结果。

在解决了这些小问题之后,您可以通过删除不必要的复制粘贴来清理一些代码。

这里有更多的机会,但是作为初次通过,那会很好:

Sub test()

    'DESPOSITION changed to integer
    Dim lastrow As Long, Text As String, DESPOSITION As Integer, i As Integer

    'I added these, you may already have them in code you didnt share:
    Dim x As Workbook
    Set x = ThisWorkbook

    lastrow = x.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row

    'Just put the value you want directly into Column F, deprecate all the copy/paste stuff.
    For i = 2 To lastrow
        Text = x.Sheets("Sheet1").Cells(i, 5).value
        DESPOSITION = InStr(Text, "DES")
        x.Sheets("Sheet1").Cells(i, 6).value = Application.WorksheetFunction.Trim(Left(Text, DESPOSITION - 2))
    Next i

    x.Save

    'Do VLookup to rename Customer name
    Dim y As Workbook
    Set y = Workbooks.Open("C:\Users\arisarka\Desktop\DATABASE\RemitterVsCustomer.xlsx")

    For i = 2 To lastrow
        With x.Sheets("Sheet1")
          .Cells(i, 7).value = vlookup(.Cells(i, 6), y.Sheets("Sheet1").Range("A:B"), 2, False)
        End With
    Next i

End Sub

Private Function vlookup(value As Variant, rng As Range, col As Integer, mode As Boolean)
    On Error GoTo uhoh:
    vlookup = Application.WorksheetFunction.vlookup(value, rng, col, mode)
    Exit Function
uhoh:
    vlookup = ""
End Function