简体   繁体   English

使用日期的Excel VBA vlookup

[英]Excel VBA vlookup using Dates

I am working with three sheets. 我正在处理三张纸。 Worksheet Start Page has dates from A4 to lastrow. 工作表起始页的日期从A4到最后一行。 I have a Fund trend sheet with dates from A11 to last row. 我有一个基金趋势表 ,日期从A11到最后一行。 The vlookup is searching for Dates in the Fund trend sheet based on the list of Dates in the start page sheet. vlookup根据起始页工作表中的日期列表在基金趋势工作表中搜索日期 The search table range in the Fund trend sheet is Range(A11:C11) to lastrow . 资金趋势表中的搜索表范围Range(A11:C11)至lastrow When the date is found it offsets (3,0) , and that value is presented is sheet Acurred Expenses Range("C7"). 找到日期后,它会偏移(3,0) ,并且该值将显示在工作表已发生费用范围(“ C7”)中。 This will loop till the lastrow in sheet start page A4. 这将一直循环到工作表起始页A4中的最后一行

=VLOOKUP('Start page'!A4,'Fund Trend'!A11:C21,3,0) = VLOOKUP('开始页面'!A4,'资金趋势'!A11:C21,3,0)

=VLOOKUP('Start page'!A5,'Fund Trend'!A12:C22,3,0) = VLOOKUP('开始页面'!A5,'资金趋势'!A12:C22,3,0)

as code i have not been successful: 作为代码,我没有成功:

Sub equity() 

Dim Nav_date As Date 

Dim equity As Integer 

Nav_date = Sheets("Start page").Range("A4") 

equity = Application.WorksheetFunction.VLookup(Nav_date,_
 Worksheets("Fund Trend").Range("A11:C12"), 3, False) 

Sheets("Acurred Expenses").Range("C7") = equity 

End Sub

I think this answer can be broken down into three parts: correctly referencing the properties of a Range object, retrieving the last row of data, and using a loop 我认为这个答案可以分为三个部分:正确引用Range对象的属性,检索数据的最后一行以及使用循环

Correctly referencing the range's value: 正确引用范围的值:


The first thing that I noticed is that you are attempting to assign a Date variable as a Range object. 我注意到的第一件事是您试图将Date变量分配为Range对象。

This line: 这行:

Nav_date = Sheets("Start page").Range("A4")

Should be: 应该:

Nav_date = Sheets("Start page").Range("A4").Value

A Range is an object with has properties and methods. Range是具有属性和方法的对象。 You must explicitly reference what it is about the range you want to get. 你必须明确地引用它是什么 ,你想要得到的范围内。 It's value, it's cell address, etc. 它是值,它是单元格地址等。

Likewise this incorrect syntax is repeated below. 同样,下面将重复此错误语法。 The line: 该行:

Sheets("Acurred Expenses").Range("C7") = equity

Should be: 应该:

Sheets("Acurred Expenses").Range("C7").Value = equity

EDIT: Per the comments whytheq raises the point of default properties . 编辑:根据注释, whytheq引发默认属性的问题 Technically the code Sheets("Acurred Expenses").Range("C7") = equity is not incorrect, and will work, because the default property of the range is Value . 从技术上讲,代码Sheets("Acurred Expenses").Range("C7") = equity正确的,并且可以工作,因为范围的默认属性是Value I tend to prefer to be more explicit, but that is my personal preference so I always use Range.Value so there is not ambiguity. 我倾向于更明确一些,但这是我的个人喜好,因此我始终使用Range.Value因此不会产生歧义。 Either way should work though! 无论哪种方法都可以!

Retrieving the last row of the worksheet 检索工作表的最后一行


To find the last used row of the data in the worksheet, we can start at the bottom of the workbook and "look up" until we find the first row (which will correspond to the last row of the data in the worksheet). 要查找工作表中数据的最后使用行,我们可以从工作簿的底部开始并“查找”,直到找到第一行(它将与工作表中数据的最后一行相对应)。

This code would be the same as activating the last cell in column A and them pressing CTRL + Shit + 此代码与激活A列中的最后一个单元格相同,然后按CTRL + Shit +

Sub LastRow()

    Dim lRow As Long

    lRow = Cells(Rows.Count, 1).End(xlUp).Row

    Debug.Print lRow

End Sub

To reiterate, this starts at the very bottom row and goes all the way up, returning the row number of where it stops. 重申一下,它从最底端的行开始,一直到最上端,并返回其停止处的行号。 This corresponds to the last value entered in column A. You might need to change A if your data is in a different column. 这对应于在A列中输入的最后一个值。如果数据在其他列中,则可能需要更改A。

The loop 循环


Finally, we can put everything we've learned together. 最后,我们可以将我们学到的所有东西放在一起。 After you have lRow which corresponds to your last row in your set of data we can perform a look for the VLOOKUP like so: 在具有与数据集中的最后一行相对应的lRow之后,我们可以像下面这样查找VLOOKUP

Sub equity()

    Dim Nav_date As Date
    Dim equity As Integer
    Dim lRow As Long
    Dim i As Long

    lRow = Sheets("Start page").Cells(Rows.Count, 1).End(xlUp).Row

    For i = 4 To lRow 'Begin in Row 4 of the "Start page" sheet
        Nav_date = Sheets("Start page").Range("A" & i).Value

        'Tell code to continue even if error occurs
        On Error Resume Next

        equity = Application.WorksheetFunction.VLookup(Nav_date, _
         Worksheets("Fund Trend").Range("A11:C12"), 3, False)

        'Check the results of the VLOOKUP, an error number of 0 means no error
        If Err.Number = 0 Then
            'Here I use i+3 because the data started in row 7 and I assume
            'it will always be offset by 3 from the "Start Page"
            Sheets("Acurred Expenses").Range("C" & i + 3).Value = equity
        End If

        'Return to normal error handling
        On Error GoTo 0

    Next i

End Sub

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

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