[英]Adding Column from a Range of Cells
道歉,如果這看起來像重復的話,但是我正在尋找針對常見問題的特定解決方案。
我有以下代碼從一系列單元格中添加行:
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
更換
Fc = Columns("D").Find(What:="User C", After:=Range("E6")).Column
同
Fc = Rows(6).Find(What:="User C", After:=Range("E6")).Column
編輯:這是使用的工作代碼(我轉載了表格):
Sub Insert_Matrix_Columns()
Dim lastCol As Range
Set lastCol = Rows(6).Find(What:="User C").End(xlToRight).EntireColumn
Columns(lastCol.Column + 1).Insert Shift:=xlShiftToRight
lastCol.Copy
Columns(lastCol.Column + 1).PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
End Sub
您的代碼的主要問題是使用xlRight
而不是xlShiftToRight
和xlToRight
分別是-4152,-4161,-4161
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.