我目前正在开发一个 VBA 项目,该项目根据任务的缩进间距将序列号分配给任务列表。目前,如果步骤之间有 0 或 1 个空行,它将起作用,但如果步骤之间有 2 个或更多空行,它将停止。我希望这适用于任意数量的空白行。
在VBA代码中,我目前有:
Loop through cells with project tasks and generate WBS Do While Not (IsEmpty(Cells(r, 2)) And IsEmpty(Cells(r + 1, 2)))
我可以通过添加以下内容来跳过更多行:
And IsEmpty(Cells(r + 3, 2)) And IsEmpty(Cells(r + 4, 2)) And IsEmpty(Cells(r + 5, 2)).....
我正在寻找一种方法,让它跳过 1 到 100 任意数量的空白行。
当前VBA代码:
'Hide page breaks and disable screen updating (speeds up processing)
Application.ScreenUpdating = False
ActiveSheet.DisplayPageBreaks = False
'Format WBS column as text (so zeros are not truncated)
ActiveSheet.Range("A:A").NumberFormat = "@"
ActiveSheet.Range("A:A").HorizontalAlignment = xlRight
ActiveSheet.Range("2:2").HorizontalAlignment = xlLeft
Dim r As Long 'Row counter
Dim depth As Long 'How many "decimal" places for each task
Dim wbsarray() As Long 'Master array holds counters for each WBS level
Dim basenum As Long 'Whole number sequencing variable
Dim wbs As String 'The WBS string for each task
Dim aloop As Long 'General purpose For/Next loop counter
r = 3 'Starting row
basenum = 0 'Initialize whole numbers
ReDim wbsarray(0 To 0) As Long 'Initialize WBS ennumeration array
'Loop through cells with project tasks and generate WBS
Do While Not (IsEmpty(Cells(r, 2)) And IsEmpty(Cells(r + 1, 2)))
'Ignore empty tasks in column B
If Cells(r, 2) <> "" Then
'Skip hidden rows
If Rows(r).EntireRow.Hidden = False Then
'Get indentation level of task in col B
depth = Cells(r, 2).IndentLevel
我尝试将变量“n”定义为所有值 1 到 100,这样它基本上会执行 r+1、r+2、r+ 3、r+4...无需全部输入。
Do While Not (IsEmpty(Cells(r, 2)) And IsEmpty(Cells(r + n, 2)))
我无法让变量达到这个范围的值并在工作簿中工作。
我在工作簿中创建了一个值范围 1 到 100,我已将变量定义为数组,但这仍然需要输入所有数字。寻找一个简单干净的修复方法 Do While Not (IsEmpty(Cells(r, 2)) 和 IsEmpty(Cells(r + n, 2))) n 为 1 到 100 之间的数字。
确定数据的最后一行,然后检查值以跳过空白行。
另外:请确保验证所有工作表参考
Option Explicit
Sub Example()
Dim thisWS As Worksheet
Set thisWS = ActiveSheet
With thisWS
Dim lastRow As Long
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
Dim r As Long
For r = 3 To lastRow
'--- ignore empty tasks in column B
If .Cells(r, 2) <> "" Then
'--- skip hidden rows
If Not .Rows(r).EntireRow.Hidden Then
'--- do stuff
End If
End If
Next r
End With
End Sub