有谁知道如何运行具有不同限制的循环?每次我需要运行此宏时,我的限制都会由CountA(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
答案 0 :(得分:0)
试试这个:
'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:Rang(A:A)
是您的代码所在的工作表范围。如果您想更明确,可以编写Worksheet("The_Name_of_the_Worksheet").Range("A:A")
或Worksheet(Sheet#).Range("A:A")
。更加精确和确定:
Thisworkbook.Worksheet("The_Name_of_the_Worksheet").Range("A:A")
或Thisworkbook.Worksheet(Sheet#).Range("A:A")
。
注意#2:阅读this以了解With
声明的用法。
我还建议您阅读此post,了解如何避免因多种原因使用.Select
。