Vba 正确循环复制和粘贴

Vba 正确循环复制和粘贴,vba,excel,Vba,Excel,基本上我有3列,每列都有自己不规则的空白行(它们实际上是从透视表复制的行标签,3层) 我想让每一个值填充在它们下面的空白行和下一行标签下面,重复所有的复制粘贴程序,直到3个行为止。我希望宏从我选择的活动单元格开始运行,因此我应该运行宏3次,每列一次 我试过我的手,并以下面的代码结束,但它没有给我什么我想要的,任何帮助将不胜感激 Sub LoopCopyPaste() Application.Goto Reference:="LoopCopyPaste" Do Until Selection.

基本上我有3列,每列都有自己不规则的空白行(它们实际上是从透视表复制的行标签,3层)

<>我想让每一个值填充在它们下面的空白行和下一行标签下面,重复所有的复制粘贴程序,直到3个行为止。我希望宏从我选择的活动单元格开始运行,因此我应该运行宏3次,每列一次

我试过我的手,并以下面的代码结束,但它没有给我什么我想要的,任何帮助将不胜感激

Sub LoopCopyPaste()

Application.Goto Reference:="LoopCopyPaste"

Do Until Selection.Value = "(blank)"

    Selection.Copy
    Range(Selection, Selection.End(xlDown)).Select
    ActiveCell.Offset(-1, 0).Select
    ActiveSheet.Paste
    Selection.End(xlDown).Select
    Application.CutCopyMode = False

Loop

End Sub

这里有一个有效的解决方案。这是一个相当有趣的问题,即使我不明白背后的需要

Sub replaceBlanks()

' define variables
Dim column As Integer
Dim row As Integer
Dim lastRow As Integer
Dim previousValue As String
Dim value As String

' stop screen from updating to speed things up
Application.ScreenUpdating = False

' use the active sheet
With ActiveSheet

    ' get the current cell selected and the last row in column selected
    column = ActiveCell.column
    row = ActiveCell.row
    lastRow = .Cells(.Rows.Count, column).End(xlUp).row

    ' set previous value to the first cell
    previousValue = Cells(row, column).value

    ' iterate for every row between selected and last row with data in
    For i = row To lastRow
        ' set value = the content of that cell
        value = Cells(i, column).value
        ' if it contains nothing
        If Len(value) < 1 Then
            ' set the value of cell equal to the previous cell that had something in it
            Cells(i, column).value = previousValue
        ' if it contains something
        Else
            ' update the previous value and move on to next row
            previousValue = value
        End If

    Next i

End With

' update the screen at the end
Application.ScreenUpdating = True

End Sub
子替换空格()
'定义变量
将列设置为整数
将行设置为整数
将最后一行设置为整数
将以前的值设置为字符串
将值设置为字符串
'停止屏幕更新以加快速度
Application.ScreenUpdating=False
'使用活动工作表
使用ActiveSheet
'选择当前单元格并选择列中的最后一行
column=ActiveCell.column
row=ActiveCell.row
lastRow=.Cells(.Rows.Count,column).End(xlUp).row
'将上一个值设置为第一个单元格
previousValue=单元格(行、列)。值
'对选定行和最后一行之间的每一行进行迭代,数据位于
对于i=行到最后一行
'设置值=该单元格的内容
值=单元格(i,列)。值
"如果没有,
如果Len(值)<1,则
'将单元格的值设置为包含内容的前一个单元格的值
单元格(i,列)。值=以前的值
"如果里面有东西,
其他的
'更新上一个值并移到下一行
前一个值=前一个值
如果结束
接下来我
以
'最后更新屏幕
Application.ScreenUpdating=True
端接头