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:
- 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.
- 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`
`
user25639564 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.