我在MS Word中有一个表,该表是2列乘1000行。我做了一个宏,按顺序
没有任何条件可以在运行时将其弄乱(例如,空单元格等),我不需要代码来查找特定的东西,只需要重复它即可。
我需要在现有代码中添加些什么,以使其重复到文档末尾(具体是1000次)。
我看过Excel的代码,但是我没有看过在Word中浏览表格的代码。
Selection.MoveDown Unit:=wdParagraph, Count:=2, Extend:=wdExtend
Selection.InsertRowsBelow 1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=2, Extend:=wdExtend
Selection.Cells.Merge
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
Selection.Cut
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.PasteAndFormat (wdPasteDefault)
Selection.Font.Size = 4
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Cut
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.PasteAndFormat (wdPasteDefault)
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Font.Size = 3
Selection.TypeText Text:="Unique specimen identifier not a property tag"
Selection.MoveDown Unit:=wdLine, Count:=2
Selection.MoveLeft Unit:=wdCharacter, Count:=2, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1
我希望能够运行一次代码,并使其重复执行直到到达文档末尾为止,并且如果可能的话,可以重复执行1000次。
我希望这不是太“广泛”,因为我有代码并在问一个特定的问题。
答案 0 :(得分:1)
您可以通过将其包装到For...Next
循环中来重复指令集,这在VBA应用程序中是相当标准的。这段代码创建了一个循环和一个'index'变量,然后告诉它精确地遍历该索引1000次。
For index As Integer = 1 To 1000
Selection.MoveDown Unit:=wdParagraph, Count:=2, Extend:=wdExtend
Selection.InsertRowsBelow 1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=2, Extend:=wdExtend
Selection.Cells.Merge
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
Selection.Cut
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.PasteAndFormat (wdPasteDefault)
Selection.Font.Size = 4
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Cut
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.PasteAndFormat (wdPasteDefault)
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Font.Size = 3
Selection.TypeText Text:="Unique specimen identifier not a property tag"
Selection.MoveDown Unit:=wdLine, Count:=2
Selection.MoveLeft Unit:=wdCharacter, Count:=2, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Next
如果您需要无限次地循环某事(例如,当满足特定条件时),则可以使用Do...While
或Do...Until
循环。