繁体   English   中英

无法理解为什么 VBA 在验证 If 语句时退出我的 For-Next 循环

[英]Can't figure why VBA exits my For-Next Loop when a If Statement is Verified

好的,所以我最近进入了 VBA 编程,我一直在尝试编写一些代码来执行以下操作:

  • 循环浏览包含正确或错误陈述的列(在我的情况下为“K”列)
  • 如果为真,则收集相应的名称(“C”列)并创建一个相应命名的工作表

就这样

所以这是我写的代码:

Sub Generate_Attribute_Table()

    Dim LastRow As Long
    Dim i As Integer
    Dim Nom As String

    LastRow = Range("A1").End(xlDown).Row
    
    For i = 2 To LastRow
        
        If (Cells(i, "K").Value) Then
            
            Nom = Worksheets(1).Cells(i, "C").Value
            ActiveWorkbook.Sheets.Add(After:=Worksheets(Sheets.Count)).Name = Nom
            
        Else
        
            Cells(i, "K").Select
            
        End If
        
    Next i

End Sub

它似乎工作得很好,但即使列中有其他 True ,它也会在生成第一张表后停止。

else 案例用于调试目的,因为我想看看发生了什么,它确认只要验证了 if 语句,循环就会停止。

我尝试使用“直到”循环来做同样的事情,但它的作用是一样的。

我在这里想念什么? 我在网上找不到任何答案,所以任何帮助都会非常好。

提前致谢。

Sheets.Add 方法文档。

创建新的工作表、图表或宏表。 新工作表成为活动工作表。

您有对ActiveSheet的隐式引用,因此每次添加新工作表时,您的代码现在都在引用新工作表。

为您打算使用的工作表添加一些明确的引用,例如:

LastRow = Sheets("Sheet1").Range("A1").End(xlDown).Row 

Sheets("Sheet1").Cells(i, "K").Value

使用列表中的名称添加工作表

  • 如果工作表名称无效,以下内容仍会引发错误。
Option Explicit

Sub Generate_Attribute_Table()

    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    Dim sws As Worksheet: Set sws = wb.Worksheets(1) ' wb.Worksheets("Sheet1")
    ' This (usually preferred) way will allow empty cells in the column.
    Dim LastRow As Long: LastRow = sws.Cells(sws.Rows.Count, "A").End(xlUp).Row
    
    Dim dsh As Object
    Dim Nom As String
    Dim i As Long
    
    For i = 2 To LastRow
        ' If you use '(sws.Cells(i, "K").Value)', an error will occur
        ' if there is not a boolean in the cell.
        If sws.Cells(i, "K").Value = True Then
            Nom = sws.Cells(i, "C").Value
            ' Attempt to create a reference to the sheet named 'Nom'.
            Set dsh = Nothing
            On Error Resume Next
            Set dsh = wb.Sheets(Nom)
            On Error GoTo 0
            ' Test for existence.
            If dsh Is Nothing Then ' A sheet named 'Nom' doesn't exist.
                wb.Worksheets.Add(After:=wb.Sheets(wb.Sheets.Count)).Name = Nom
            'Else ' A sheet named 'Nom' already exists.
            End If
        End If
    Next i

End Sub

想通了,现在我觉得很愚蠢。 我所要做的就是准确地了解我的单元格引用:只需编写:

If (ActiveWorkbook.Sheets(1).Cells(i, "K").Value) Then

它解决了一切。

尝试这个:

Sub Generate_Attribute_Table()
Dim LastRow As Long
Dim i As Integer
Dim Nom As String
Dim Sh As Worksheet

Set Sh = ActiveWorkbook.Worksheets("Sheet1")
LastRow = Range("A1").End(xlDown).row

For i = 2 To LastRow
    If Sh.Cells(i, 11).Value = True Then
        Nom = Sh.Cells(i, 3).Value
        ActiveWorkbook.Sheets.Add(After:=Worksheets(Sheets.count)).Name = Nom
    Else
        'Cells(i, 11).Select 'Commented because I don't see why you'd need it
    End If
Next i

结束子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM