簡體   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