Do Until循环Excel VBA

时间:2017-08-25 17:30:07

标签: excel vba excel-vba

我正在尝试在Excel VBA中创建“Do Until”循环,它复制“SQL”工作表A列中填充的每个值(从单元格A2开始),将值粘贴到“Home”的单元格“A2”中工作表,运行已存在的“PDF”宏,为每个填充的值继续此过程,并在“SQL”工作表的列A中没有更多值时结束。我对VBA很新,并尝试过来自其他帖子/博客的东西。任何帮助将不胜感激。

Dim rngMyRange As Range, rngCell As Range
Dim sht As Worksheet
Dim LastRow As Long
Dim SheetName As String


With Worksheets("SQL")
Set rngMyRange = .Range(.Range("a2"), .Range("A1000").End(x1up))

Do Until IsEmpty(Cells(rowNo, 1))
  For Each rngCell In rngMyRange
  rngCell.Cells.Select
  Selection.Copy
  Sheets("HOME").Select
  Range("A2").Select
  ActiveSheet.Paste
  Application.CutCopyMode = False
  Application.Run "'PDF Generator.xlsm'!PDF"

Loop
End Sub

2 个答案:

答案 0 :(得分:2)

你在Do循环中有一个for循环。删除Do循环。

您永远不会使用End With

同时避免使用.Select.Activate,这会降低代码速度:

Dim rngMyRange As Range, rngCell As Range


With Worksheets("SQL")
    Set rngMyRange = .Range(.Range("a2"), .Range("A1048576").End(xlup))
End With

For Each rngCell In rngMyRange
  rngCell.Copy
  Sheets("HOME").Range("A2").PasteSpecial
  Application.CutCopyMode = False
  Application.Run "'PDF Generator.xlsm'!PDF"
Next rngCell

答案 1 :(得分:1)

在这里添加一个点:

Do Until IsEmpty(.Cells(rowNo, 1))

没有圆点就会引用活动表。