You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求改写VBA工作表生成代码,添加忽略特定行的判断逻辑

Modified VBA Code to Skip Rows with "Reversed" (Case-Insensitive) in Column C

I’ve adjusted your existing VBA code to skip rows where Column C contains "Reversed" (regardless of uppercase/lowercase). The key tweak is reordering the check to prioritize the "Reversed" condition first—this makes the code more efficient by avoiding unnecessary sheet existence checks for rows we want to skip entirely.

Here’s the updated code:

Option Explicit
Sub SheetsFromTemplate()
    Dim wsMASTER As Worksheet, wsTEMP As Worksheet, wasVISIBLE As Boolean
    Dim shNAMES As Range, Nm As Range
    
    With ThisWorkbook
        ' Keep focus within this workbook
        Set wsTEMP = .Sheets("Template") ' Sheet to use as a template
        wasVISIBLE = (wsTEMP.Visible = xlSheetVisible) ' Track template's original visibility
        If Not wasVISIBLE Then wsTEMP.Visible = xlSheetVisible ' Make template visible temporarily
        
        Set wsMASTER = .Sheets("Paste L3 here") ' Sheet containing names and statuses
        ' Define range of names (Column A starting at A4, constants only)
        Set shNAMES = wsMASTER.Range("A4:A" & Rows.Count).SpecialCells(xlConstants)
        
        Application.ScreenUpdating = False ' Speed up macro execution
        
        For Each Nm In shNAMES ' Process each row one by one
            ' First check: skip if Column C (offset 2 columns right) is "Reversed" (case-insensitive)
            If UCase$(Nm.Offset(0, 2).Value) = "REVERSED" Then
                GoTo SkipRow ' Jump to next row immediately
            End If
            
            ' Only proceed if the sheet doesn't already exist
            If Not Evaluate("ISREF('" & CStr(Nm.Text) & "'!A1)") Then
                wsTEMP.Copy After:=.Sheets(.Sheets.Count) ' Create new sheet from template
                ActiveSheet.Name = CStr(Nm.Text) ' Rename the new sheet
            End If
            
SkipRow:
        Next Nm
        
        wsMASTER.Activate ' Return to the master sheet
        If Not wasVISIBLE Then wsTEMP.Visible = xlSheetHidden ' Restore template's original visibility
        Application.ScreenUpdating = True ' Re-enable screen updates
    End With
    
    MsgBox "All valid sheets created" ' Update message to reflect skipped rows
End Sub

Key Changes Explained:

  • Prioritized "Reversed" Check: Added a check at the start of the loop that immediately skips the row if Column C contains "Reversed" (using UCase$ to make the comparison case-insensitive).
  • Clean Skip Mechanism: Used a SkipRow label to jump directly to the next iteration when the "Reversed" condition is met, keeping the code flow readable.
  • Clarified Final Message: Updated the MsgBox text to reflect that only non-"Reversed" rows were processed.
  • Preserved Core Features: Kept all original functionality like handling template visibility, optimizing screen updates, and avoiding duplicate sheet creation.

内容的提问来源于stack exchange,提问作者Charlotte879

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:19:44