简体   繁体   English

VLOOKUP并在同一工作表中搜索不同的n

[英]VLOOKUP and search for different n in the same worksheet

[Edited] I am so sorry I did not describe this very clear: My question is about the VLookup function: I used the code like: [编辑]我很抱歉我没有说清楚这个:我的问题是关于VLookup功能:我使用的代码如下:

    B5'n$CX$"

with the intention to let VBA go to the B5 worksheet and also determine which "n"(either n25 n35 and so on) it has to go to. 我打算让VBA进入B5工作表,并确定它必须去哪个“n”(n25 n35等)。

This was not working so I am here to seek if I can get any suggestion. 这不行,所以我在这里寻求是否可以得到任何建议。

Below is the original post: 以下是原帖:

I am very newbie in VBA, just started one or two times touching it. 我是VBA的新手,刚开始接触一两次。 I have modified the code as below, and my objective is to let VBA find the values for "DI"&i, depending on the values of "DE"&i in the spreadsheet B5, but in B5, the number "n" which depends on the values of "CX"&i, and when VBA goes to look up the values of "DE"&i, it has to determine the value of n firstly with the help of the values "CX"&i. 我修改了下面的代码,我的目标是让VBA找到“DI”和i的值,这取决于电子表格B5中“DE”和i的值,但是在B5中,数字“n”依赖于“CX”和i的值,当VBA查找“DE”和i的值时,它必须首先在值“CX”和i的帮助下确定n的值。 The screenshot of the worksheet B5 is as below: 工作表B5的屏幕截图如下:

https://www.dropbox.com/s/do6i7zeylaz0sch/B5.jpg?dl=0 https://www.dropbox.com/s/do6i7zeylaz0sch/B5.jpg?dl=0

My code is as below: 我的代码如下:

firstly, if "DE"&i >3.9, I would like VBA set "DI"&i = 0, else with vlookup function. 首先,如果“DE”&i> 3.9,我希望VBA设置“DI”&i = 0,否则使用vlookup功能。 Thank you very much for any help and advice. 非常感谢您的任何帮助和建议。 Appreciated. 赞赏。

    Sub FindPl()
    For i = 2 To 1730
    If .Cells("DE" & i).Value > 3.9 Then .Cells("DI" & i).Value = 0
    Else: .Cells("DI" & i).Value = 
    Application.WorksheetFunction.VLookup($DE$" & 
    i&",'[C:\Users\chenj5\Documents\Meeting_Jan_2019\simulation of Z1.9 for 
    Ultra Multi-Focal\Meeting 0220\Dataset used for simulation]B5'n$CX$" & 
    i&", 2, True)
    End If
    Next cell
    Next i
    End Sub

Will take a stab at some example code... I guessed on your source data and fixed some syntax errors... spend some time updating and appropriately dimensioning. 将采取一些示例代码...我猜测你的源数据并修复了一些语法错误...花一些时间更新和适当的尺寸。 There is too much going on/wrong with your posted code to provide anything more clear. 您发布的代码存在太多错误,无法提供更清晰的信息。

Sub FindPl()
    dim i as long, wb as workbook, OUTPUTRANGE as range, SEARCHRANGE as range
    set wb = "C:\Users\chenj5\Documents\Meeting_Jan_2019\simulation of Z1.9 for Ultra Multi-Focal\Meeting 0220\Dataset used for simulation.xlsx" 'added xlsx extension
    with wb
        set OUTPUTRANGE = .range(.cells(5,"D"),.cells(100,"D")) 'guessed... the range with the desired output
        set SEARCHRANGE = .range(.cells(5,"B"),.cells(100,"B")) 'guessed... the range where you will find .cells(i,"DE")
    end with
    with activeworkbook.sheets(1) 'FIX THIS TO FIT YOUR NEEDS
        For i = 2 To 1730
            If .Cells(i, "DE").Value > 3.9 Then 'FIXED SYNTAX ERROR
                .Cells(i, "DI").Value = 0
            Else: 
                .Cells(i, "DI").Value = Application.Index(OUTPUTRANGE, Application.Match(.cells(i, "DE").value,SEARCHRANGE, 0)) 
            End If
        Next   
    end with
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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