【问题标题】:Excel 2013: I need Sheet1 Col D = Sheet2 Col D IF Sheet1 Col B = Sheet 2 Col A , else Sheet1 Col D is yesterdays dateExcel 2013:我需要 Sheet1 Col D = Sheet2 Col D IF Sheet1 Col B = Sheet 2 Col A ,否则 Sheet1 Col D 是昨天的日期
【发布时间】:2016-01-12 04:15:24
【问题描述】:

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

我对 VBA 非常陌生,真的不知道从哪里开始!

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

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

【问题讨论】:

  • 要引用 vba 中的第一张表,请使用 Sheets(1) 引用第二张表以便使用 Sheets(2)Sheets() 可以采用索引号,即工作表的顺序,也可以是一个字符串,即工作表名称。
  • 您不需要 VBA。这是一个简单的 IIF 函数调用。
  • @KenWhite - IIf() 不是仅在 VBA 中可用吗?但是 OP,您希望通过检查工作表的索引来执行 If 语句 Sheets("Sheet1").Index
  • 这个LINK 给出了一个可以用INDIRECT 引用的快速UDF,所以你可以做一个简单的IF() 公式。
  • @Bruce:是的,我的错。在 Excel 公式中,它只是 IF()(一个 I)。将我的最后一条评论更改为 这是一个简单的 IF() 函数调用

标签: vba excel


【解决方案1】:

使用以下 UDF:

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

UDF 来自 THIS POST

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

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

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

然后抄下来

【讨论】:

    【解决方案2】:

    嗯,这就是我的回答,希望能给你一些帮助。

    如果你想使用 VBA:

    解释,在 cmets 中。

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

    【讨论】:

      猜你喜欢
      • 2020-12-01
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 2011-09-27
      • 2016-10-25
      • 2016-04-25
      • 1970-01-01
      • 2016-03-16
      相关资源
      最近更新 更多