gpt4 book ai didi

vba - Word VBA : Macro to change cells in selection, 并创建表格的汇总表?

转载 作者:行者123 更新时间:2023-12-04 08:21:29 26 4
gpt4 key购买 nike

我在文档中有一堆表格,大致如下所示:

| Thing     |   Title   |
|-----------|:---------:|
| Info | A, B, C. |
| Score | Foo |
| More Info | Long Text |
| Proof | Blah |

Figure 1
<Screenshot of Proof>

我想让它看起来像这样(左上角单元格中的数字):
| Thing #1  |       Title       |
|-----------|:-----------------:|
| Info | A, B, C. |
| Score | Foo |
| More Info | Long Text |
| Proof | Blah <Screenshot> |

但是,文档中有很多表格,我只想使用“选择范围内”的表格。

简而言之:我必须选择一个选择中的所有表格并按顺序编号。
我还想为这些表格制作一个表格,如下所示:
| Number | Title | Score | Number of CSV's in Info |
|--------|:-----:|-------|-------------------------|
| 1 | Thing | Foo | 3 |
| ... | ... | ... | ... |
| ... | ... | ... | ... |
| ... | ... | ... | ... |

这是我到目前为止所拥有的:

编号表:
Sub NumberTablesSelection()
Dim t As Integer

Dim myRange as Range
Set myRange = Selection.Range

With myRange
For t = 1 To .Tables.Count
Set myCell = .Tables(t).Cell(1,1).Range
myCell.Text = "Thing #" + t
Next t
End With
End Sub

表(带信息):
Sub TableOfThings()
Dim t As Integer

Dim myRange as Range
Set myRange = Selection.Range

myTable = Tables.Add(Range:=tableLocation, NumRows:=1, NumColumns:=4)
myTable.Cell(1,1).Range.Text = "Number"
myTable.Cell(1,2).Range.Text = "Title"
myTable.Cell(1,3).Range.Text = "Score"
myTable.Cell(1,4).Range.Text = "Instances"

With myRange
For t = 1 To .Tables.Count

Set Title = .Tables(t).Cell(1,2).Range
Set Instances = .Tables(t).Cell(2,2).Range
Set Score = .Tables(t).Cell(3,2).Range

Set NewRow = myTable.Rows.Add
NewRow.Cells(1).Range.Text = t
NewRow.Cells(2).Range.Text = Title
NewRow.Cells(3).Range.Text = Score
NewRow.Cells(4).Range.Text = Instances
End With
End Sub

但是他们完全没有按照我希望的方式工作,而且我似乎无法让他们工作。

有人可以为我提供解决方案吗?

最佳答案

我们需要考虑以下方面的宏才能按要求运行:

  • 不能使用按钮或其他对象来调用宏,因为这会有效地更改选择。相反,它可以通过 Alt + F8 或分配给宏
  • 的快捷键运行。
  • 选择必须是连续的。因此,如果有 4 个表,仅选择表#1 和 3 将不起作用。它应该更像表#1 到 3。

  • 有了这个和一些小的调整,下面复制的修改后的代码应该可以工作。
    Option Explicit
    Sub NumberTablesSelection()
    Dim t As Integer, myRange, myCell As Range
    Set myRange = Selection.Range
    With myRange
    For t = 1 To .Tables.Count
    Set myCell = .Tables(t).Cell(1, 1).Range
    myCell.Text = "Thing #" & t
    Next t
    End With
    End Sub
    Sub TableOfThings()
    Dim t As Integer, myRange As Range, myTable As Table, NewRow As Row, Title As String, Instances As Integer, Score As String
    Set myRange = Selection.Range
    Selection.EndKey Unit:=wdStory
    Set myTable = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=1, NumColumns:=4)
    With myTable
    .Style = "Table Grid"
    .Rows(1).Shading.BackgroundPatternColor = -603917569
    .Cell(1, 1).Range.Text = "Number"
    .Cell(1, 2).Range.Text = "Title"
    .Cell(1, 3).Range.Text = "Score"
    .Cell(1, 4).Range.Text = "Instances"
    End With
    With myRange
    For t = 1 To .Tables.Count
    Title = .Tables(t).Cell(1, 2).Range
    Instances = UBound(Split(.Tables(t).Cell(2, 2).Range, ",")) + 1
    Score = .Tables(t).Cell(3, 2).Range
    Set NewRow = myTable.Rows.Add
    With NewRow
    .Shading.BackgroundPatternColor = wdColorAutomatic
    .Cells(1).Range.Text = t
    .Cells(2).Range.Text = txtClean(Title)
    .Cells(3).Range.Text = txtClean(Score)
    .Cells(4).Range.Text = Instances
    End With
    Next t
    End With
    End Sub
    Function txtClean(txt As String) As String
    txt = Replace(txt, Chr(7), "")
    txt = Replace(txt, Chr(13), "")
    txt = Replace(txt, Chr(11), "")
    txtClean = txt
    End Function

    编辑:列 Instances 的结果已更改为“实例数”,而不是显示原始值。

    关于vba - Word VBA : Macro to change cells in selection, 并创建表格的汇总表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44684892/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com