SpreadSheet printing when it is not supposed to VBA Macros

I have been working on this VBA project for quite some time and I am quite close to getting it done, the VBA code is supposed to look at every sheet in the Excel document and look for the named ranges that:

  1. First it looks for names that have total and their value is > 0, if they fulfill those 2 requisites, it adds the ws.name to sheetsArrayA and prints the sheet.
  2. Second, it checks the sheets that are in the array A, and checks if they have a name that contains paytype and its value = “CHECK”, if it does, it prints it again.

It prints all the correct sheets besides ML. I keep getting the ML Company to print once when it is not supposed to, the rest are good. they all contain a named range total and greater than 0, and some do have a paytype named range with its value = check, the ML company does not have a total, and therefore it is not greater than 0 either, the only thing it does have is a paytype that equals check, but that should not print it as total is more important). I do not why it is checking that sheet and its value when it does not have a total named range (not greater than 0 obviously) and it does have a paytype named range and equals check, but if it does not fullfill the first requirement, then it is supposed to ignore the paytype loop.

heres the code:

``Option Explicit
Global Mark As Boolean, Unmark As Boolean, MagicPrint As Boolean

Sub PrintWorksheets()
    SpecialPrinting False, False, True
End Sub
Sub MarkWorksheets()
    SpecialPrinting True, False, False
End Sub
Sub UnmarkWorksheet()
    SpecialPrinting False, True, False
End Sub

Sub SpecialPrinting(Optional Mark As Boolean = False, Optional Unmark As Boolean = False, Optional MagicPrint As Boolean = False)
    
    Dim ws As Worksheet
    Dim SheetsArrayA(), SheetsArrayB()
    Dim i, j, sheetCount As Long
    Dim nm As Name
    Dim wsName As String
    Dim isInArrayA As Boolean
    Dim x As Integer
    Dim HomeSheet As Worksheet
    
    Set HomeSheet = ActiveSheet
    sheetCount = ThisWorkbook.Sheets.Count
    
    ' Assign them as arrays.
    ReDim SheetsArrayA(1 To sheetCount)
    ReDim SheetsArrayB(1 To sheetCount)
    
    ' Array A
        On Error Resume Next
        For i = 1 To sheetCount
            wsName = ThisWorkbook.Worksheets(i).Name
            Set ws = ThisWorkbook.Worksheets(wsName)
            For Each nm In Names
                If Trim(Right(UCase(nm.Name), 5)) = "TOTAL" Then
                    SheetsArrayA(i) = ws.Name
                    
                    If MagicPrint And ThisWorkbook.Names(nm.Name).RefersToRange(1, 1) > 0 And _
                     GetWSName(nm) = ws.Name Then
                        ThisWorkbook.Worksheets(i).PrintOut
                        Debug.Print "A: " & GetWSName(nm)
                    End If
                    
                    If Mark Then
                        With Worksheets(i).Range(nm).AddComment
                         .Visible = False
                         .Text "Total Field for Print Option: " & nm.Name
                        End With
                    End If
                    
                    If Unmark Then Worksheets(i).Range(nm).Comment.Delete
                End If
            Next nm
        Next i
    ' Array B
        For j = 1 To sheetCount
            x = 0
            x = WorksheetFunction.IfError(WorksheetFunction.Match(ThisWorkbook.Worksheets(j).Name, SheetsArrayA, 0), 0)
            If x > 0 Then
                For Each nm In Names
                    If Trim(Right(UCase(nm.Name), 7)) = "PAYTYPE" Then
                             
                        If MagicPrint And UCase(ThisWorkbook.Names(nm.Name).RefersToRange(1, 1)) = "CHECK" And _
                         GetWSName(nm) = ThisWorkbook.Worksheets(x).Name Then
                            Debug.Print "Named Range: " & nm.Name
                            ThisWorkbook.Worksheets(j).PrintOut
                            Debug.Print "B: " & GetWSName(nm)
                        End If
                
                        If Mark Then
                            With Worksheets(j).Range(nm).AddComment
                                .Visible = False
                                .Text "PayType Field for Print Option: " & nm.Name
                            End With
                        End If
                        
                        If Unmark Then Worksheets(j).Range(nm).Comment.Delete
                    End If
                Next nm
            End If
        Next j
        On Error GoTo 0
    HomeSheet.Activate
End Sub

Function GetWSName(wsData) As String
Dim s As Integer
Dim e As Integer
    If Left(wsData, 2) = "='" Then
        s = InStr(1, wsData, "'") + 1
        e = InStr(s, wsData, "'")
    Else
        s = 2
        e = InStr(2, wsData, "!")
    End If
    GetWSName = Mid(wsData, s, e - s)
End Function`
`

New contributor

user25639564 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật