我有一些表格在文档中,大致看起来像这样:
| 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
但它们根本不能按照我想要的方式工作,我似乎无法使它们正常运行。
有人可以为我提供解决方案吗?
myCell.Text = "Thing #" + t
,会尝试将字符串转换为数字,然后进行数字加法运算。因此,"Thing #" + t
会导致“类型不匹配”的错误。应该使用连接符号“&”来代替:myCell.Text = "Thing #" & t
。 - rskarSet myCell = .Tables(t).Cell(1,1).Range myCell.Text = "Thing #" + t
正确的应该是:Set myCell = .Tables(t).Cell(1,1) myCell.Range.Text = "Thing #" & t
同时,myCell
也应该被声明为一个范围。 - J. Garth