【问题标题】:copy row content in excel vba在excel vba中复制行内容
【发布时间】:2017-05-20 04:28:24
【问题描述】:

我从未做过任何 excel vba 程序。我想编写一个 vba 代码,它允许我将包含第一列上的按钮的行复制到新行。

我可以复制行内容,但不能复制按钮。请帮忙

我的excel如下:-

按钮||数据||数据||数据

图片链接: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

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    这样试试吧……

    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
    

    【讨论】:

    • 什么不起作用?我对其进行了测试,如果 Lr 计算正确,它会复制带有 A 列中按钮的行。
    • 对我来说不是复制按钮。并添加一个空白行,即使我没有清除这些列的内容。不知道发生了什么
    • 您正在使用 A 列中的数据搜索最后一行。请确保如前所述正确计算 Lr。根据您的示例数据,如果 Lr 为 5,那么只有代码会复制 Row5。在计算 Lr 的行下方放置一行 MsgBox Lr,看看是否得到要复制的正确行。
    • 我想问题是我无法将按钮放在列中。例如,这里我从 A5 尝试,但那个按钮不存在 A5。不知道如何将此按钮添加到特定列。你能帮助我吗?我在开发人员>设计模式中绘制该按钮
    • 在移动或调整按钮大小之前按住 Alt 键,然后移动或调整按钮大小,它将完全适合单元格。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-16
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多