【发布时间】:2017-07-28 11:23:23
【问题描述】:
抱歉,如果这看起来像重复,但我正在寻找可能是常见问题的特定解决方案。
我有以下代码可以从一系列单元格中添加行:
Sub Insert_Matrix_Rows()
Dim Lr As Integer, Fr As Integer
Fr = Columns("B").Find(What:="User R", After:=Range("B9")).Row 'Searching row of "User R" header
Lr = Range("B" & Fr).End(xlDown).Row 'Searching last row in Risk table
Rows(Lr + 1).Insert Shift:=xlDown 'Inserting new row
'Cells(Lr + 1, "B") = Cells(Lr, "B") + 1 'Adding a sequential number
Rows(Lr).Copy 'Copying format of last row
Rows(Lr + 1).PasteSpecial Paste:=xlPasteFormats 'Pasting format to new row
Application.CutCopyMode = False 'Deactivating copy mode
Cells(Lr + 1, "C").Select
End Sub
我将它分配为宏,它可以复制第 9 行,并将其粘贴到该行下方。本质上,我想为列复制此过程。这样在生成列时将包含上述代码可能生成的任何行,反之亦然。
我试图翻译代码以适用于列,但我遇到了错误(类型不匹配):
Sub Insert_Matrix_Columns()
Dim Lc As Integer, Fc As Integer
Fc = Columns("D").Find(What:="User C", After:=Range("E6")).Column 'Searching row of "User C" header
Lc = Range("E" & Fr).End(xlRight).Column 'Searching last row in Risk table
Columns(Lc + 1).Insert Shift:=xlRight 'Inserting new row
'Cells(Lr + 1, "B") = Cells(Lr, "B") + 1 'Adding a sequential number
Columns(Lc).Copy 'Copying format of last row
Columns(Lc + 1).PasteSpecial Paste:=xlPasteFormats 'Pasting format to new row
Application.CutCopyMode = False 'Deactivating copy mode
Cells(Lc + 1, "E").Select
End Sub
【问题讨论】:
-
错误发生在哪一行?
-
错误发生在:Sub Insert_Matrix_Columns(),包含“Fc - ...”的行
-
o 认为您的评论中缺少某些内容
-
以上评论已更正^
-
您在 D 列中搜索,在 E 列中的单元格之后