Excel 2013:我需要Sheet1 Col D = Sheet2 Col D IF Sheet1 Col B = Sheet 2 Col A,否则Sheet1 Col D是昨天的日期

时间:2016-01-11 16:54:56

标签: excel vba excel-vba

使用Excel2013,我需要在Sheet1列D中填充单元格,只有Sheet1列B中的条目= Sheet2列A,否则Sheet1列D将填充昨天的日期。这需要参考工作表的索引号,而不是名称或代码名称,因为Sheet2将每天更改。

我对VBA很新,真的不知道从哪里开始!

编辑:Sheet1是完整列表,Sheet2是每日例外列表,或者只是那些未报告的例外。所以我需要它来查看ColumnA的所有内容并将其与所有columnB进行比较。 Sheet2 ColumnD是最后一个报告日期。

对于这个非常陌生,我所尝试的都是相当基本的公式。如果我不需要它总是参考第二张,那就完成了! 我感谢所有的建议!

2 个答案:

答案 0 :(得分:1)

使用以下UDF:

Function SHEETNME(number As Long) As String
    Application.Volatile
    SHEETNME = Sheets(number).Name
End Function

UDF由THIS POST提供。

将此函数粘贴到工作簿附带的模块中。请勿将其放在工作表代码或ThisWorkbook Code中。

然后,您可以在Sheet1上的D2中使用以下公式:

=IFERROR(VLOOKUP(B2,INDIRECT("'" & SHEETNME(2) &"'!A:D"),4,FALSE),TODAY()-1)

然后复制

答案 1 :(得分:0)

嗯,这是我的答案,希望能给予一些帮助。

如果您想使用VBA:

解释,在评论中。

Sub importaData()
    Dim r 'to record the last rows in column b in sheet1
    Dim B 'column number in sheet1
    Dim A 'column number in sheet2
    Dim sht1 As Worksheet 'to store the sheet1
    Dim sht2 As Worksheet 'to store the sheet2
    Dim i
    Dim n 'index
    Dim rngDsht2 As Range 'D column in sheet2
    Dim rngBsht1 As Range 'B column in sheet1
    Dim rngAsht2 As Range 'A column in sheet2
    Dim tmpB 'one cell of column B (of sheet1)
    Dim tmpD 'one cell of column D (OF SHEET2)!
    Dim tmpA 'one cell of column a (of sheet1)

    r = Range("B1").End(xlDown).Row 'As said, found the last row in column B
    B = 2 'Just the number of the columns
    A = 1 'Just the number of the columns

    Set sht1 = Sheets("Sheet1") 'Storing sheets objects into vars
    Set sht2 = Sheets("Sheet2")
    Set rngBsht1 = sht1.Range(Cells(1, 2), Cells(r, B)) 'the range of columns B with the data to compare
    'Set rngDsht1 = Range("B1" & Cells(r, B)) 'You can use this instead.
    sht2.Activate 'Go to sheet2 to set some ranges
    Set rngAsht2 = sht2.Range(Cells(1, 1), Cells(r, 1)) 'set the range of column A. "r" is the range of columns B in sheet1
    Set rngDsht2 = sht2.Range(Cells(1, 4), Cells(r, 4)) 'set the range of column D. "r" is the range of columns B in sheet1
    'Set rngDsht2 = sht2.Range("B1" & Cells(r, B)) 'You can use this instead.
    sht1.Activate 'Go back to sheet1
    n = 0 'Ini the var

    For Each i In rngBsht1 'For each cell into a range of column B
        n = n + 1 'increase the var by one every iteration
        tmpB = i.Value 'store the value of the cell
        tmpA = rngAsht2(i.Row, 1) 'store the value of the cell
        tmpD = rngDsht2(i.Row, 1) 'store the value of the cell

        If tmpB = tmpA Then 'make the comparison
            i.Offset(0, 2).Value = tmpD 'if equal, put the value of column D (sheet2) into cells of columns D in sheet 1
        Else
            i.Offset(0, 2).Value = Date - 1 'if not equal, put in column D sheet 1 the value of yesterday (the date)
        End If
    Next i
End Sub

如果您不想使用公式:

在sheet1的D列中,您需要输入以下公式:

=IF(B1=Sheet2!A1,Sheet2!D1,TODAY()-1)

摘要

In sheet1:

Columns B: with data
Columns D: with no data, we need to put data into D column

In sheet2:
Columns A: With data, used to compare with data in column B in sheet1
Columns D: With data, to take, in case any cell in sheet1.ColumnB and sheet2.columnA were equal, and put into columnD in sheet1...