2017-05-19 131 views
1

我從來沒有做過任何excel vba程序。我想寫一個vba代碼,這將允許我複製包含第一列上的按鈕的行到新行。在excel vba中複製行內容

我能夠複製行內容但無法複製按鈕。請幫助

我的Excel是象下面這樣: -

按鈕|| DATA || DATA || DATA

圖像鏈接:https://ibb.co/cijovv

這段代碼到目前爲止,我已經寫了:=

Dim Lr As Integer 
Dim newLr As Integer 
Dim lim As String 

Lr = range("A" & Rows.Count).End(xlUp).Row 'Searching last row in column A 
newLr = Lr + 1 
lim = "B" & newLr & ":" + "D" & newLr 

Rows(Lr).Copy 
Rows(newLr).Insert Shift:=x1Down 
range(lim).ClearContents 

回答

0

嘗試像這樣...

Dim Lr As Integer 
Dim newLr As Integer 
Dim lim As String 

Lr = Range("A" & Rows.Count).End(xlUp).Row 'Searching last row in column A 
newLr = Lr + 1 
lim = "B" & newLr & ":" + "D" & newLr 
Application.CopyObjectsWithCells = True 
Rows(Lr).Copy 
Rows(newLr).Insert Shift:=xlDown 
Range(lim).ClearContents 
Application.CopyObjectsWithCells = False 

如果在A5的ActiveX命令按鈕,您可以嘗試這樣的事情...

Dim Lr As Integer 
Dim newLr As Integer 
Dim lim As String 
Dim oleObj As OLEObject, oleCopy As OLEObject 

Lr = Range("A" & Rows.Count).End(xlUp).Row 'Searching last row in column A 
newLr = Lr + 1 
lim = "B" & newLr & ":" + "D" & newLr 
Rows(Lr).Copy 
Rows(newLr).Insert Shift:=xlDown 
Application.CutCopyMode = 0 
For Each oleObj In ActiveSheet.OLEObjects 
    If oleObj.TopLeftCell.Row = Lr Then 
     Set oleCopy = oleObj.Duplicate 
     With oleCopy 
      .Left = Range("A" & newLr).Left 
      .Top = Range("A" & newLr).Top 
     End With 
     oleObj.Delete 
     Exit For 
    End If 
Next oleObj 
Range(lim).ClearContents 
+0

沒有這個也沒有工作:( – Rajarshi

+0

什麼是不工作?我對它進行了測試,並使用列A中的按鈕複製該行,如果Lr計算正確。 – sktneer

+0

對我來說不是複製按鈕。並添加一個空行,即使我不清除這些列的內容。不確定發生了什麼 – Rajarshi