简体   繁体   English

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

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

Okay, so I recently got into VBA programming and I have been trying to write some code to do the following:好的,所以我最近进入了 VBA 编程,我一直在尝试编写一些代码来执行以下操作:

  • Cycle through a column containing True or False Statements (column "K" in my case)循环浏览包含正确或错误陈述的列(在我的情况下为“K”列)
  • If True then gather the corresponding name (column "C") and create a sheet named accordingly如果为真,则收集相应的名称(“C”列)并创建一个相应命名的工作表

And that's all就这样

So here is the code I wrote:所以这是我写的代码:

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

And it seems to work perfectly fine but it stops after generating the first sheet even if there are other True in the column.它似乎工作得很好,但即使列中有其他 True ,它也会在生成第一张表后停止。

The else case is there for debug purposes as I wanted to see what was happening and it confirms that whenever the if statement is verified, the loop stops. else 案例用于调试目的,因为我想看看发生了什么,它确认只要验证了 if 语句,循环就会停止。

I tried doing the same thing using a "Do Until" loop but it does the same.我尝试使用“直到”循环来做同样的事情,但它的作用是一样的。

What am I missing here?我在这里想念什么? I couldn't find any answers online so any help would be really nice.我在网上找不到任何答案,所以任何帮助都会非常好。

Thanks in advance.提前致谢。

Per Sheets.Add method documentation.Sheets.Add 方法文档。

Creates a new worksheet, chart, or macro sheet.创建新的工作表、图表或宏表。 The new worksheet becomes the active sheet.新工作表成为活动工作表。

You have implicit references to the ActiveSheet so each time you add a new sheet, your code is now referencing the new worksheet.您有对ActiveSheet的隐式引用,因此每次添加新工作表时,您的代码现在都在引用新工作表。

Add some explicit reference to the worksheet you intend to use, such as:为您打算使用的工作表添加一些明确的引用,例如:

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

And

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

Add Worksheets with Names From a List使用列表中的名称添加工作表

  • The following will still raise an error if the worksheet name is invalid.如果工作表名称无效,以下内容仍会引发错误。
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

Figured it out, And now I feel really dumb.想通了,现在我觉得很愚蠢。 all I had to do was to be precise about my cell reference: By simply writing :我所要做的就是准确地了解我的单元格引用:只需编写:

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

It solved everything.它解决了一切。

Try this:尝试这个:

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

End Sub结束子

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

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