需要从 Access 中搜索 VBA 创建的 powerpoint table 中的特定文本

Need to search for specific text in powerpoint table created by VBA from Access

所以在创建 table 之后,我有一列 Yes、No 和 N/A,Yes 需要绿色,No 需要红色,N/A 需要灰色。不是整行只是列。

我不是初学者,但我也不是编码专家...这是我的代码:

With .Shapes.AddTable(5, 5, 0, 140, 720, 40)                                       
' NumRows / NumColumns / Left / Top / Width / Height
  .Table.ApplyStyle "{5940675A-B579-460E-94D1-54222C63F5DA}"                  
  ' Table Style for No Style, Table Grid
  .Table.Cell(1, 1).Shape.TextFrame.TextRange.Text = "Text1"
  .Table.Cell(1, 2).Shape.TextFrame.TextRange.Text = "TeXT2"
  .Table.Cell(1, 3).Shape.TextFrame.TextRange.Text = "Text3"
  .Table.Cell(1, 4).Shape.TextFrame.TextRange.Text = "Text4"
  .Table.Cell(1, 5).Shape.TextFrame.TextRange.Text = "Text5"
  R = 1
  With .Table
    For C = 1 To 5
      .Cell(R, C).Shape.TextFrame.TextRange.Font.Size = 8
      .Cell(R, C).Shape.TextFrame.TextRange.Font.Color.RGB = RGB(255, 255, 255)
      .Cell(R, C).Shape.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignCenter
    Next
    R = R + 1
  End With

  .Table.Columns(1).Width = 140
  .Table.Columns(2).Width = 60
  .Table.Columns(3).Width = 60
  .Table.Columns(4).Width = 100
  .Table.Columns(5).Width = 360
  .Fill.BackColor.RGB = RGB(255, 255, 255)
  For Each cl In .Table.Rows(1).Cells
    cl.Shape.Fill.BackColor.RGB = RGB(0, 32, 96)
    cl.Shape.Fill.ForeColor.RGB = RGB(0, 32, 96)
    cl.Shape.TextFrame.TextRange.Characters.Font.Bold = True
    cl.Shape.TextFrame.VerticalAnchor = msoAnchorBottom
  Next cl

  ' Adding data to table ------------------------
  R = 2
  With .Table
    While Not rs26.EOF
      For C = 1 To 5
        .Cell(R, C).Shape.TextFrame.TextRange.Text = Nz(rs26.Fields(C - 1)) 
        .Cell(R, C).Shape.TextFrame.TextRange.Font.Size = 8
        .Cell(R, C).Shape.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignLeft
      Next    'c column
      rs26.MoveNext
      R = R + 1
    Wend
    rs26.Close
  End With

  ' Having trouble getting this part of the code to work ------------------ 
  With .Table
    If .Cell(R, 2).Shape.TextFrame.TextRange.Text = "No" Then
      .Cell(R, 2).Shape.Fill.BackColor.RGB = RGB(255, 0, 0) ' Red
      .Cell(R, 2).Shape.Fill.ForeColor.RGB = RGB(255, 0, 0)
    ElseIf .Cell(R, 2).Shape.TextFrame.TextRange.Text = "Yes" Then
      .Cell(R, 2).Shape.Fill.BackColor.RGB = RGB(146, 208, 80)  ' Green
      .Cell(R, 2).Shape.Fill.ForeColor.RGB = RGB(146, 208, 80)
    Else
      .Cell(R, 2).Shape.Fill.BackColor.RGB = RGB(166, 166, 166) ' Gray
      .Cell(R, 2).Shape.Fill.ForeColor.RGB = RGB(166, 166, 166)
    End If
  End With

End With ' table for chart

我在遇到问题的代码上方添加了注释行。当它到达这部分代码时它停止 运行.

我试过把这部分代码放在几个不同的地方,但还是不行。

希望我已经提供了足够的信息来帮助别人。

感谢您的宝贵时间。

好吧,自从我发布了这个问题后,我就继续研究代码并得出答案。我知道 IF...THEN 会起作用,我只需要输入正确的组合,一旦我这样做了,我就可以使用代码了...请参阅下面的解决方案:

    R = 2
    With .Table
      While Not rs26.EOF
        For C = 1 To 5
         .Cell(R, C).Shape.TextFrame.TextRange.Text = Nz(rs26.Fields(C - 1))
         .Cell(R, C).Shape.TextFrame.TextRange.Font.Size = 8
         .Cell(R, C).Shape.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignLeft
           If Nz(rs26.Fields(2 - 1)) = "Yes" Then
            .Cell(R, 2).Shape.Fill.ForeColor.RGB = RGB(146,208,80)    'Green
            .Cell(R, 2).Shape.Fill.BackColor.RGB = RGB(146, 208, 80)
            .Cell(R, 2).Shape.TextFrame.TextRange.Font.Color.RGB = RGB(255, 255, 255)
            .Cell(R, 2).Shape.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignCenter
           ElseIf Nz(rs26.Fields(2 - 1)) = "No" Then
            .Cell(R, 2).Shape.Fill.ForeColor.RGB = RGB(255, 0, 0)   'Red
            .Cell(R, 2).Shape.Fill.BackColor.RGB = RGB(255, 0, 0)
            .Cell(R, 2).Shape.TextFrame.TextRange.Font.Color.RGB = RGB(255, 255, 255)
            .Cell(R, 2).Shape.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignCenter
           Else
            .Cell(R, 2).Shape.Fill.BackColor.RGB = RGB(166, 166, 166) ' Gray
            .Cell(R, 2).Shape.Fill.ForeColor.RGB = RGB(166, 166, 166)
            .Cell(R, 2).Shape.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignCenter
           End If
        Next    'c column
        rs26.MoveNext
        R = R + 1
      Wend
      rs26.Close
    End With

我基本上是在 FOR 语句中嵌入了 IF...THEN 语句,同时将数据加载到 table。在数据已经在 table.

之后我尝试这样做之前