Excel 2013:我需要Sheet1 Col D = Sheet2 Col D如果Sheet1 Col B = Sheet 2 Col A,否则Sheet1 Col D是昨天的date

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

我是VBA新手,真的不知道从哪里开始呢!

编辑:Sheet1是完整列表和Sheet2是每日例外的列表,或只是那些不报告。 所以我需要它来查看所有的ColumnA,并将其与所有的ColumnB进行比较。 Sheet2 ColumnD是最后的报告date。

对此我非常陌生,所有我试过的都是比较基本的配方。 如果我不需要它总是参考第二张纸,那就完成了! 我感谢所有的build议!

使用以下UDF:

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

UDF来自这篇文章 。

将此function粘贴到连接到工作簿的模块中。 不要把它放在工作表代码或ThisWorkbook代码。

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

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

然后复制下来

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

如果你想使用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列中,您需要input以下公式:

=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...