0

Have a worksheet with 1000+ rows with a pattern of text on first row and then have 3 blank rows. I need to copy the text from column B first row down through the next 3 rows. Then I need to designate those 3 rows as a group of the initial row. I have got the below code to work for the first two lines, but then it stops on line 3, I get a Compile error: Syntax error. I know from having recorded the initial macro from doing the steps that the beginning of line 3, Rows, is needed, but can't seem to find the right syntax to tell the system that the rows to be grouped are the next 3.

Edit: Added missing code sample.

Selection.AutoFill Destination:=Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(3, 0)), Type:=xlFillDefault
Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(3, 0)).Select
Rows(ActiveCell.Row 1,ActiveCell.Row 3).Select
Range(ActiveCell.Offset(3, -1)).Activate
Selection.Rows.Group
Range(ActiveCell.Offset(4, 0)).Select

Second Edit: Added in recorded macro from doing the keystrokes to accomplish the grouping of the next 3 rows. Starting with having just copied the first row to the other 3 rows, the keystrokes were: 1 down, Shift/Space, Shift/2 down, Alt/dgg. Macro:

Rows("1037:1039").Select
Range("A1039").Activate
Selection.Rows.Group
4

1 回答 1

0

通过从开发人员选项卡的代码部分激活“使用相对引用”记录宏,我记录了一个工作模型来解决我的问题。该宏执行以下操作: - 将活动单元格内容向下复制 3 行 - 选择整个 3 行 - 使这 3 行成为初始行的一组 - 转到下一个分组的下一个主行

########### 录制宏的初始脚本###########################
Selection.AutoFill Destination:=ActiveCell.Range("A1:A4"), Type:=xlFillDefault
ActiveCell.Range("A1:A4").Select
ActiveCell.Offset(1, 0).Rows("1:3").EntireRow.Select
ActiveCell.Offset(3, 0).Range("A1").Activate
Selection.Rows.Group
ActiveCell.Offset(1, 0).Range("A1").Select

在测试生成的宏时,我发现了一个通过消除第 4 行来解决的问题。所以成功的宏代码如下:

%%%%%%%%%%%%%%%%%%%%%%% 最终脚本
选择。自动填充目标:=ActiveCell.Range("A1:A4"),类型:=xlFillDefault
ActiveCell.Range ("A1:A4").Select
ActiveCell.Offset(1, 0).Rows("1:3").EntireRow.Select
Selection.Rows.Group
ActiveCell.Offset(3, 1).Range("A1") 。选择

于 2013-09-10T16:33:43.057 回答