如何循环Excel VBA宏,每次向下移动列

时间:2016-01-28 21:40:28

标签: excel vba excel-vba

在向下移动A1:A100时,我希望我的宏循环直到它遇到A列中的空白单元格,而不是让代码在每行调用一个宏。

目前,我有这样的事情:

Sub test()
Application.ScreenUpdating = False
Range("A2").Select
ActiveCell.FormulaR1C1 = "='Single Column'!R[-1]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!RC"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[1]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[2]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[3]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[4]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[5]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[6]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[7]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[8]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[9]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[10]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[11]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[12]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[13]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[14]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[15]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[16]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[17]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[18]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[19]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[20]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[21]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[22]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[23]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[24]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[25]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[26]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[27]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[28]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[29]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[30]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[31]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[32]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[33]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[34]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[35]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[36]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[37]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[38]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[39]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[40]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[41]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[42]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[43]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[44]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[45]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[46]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[47]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[48]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[49]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[50]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[51]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[52]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[53]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[54]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[55]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[56]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[57]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[58]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[59]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[60]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[61]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[62]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[63]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[64]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[65]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[66]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[67]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[68]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[69]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[70]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[71]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[72]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[73]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[74]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[75]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[76]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[77]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[78]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[79]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[80]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[71]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[72]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[73]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[74]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[75]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[76]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[77]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[78]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[79]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[80]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[81]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[82]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[83]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[84]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[85]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[86]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[87]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[88]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[89]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[90]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[91]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[92]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[93]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[94]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[95]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[96]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[97]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!R[98]C"
Call Search    
 End Sub

循环最好,但是当我尝试以下代码时,我得到一个:

  

"运行时错误' 1004':应用程序定义或对象定义错误":

Sub test()
Application.ScreenUpdating = False
Dim i As Integer
i = 1
Range("A2").Select
ActiveCell.FormulaR1C1 = "='Single Column'!R[-1]C"
Call Search
ActiveCell.FormulaR1C1 = "='Single Column'!RC"
Call Search
Do While i < 98
ActiveCell.FormulaR1C1 = "='Single Columns'!R[i]C"
Call Search
i = i + 1
Loop
End Sub

搜索宏查看文本记录以查看哪些记录包含我们正在搜索的单词,然后将该记录中的一个范围从一个工作表复制/粘贴到另一个工作表中。 代码:

Sub Search()

Sheets("Formula").Select
ActiveSheet.Range("$A$1:$H$2505").AutoFilter Field:=7
ActiveSheet.Range("$A$1:$H$2505").AutoFilter Field:=7, Criteria1:=Array("1", "2", "3"), Operator:=xlFilterValues
Sheets("Search").Select

Range("a2:i2").Copy

Sheets("Search").Range("a" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues

Application.CutCopyMode = False
Range("A2").Select

End Sub

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

您可以创建一个简单的循环,例如此示例。使用指数的计数器。我真正看到的唯一问题是你永远不会改变你的活跃单元格,因此这可能是一个无限循环......

Sub test()
Application.ScreenUpdating = False
Dim i As Integer
i = -1
Range("A2").Select
Do While ActiveCell.Value <> ""
Dim iValue As String
    If i = 0 Then
        iValue = ""
    Else
        iValue = "[" & CStr(i) & "]"
    End If
ActiveCell.FormulaR1C1 = "='Single Column'!R" & iValue & "C"
Call Search

i = i + 1
If i > 100 Then Exit Sub
Loop
End Sub