简体   繁体   中英

Creating an If Statement within a Loop for a Vlookup

Trying to create a loop with if statement, where I go through every name of a column, it then creates an IFERROR with VLOOKUP that will extract the inventory of that name at a certain date (found in another workbook), and if that product does not have any inventory on that date it equals zero.

For some reason it is giving me an error when I try to write the VLOOKUP formula , any help would be great.

In the consolidated page, the names of the product are on column B, then the date is on column C, I then want to place the value on column K.

thanks!

Sub IFTEST()


LR = ActiveSheet.Cells(Rows.Count, "M").End(xlUp).Row

Dim APPLE As Range
Dim GRAPE As Range
Dim RICE As Range
Dim BREAD As Range
Dim PASTA As Range
Dim INVENTORY As Range
Dim cell As Range


Set APPLE = Workbooks("APPLE.xls").Worksheets("APPLE").Range("K:N")
Set GRAPE = Workbooks("GRAPE.xls").Worksheets("GRAPE").Range("K:N")
Set RICE = Workbooks("RICE.xls").Worksheets("RICE").Range("K:N")
Set BREAD = Workbooks("BREAD.xls").Worksheets("BREAD").Range("K:N")
Set PASTA = Workbooks("PASTA.xls").Worksheets("PASTA").Range("K:N")
Set INVENTORY= Workbooks("INVENTORY.xlsm").Sheets("INVENTORY").Range("B2:B" & LR)

Application.ScreenUpdating = False


For Each cell In Inventory
    If cell.Value = "apple" Then

'here is where I get the error in the lookup

        cell.Offset(0, 12).Value = WorksheetFunction.IfError(WorksheetFunction.VLookup(cell.Offset(0, 1).Value, APPLE, 4, False), 0)

    ElseIf cell.Value = "grape" Then

        cell.Offset(0, 12).Value = WorksheetFunction.IfError(WorksheetFunction.VLookup(cell.Offset(0, 1).Value, GRAPE, 4, False), 0)

    ElseIf cell.Value = "rice" Then

        cell.Offset(0, 12).Value = WorksheetFunction.IfError(WorksheetFunction.VLookup(cell.Offset(0, 1).Value, RICE, 4, False), 0)

    ElseIf cell.Value = "bread" Then

        cell.Offset(0, 12).Value = WorksheetFunction.IfError(WorksheetFunction.VLookup(cell.Offset(0, 1).Value, BREAD, 4, False), 0)

    ElseIf cell.Value = "pasta" Then

        cell.Offset(0, 12).Value = WorksheetFunction.IfError(WorksheetFunction.VLookup(cell.Offset(0, 1).Value, PASTA, 4, False), 0)

    Else

         cell.Offset(0, 12).Value = 0



    End If


Next





End Sub

Here you have both, a way to avoid the nested worksheet function and an approach without the space consuming multiple IFs or their smarter but slow replacement with a Select... Case statement.

Sub IfTest()
    ' 012

    Dim Wbooks() As String
    Dim Inventory As Range
    Dim Target As Range
    Dim Cell As Range
    Dim Tmp As Variant
    Dim i As Integer

    Wbooks = Split("Apple,Grape,Rice,Bread,Pasta", ",")

    With Workbooks("Inventory.xlsm").Sheets("INVENTORY")
        Set Inventory = .Range(.Cells(2, "B"), .Cells(.Rows.Count, "B").End(xlUp))
    End With

    Application.ScreenUpdating = False
    For Each Cell In Inventory
        Tmp = Cell.Value
        For i = UBound(Wbooks) To 0 Step -1
            If StrComp(Wbooks(i), Tmp, vbTextCompare) = 0 Then
                ' I would reduce the size of this range
                Set Target = Workbooks(Tmp & ".xls").Worksheets(Tmp).Range("K:N")
            End If
        Next i

        If i Then
            On Error Resume Next
            Tmp = WorksheetFunction.VLookup(Cell.Offset(0, 1).Value, Target, 4, False)
            If Err Then Tmp = 0
            On Error GoTo 0
        Else
            Tmp = 0
        End If
        Cell.Offset(0, 12).Value = Tmp
    Next Cell
End Sub

I hesitated to presume that columns K:N in the target workbook would be of identical length but that would be one way to avoid giving Excel 1.4 million cells to search for each VLookup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM