【问题标题】:How to find (and edit) specific row with an Excel ListObject Table (former List)如何使用 Excel ListObject 表(前列表)查找(和编辑)特定行
【发布时间】:2015-06-18 10:47:54
【问题描述】:

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

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 表中搜索和更改行。

【问题讨论】:

  • 我编辑了您的示例数据,请确认我在正确的列中编辑了您的数据;)。

标签: vba excel listobject excel-tables


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 2014-11-02
    • 1970-01-01
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多