sub不会循环执行第二个if语句vba
真的希望有人能帮我。所以我有下面的代码。独立代码本身工作正常,但在执行脚本时,它只循环第一个条件。我想要它做的是每次循环遍历所有代码。我想这是我遗漏的一件小事,但我似乎找不到解决办法sub不会循环执行第二个if语句vba,vba,excel,Vba,Excel,真的希望有人能帮我。所以我有下面的代码。独立代码本身工作正常,但在执行脚本时,它只循环第一个条件。我想要它做的是每次循环遍历所有代码。我想这是我遗漏的一件小事,但我似乎找不到解决办法 Sub Copypre() Dim i As Integer Dim n As Integer For i = 2 To 10 'Checks the number of entries in the "Pre" table, to make sure that there are no spaces bet
Sub Copypre()
Dim i As Integer
Dim n As Integer
For i = 2 To 10
'Checks the number of entries in the "Pre" table, to make sure that there are no spaces between the lines
On Error Resume Next
n = Worksheets("Pre").Range("A2:A6000").Cells.SpecialCells(xlCellTypeConstants).Count
If n = Null Then
n = i
'Goes through the different sheets to find all "pre" values and paste them in the "Pre" sheet
If ThisWorkbook.Sheets("273").Range("A" & i).Value = "Pre" Then
Range(Cells(i, 1), Cells(i, 3)).Select
Selection.Copy
Sheets("Pre").Select
Range("A" & n).Select
ActiveSheet.Paste
Sheets("2736").Select
End If
End If
Next i
End Sub
循环可以很快地消耗长数据列的时间,我怀疑您的代码被严重修改了。尝试此替代方法,将块复制到目标工作表
Sub Copypre()
With Sheets("273").Cells(1, 1).CurrentRegion
.AutoFilter
.Columns(1).AutoFilter field:=1, Criteria1:="=Pre"
If CBool(Application.Subtotal(103, .Offset(1, 0))) Then
.Offset(1, 0).Resize(, 3).Copy _
Destination:=Sheets("Pre").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
End If
.AutoFilter
End With
End Sub
所有这些都可以在没有单个变量声明的情况下完成
增编:
至于您原来的问题,如果“pre”/copy/paste部分嵌套在if n=Null
中,则整个if“pre”/copy/paste
部分只有在n=Null
为真时才能到达。如果没有.SpecialCells(xlCellTypeConstants)
可计数,则n将被分配其默认值(例如0)。零不等于Null,因此永远不满足条件。要检查代码,请添加以下行
On Error Resume Next
n = Worksheets("Pre").Range("A2:A6000").Cells.SpecialCells(xlCellTypeConstants).Count
Debug.Print "n is " & n
运行它之后,用
Ctrl+G
打开即时窗口。如果Pre中没有非公式值!A2:A6000您应该看到n为0
您的代码有几个问题,但主要问题可能是如果n=Null
将永远不会为真,因为整数不能为Null
。如果n=0,您可以将其更改为
需要考虑的几件事:
错误处理:始终返回正常的错误处理,并尽快将错误转到0
。这样,您就会知道(假设工作簿中没有工作表“2736”
),您的代码正在尝试选择一个不存在的工作表
范围参数:在使用范围
(和单元格
)参数时,如果不指定工作表,请小心。当您在所选的不同工作表之间切换回第四个工作表和第四个工作表时,您可能会丢失对范围
从哪个工作表返回数据的跟踪。考虑声明每个工作表,然后复制您的范围如下:
Dim w273 As Worksheet
Set w273 = ThisWorkbook.Sheets("273")
w273.Range(w273.Cells(i, 1), w273.Cells(i, 3)).Copy
谢谢你的建议。无效的伎俩奏效了!我是VBA的新手,所以很高兴能从专家那里得到一些提示和技巧。我将尝试使代码更简单,正如Jeeped提到的,因为这不是很优雅。关于工作表,我完全能理解其中的困惑,我也解决了这个问题。它现在可以工作了,如下所示:
Sub Copypre()
Dim i As Integer
Dim n As Integer
For i = 2 To 5000
' Checks the number of entries in the "Pre" table, to make sure that there are no spaces between the lines
On Error Resume Next
n = Worksheets("Pre").Range("A2:A6000").Cells.SpecialCells(xlCellTypeConstants).Count
' Goes through the different sheets to find all pre values and paste them in the "Pre" sheet
If ThisWorkbook.Sheets("2736").Range("A" & i).Value = "Pre" Then
Sheets("2736").Select
Sheets("2736").Range(Cells(i, 1), Cells(i, 3)).Select
Selection.Copy
Sheets("Pre").Select
Range("A" & (n + 2)).Select
ActiveSheet.Paste
Sheets("2736").Select
Sheets("2736").Select
Range(Cells(i, 5), Cells(i, 6)).Select
Selection.Copy
Sheets("Pre").Select
Range("E" & (n + 2)).Select
ActiveSheet.Paste
Sheets("2736").Select
End If
Next i
End Sub
不清楚源工作表的名称是273还是2736。很难找出您的问题所在,但如果n=null,您肯定有一些不好的代码<代码>n定义为整数,不能为空。如果n=0,则应将测试更改为
。也许这会解决您的问题。删除错误时的,然后继续下一步。。。你有错误吗?如果n=Null,则
应为n=0
?@pnuts=Yes,工作表名称存在一些歧义。我推断这只不过是一个打字错误,但在这一点上我可能大错特错。