如果日期为今天,则使用VLookup进行循环

时间:2018-09-10 19:36:36

标签: excel-vba vlookup

在这里,我试图对两个不同工作簿的两个不同工作表之间的某些值(例如a,b,c,d)进行vlookup。 我站在一个单独的工作簿上,在其中我放置了一个命令按钮来运行VBA代码,但是 在以下行中显示错误:“参数不可选”:

y_Sheet.Cells(i, 4) = Application.WorksheetFunction.VLookup(Cells(i, 2).myrange.False)

在这方面的任何帮助都受到高度赞赏。

下面指定了完整的代码以供参考:

Sub macro()
With Application
   .ScreenUpdating = False
   .EnableEvents = False
End With

'On Error Resume Next

Dim x As Workbook
Dim x_Sheet As Worksheet

Dim y As Workbook
Dim y_Sheet As Worksheet

Set x = Workbooks.Open("C:\Users\Win 7\Desktop\V_Lookup\database")
Set y = Workbooks.Open("C:\Users\Win 7\Desktop\V_Lookup\report")

Set x_Sheet = x.Sheets("Sheet1")
Set y_Sheet = y.Sheets("Data")

Dim endrow As Long
Dim nextrow As Long

endrow = x_Sheet.Range("A" & Rows.Count).End(xlUp).Row
nextrow = y_Sheet.Range("A" & Rows.Count).End(xlUp).Row + 1

'COPY ENTIRE ROW USING VLOOKUP IF THE DATE IS TODAY
For i = 2 To endrow
    If x_Sheet.Cells(i, 1).Value = Date Then
        x_Sheet.Cells(i, 1).EntireRow.Copy Destination:=y_Sheet.Cells(nextrow, "A")
        nextrow = nextrow + 1
    End If
Next i

'AUTOMATE REQUIRED VALUE USING VLOOKUP IF THE DATE IS TODAY
Dim lastrow As Long
lastrow = y_Sheet.Range("A" & Rows.Count).End(xlUp).Row
Set myrange = x.Sheets("Sheet2").Range("A:B")
For ii = 2 To lastrow
    If y_Sheet.Cells(ii, 1).Value = Date Then
        y_Sheet.Cells(ii, 4) = Application.WorksheetFunction.VLookup(Cells(ii, 2).myrange.False)
    End If
Next ii

MsgBox "Done"

结束子

0 个答案:

没有答案
相关问题