0

让我们想象一下,在ListObject表格行中,我想找到一些字符串或一些模式,如果找到,我想格式化整行。我的示例可能是通过 VBA 和 ACE OLEDB 从记录集中转储到 excelListObject表中,我想对其进行格式化:

col1 | col2      | col3          | col4
-----+-----------+---------------+----------
1.   | empty     | AAA           | 
2.   | empty     | AAA001value   | value
3.   | empty     | AAA002value   | value
4.   |  Total    | desc          | value
5.   | empty     | BBB           | 
6.   | empty     | BBB001value   | value
7.   | empty     | BBB002value   | value
8.   |  Total    | desc          | value

例如我想

  1. 将表格中出现 Total 一词的整行加粗。
  2. 在 ??? 上方插入一行,表示一个组的三个字母字符串出现。

我可能会设法提出在标准非ListObject表上运行的代码并为整个工作表添加行,但我不知道如何搜索和更改ListObject表中的行。

4

1 回答 1

1

在 VBA 代码中,您可以使用like比较运算符或使用Instr()以下示例的函数来查找字符串模式:

If (strCell like "*Total*") Then
    ' True for case-sensitive strCell string like "Total", "aTotalb" and not for "total"
End If

If (strCell like "*[A-Z][A-Z][A-Z]00[1-2]*") Then
    ' True for strCell strings with 3 Upper-Case letters And "00" after that and also "1" or "2" after that like "AAA001" or "ZBC002"
    If (InStr(1, strCell, "AAA") > 0) Then
        ' With this InStr() you can remove 'ZBC002' 
    End If  
    ...
End If

If (strCell like "*[*][*][A-Z][A-Z][A-Z][*][*]*"
    ' True for strCell like "a**AAA**b" or "asd**ZCB**"
    If (InStr(1, strCell, "AAA") > 0) Then
        ' With this InStr() you can remove "asd**ZCB**"
    End If  
End If

为了突出显示,您可以使用如下代码:

Sheets(1).Rows(1).Font.Bold = True
于 2015-04-13T09:26:06.140 回答