2017-07-28 43 views
0

道歉,如果這看起來像是重複的,但是我正在尋找一個可能是常見問題的特定解決方案。從一系列單元格中添加列

我有下面的代碼從一個單元格區域添加行:

enter image description here

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 
+0

在哪一行出現錯誤? – avb

+0

錯誤發生在:Sub Insert_Matrix_Columns(),該行包含「Fc - ...」 – Idrawthings

+0

o認爲您的評論中缺少somehing – avb

回答

0

更換

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代替xlShiftToRightxlToRight分別是-4152,-4161,-4161

+0

我得到了「400」的錯誤。 – Idrawthings

+0

我試過調試,錯誤不在線上:「Lc = ...」,我注意到了Fr的錯字,我已經將它改爲Fc,儘管我仍然收到一個錯誤:「應用程序定義或對象定義錯誤「 – Idrawthings

+0

avb,如果您嘗試複製表格,則可能會看到我遇到的問題。 – Idrawthings

相關問題