【问题标题】:Excel VBA to copy multiple rows and insert to next row depending on button clickExcel VBA复制多行并根据按钮单击插入下一行
【发布时间】:2019-02-28 03:38:31
【问题描述】:

我目前面临一个问题,即在单击几下按钮后我无法正确复制和插入行。我想要实现的逻辑是复制除标题之外的每一行并附加到下一行。请参考提供的图片。

Default Template *Before button click

After inserting from last row

Continue to insert normally

Eventually will reach to this point

以下是我的代码,一团糟。我是VBA新手,请指导我,谢谢。

Sub bt_add()

Dim a1 As Integer
Dim a2 As Integer
Dim a3 As Integer
Dim a4 As Integer
Dim a5 As Integer
Dim a6 As Integer
Dim a7 As Integer
Dim a8 As Integer
Dim a9 As Integer
Dim a10 As Integer
Dim a11 As Integer
Dim a12 As Integer
Dim n As Integer
Dim s As Integer

Static clicked As Integer

a1 = 2
a2 = 3
a3 = 6
a4 = 7
a5 = 10
a6 = 11
a7 = 14
a8 = 15
a9 = 18
a10 = 19
a11 = 22
a12 = 23

n = clicked
s = clicked + 1

If clicked = 0 Then
    a1 = 2
    a2 = 3
    a3 = 6
    a4 = 7
    a5 = 10
    a6 = 11
    a7 = 14
    a8 = 15
    a9 = 18
    a10 = 19
    a11 = 22
    a12 = 23

    clicked = clicked + 1
Else
    If clicked >= 2 Then
        a1 = a1 + n
        a2 = a2 + n
        a3 = a2 * 2
        a4 = a2 * 2 + 1
        a5 = a5 + n + 1 + s
        a6 = a6 + n + 1 + s
        a7 = a7 + n + 3 + s
        a8 = a8 + n + 3 + s
        a9 = a9 + n + 5 + s
        a10 = a10 + n + 5 + s
        a11 = a11 + n + 7 + s
        a12 = a12 + n + 7 + s

        clicked = clicked + 1
    Else
        a1 = a1 + n
        a2 = a2 + n
        a3 = a2 * 2
        a4 = a2 * 2 + 1
        a5 = a5 + n + 2
        a6 = a6 + n + 2
        a7 = a7 + n + 3
        a8 = a8 + n + 3
        a9 = a9 + n + 4
        a10 = a10 + n + 4
        a11 = a11 + n + 5
        a12 = a12 + n + 5

        clicked = clicked + 1
    End If

End If



'MsgBox a1 & ", " & a2 & ", " & a3 & ", " & a4 & ", " & a5 & ", " & a6 & ", " & a7 & ", " & a8 & ", " & a9 & ", " & a10 & ", " & a11 & ", " & a12 & ", " & n & ", " & s

Selection.Copy
Rows(a1).EntireRow.Copy
Rows(a2).Select
Selection.Insert Shift:=xlDown
Rows(a3).EntireRow.Copy
Rows(a4).Select
Selection.Insert Shift:=xlDown
Rows(a5).EntireRow.Copy
Rows(a6).Select
Selection.Insert Shift:=xlDown
Rows(a7).EntireRow.Copy
Rows(a8).Select
Selection.Insert Shift:=xlDown
Rows(a9).EntireRow.Copy
Rows(a10).Select
Selection.Insert Shift:=xlDown
Rows(a11).EntireRow.Copy
Rows(a12).Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False

End Sub

【问题讨论】:

  • 仅供参考:您的第一个变量块具有 a12 作为整数 - 其余的都是变体,因为类型未声明。这是初学者中很常见的错误 - 我自己在第一次开始 VBA 时就陷入了这个陷阱
  • 问题:1)你如何识别它是否是一个标题?如果文本是粗体? 2)如果您不在标题行中,每次单击按钮时,宏将插入一行并在 A 到 D 列中从前一行复制值(保留那些是公式的值)? 3)这个过程是单行还是你想一次“生成”新行?
  • @urdearboy 嘿,感谢您的指出。我改了,结果还是一样。
  • 我不明白你到底想做什么。您只想单击一个按钮,每次它在每个标题表下插入一个新行?这是唯一的标准吗?您突出显示的单元格是怎么回事?
  • @RicardoDiaz 感谢您的回复,Q1:您可以忽略那个,因为在第一次插入后,变量中的数字会增加,这将跳过标题。 Q2:您必须选择数据行并插入到将忽略标题行的下一行。 Q3:如果可能的话,我希望它一次全部生成,因为从我的代码中,它会逐行生成。

标签: excel vba


【解决方案1】:

如果这是您要查找的内容*,该宏假定您将始终在每个小节之间只保留一个空白行。这将复制每个小节中的最后一行并将其插入下方,同时在下一个表格之前保留下方的 1 个空白行。


Option Explicit

Sub InsertRows()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim i As Long, LR As Long

LR = ws.Range("A" & ws.Rows.Count).End(xlUp).Offset(1).Row

'Application.ScreenUpdating = False
    For i = LR To 1 Step -1
        If ws.Range("A" & i) = "" Then
            ws.Range("A" & i + 1).EntireRow.Insert
            ws.Range("A" & i - 1).EntireRow.Copy ws.Range("A" & i)
        End If
    Next i
'Application.ScreenUpdating = True

End Sub

【讨论】:

  • 你还需要格式化你的按钮,现在用行展开 lol
  • @undearboy 非常感谢,这正是我要找的。我的代码太糟糕了..哈哈哈
猜你喜欢
  • 2017-12-18
  • 1970-01-01
  • 2015-09-24
  • 2011-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多