跨多个表的Excel条件

时间:2016-07-07 09:35:23

标签: excel vba excel-vba excel-formula

我有一张相当复杂的Excel表格。但我需要一个简单的解决方案.. 我的工作簿中有2张纸,其中包含带有contactdetails的公司和带有事件的公司(由这些公司托管)

// Company Sheet
╔═══════════╦══════════════╗
║ Company   ║ Contact      ║
╠═══════════╬══════════════╣
║ Dominoes  ║ John Doe     ║
╚═══════════╩══════════════╝
// Event sheet
╔═══════════╦══════════╦═══════╦═════════════╗
║ Events    ║ Company  ║ Payed ║    Date     ║
╠═══════════╬══════════╬═══════╬═════════════╣
║Pizza Party║ Dominoes ║  Yes  ║  06-07-2016 ║
╠═══════════╬══════════╬═══════╬═════════════╣
║Pizza Swim ║ Dominoes ║  No   ║  07-07-2016 ║
╠═══════════╬══════════╬═══════╬═════════════╣
║FREE PIZZA ║ Dominoes ║  No   ║  08-07-2016 ║
╚═══════════╩══════════╩═══════╩═════════════╝

现在使用条件格式我想为公司工作表着色并将公司标记为payernot payer

查询必须执行以下操作:

  • 由公司循环完成每个活动
  • 检查公司是否支付了账单
  • 如果日期未到期,公司不必支付

因此,对于此示例,Pizza Party已付款且到期日期,因此会返回FALSE

Pizza Swim事件未付款且 到期。这将返回TRUE

FREE PIZZA事件未付款且日期尚未到期。这也会以FALSE

返回

如果公司没有支付账单(TRUE),公司将被标记为红色 如果公司支付了账单(FALSE),公司将被标记为绿色

如果一家公司有多个事件,结果TRUE将否决所有其他付费帐单(FALSE

如何做到这一点?

我用功能尝试过它:

=IF(Table2[Company]=A2;IF(Table2[Payed]="Ja";FALSE;IF(Table2[Date]<Today();TRUE;FALSE));TRUE)

2 个答案:

答案 0 :(得分:1)

我确定有一种更聪明的方法可以使用条件格式和功能来做到这一点,但我更多的是一个VBA家伙,所以我写了一个快速的子应该做你想要的一些温和的调整

Sub MarkNonPayers()
    Dim CompanySheet As Worksheet, EventSheet As Worksheet, Cell As Range, UnpayingCompany As String, FoundCell As Range
    Set CompanySheet = ThisWorkbook.Worksheets("Company Sheet")
    Set EventSheet = ThisWorkbook.Worksheets("Event Sheet")
    UnpayingCompany = "><"
    For Each Cell In EventSheet.Range(EventSheet.Cells(2,1),EventSheet.Cells(EventSheet.UsedRange.Rows.Count,1))

         If EventSheet.Cells(Cell.Row,"M").Value = "No" And CDate(EventSheet.Cells(Cell.Row,"E").Value) - Date(Now) < 0 Then
             Set FoundCell = CompanySheet.UsedRange.Find(EventSheet.Cells(Cell.Row,"A").Value, LookAt:=xlWhole)
           CompanySheet.Range(CompanySheet.Cells(FoundCell.Row, "A"), CompanySheet.Cells(FoundCell.Row,"K").Interior.Color = RGB(255,0,0)
         UnpayingCompany = UnpayingCompany & EventSheet.Cells(Cell.Row,"A").Value & "><"
         End If

    Next Cell

    For Each Cell In CompanySheet.Range(CompanySheet.Cells(2,1),CompanySheet.Cells(CompanySheet.UsedRange.Rows.Count,1))
        If Len(UnpayingCompany) - Len(Replace(UnpayingCompany,"<" & Cell.Value & ">", "")) = 0 AND Not IsEmpty(Cell.Value) AND Cell.Value <> "Total" Then
            CompanySheet.Range(CompanySheet.Cells(Cell.Row,"A"), CompanySheet.Cells(Cell.Row, "K")).Interior.Color = RGB(0,255,0)
        End If
    Next Cell
End Sub

答案 1 :(得分:0)

您是否只需要以下内容作为条件格式规则:

=AND(C2="No",D2<=TODAY())