直到宏中的空行循环

时间:2016-10-24 18:02:18

标签: excel vba macros

我有一个包含一列和多行的数据文件(动态列表)。我想将前28行转换为第一行,将第二行28转换为第二行,依此类推。我希望它一直运行直到找到一个空行。但是,当我运行代码时,它只转换前28行。我一直无法得到结果"直到空白"循环,我无法发现错误。 谢谢你的帮助。

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    '
    Range("A1").Select
    Do
    Range("A1:A28").Select
    Selection.Copy
    Sheets("Sheet2").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone,     SkipBlanks:= _
    False, Transpose:=True
    ActiveCell.Offset(1, 0).Select
    Loop Until ActiveCell.Value = ""
    End Sub

1 个答案:

答案 0 :(得分:1)

宏录制器不会为您执行循环。您需要将Offset拉出循环。否则,完全符合条件:

<强>更新

Option Explicit

Sub CopyPaste()

Dim CopySheet As Worksheet
Dim PasteSheet As Worksheet
Dim MyRange As Range
Dim i As Long
Dim r As Long
Dim wf As WorksheetFunction

Application.ScreenUpdating = False

Set wf = Application.WorksheetFunction
Set CopySheet = ActiveWorkbook.Worksheets("Sheet1")
Set PasteSheet = ActiveWorkbook.Worksheets("Sheet2")
Set MyRange = CopySheet.Range("A1:A28")
r = MyRange.Rows.Count
i = 1

Do Until wf.CountA(MyRange) = 0
    MyRange.Copy
    PasteSheet.Cells(i, 1).PasteSpecial Paste:=xlPasteAll, Transpose:=True
    Set MyRange = MyRange.Offset(r, 0)
    i = i + 1
Loop

Application.ScreenUpdating = True

End Sub