Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 带上限方程的循环_Loops_Excel_Vba - Fatal编程技术网

Loops 带上限方程的循环

Loops 带上限方程的循环,loops,excel,vba,Loops,Excel,Vba,有人知道如何在不同的限制下运行循环吗?每次我需要运行此宏时,我的限制都会按CountAColumn A-2进行更改。有没有办法把它合并到我的循环中 Sub UsedR2() With ActiveSheet Range("A3").Select Selection.End(xlDown).Select ActiveCell.Offset(-1, 0).Select Range(Selection, Selection.End(xlUp)).Select Selection.FillDown R

有人知道如何在不同的限制下运行循环吗?每次我需要运行此宏时,我的限制都会按CountAColumn A-2进行更改。有没有办法把它合并到我的循环中

Sub UsedR2()

With ActiveSheet
Range("A3").Select
Selection.End(xlDown).Select
ActiveCell.Offset(-1, 0).Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Range("A3").Activate
Dim p
For p = 1 To 46

Range("A3").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
ActiveCell.Offset(-1, 0).Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Range("A3").Activate

Next p

Range("D3").Select
Selection.End(xlDown).Select
Selection.EntireRow.Delete
Range("D3").Select
Selection.End(xlDown).Select
Selection.Offset(1, -3).Select
Range(Selection, Selection.End(xlDown)).Delete

End With

End Sub
试试这个:

'First, declare a variable to contain the info
Dim upBound as integer 'or "as Long" depending how many you expect    
upBound = Excel.WorksheetFunction.CountA(Range("A:A"))-2    
For p = 1 To upBound    
'then, the rest of your code
注1:RangA:A是代码所在工作表的范围。如果您希望更明确,您可以将工作表的名称写入工作表。范围A:A或工作表。范围A:A。更准确、更确切地说: 此工作簿。工作表工作表的名称。范围A:A或此工作簿。工作表。范围A:A。 注2:请阅读With语句的用法

我还建议您阅读这篇文章,了解如何避免使用。选择有多种原因。

upBound将保留Excel中的值。WorksheetFunction.CountARangeA:A-2。确保你选择的范围A:A在正确的表格上。然后,我假设你希望For循环的上界是向上的,也许是p=1到46+的上界,我不确定。