KentBurel
Board Regular
- Joined
- Mar 27, 2020
- Messages
- 68
- Office Version
- 2019
- Platform
- Windows
I've created a monster. I'm still a novice at VBA. I've been working on this project for a few weeks. I have a workbook that contains sheets that I use to build other sheets. The templates contain all the data I need in the final sheets. So I just copy the sheet to a new sheet, rename it, set a few variables and delete the rows I don't need. The template contains 30 rows and the output sheet have a variable number of rows. The most rows of any output sheet is 18. I built the template at 30 row so that I might accomodate environments that are bigger in the future. My code is below. Here is the immediate window of executing the macro.:
Precinct 1 began at 5/22/2020 5:24:52 PM
Precinct 2 began at 5/22/2020 5:25:08 PM
Precinct 3 began at 5/22/2020 5:26:54 PM
Precinct 4 began at 5/22/2020 5:28:14 PM
Precinct 5 began at 5/22/2020 5:29:04 PM
Precinct 6 began at 5/22/2020 5:29:51 PM
Precinct 7 began at 5/22/2020 5:30:16 PM
Precinct 8 began at 5/22/2020 5:31:19 PM
Precinct 9 began at 5/22/2020 5:32:35 PM
Precinct 10 began at 5/22/2020 5:33:21 PM
Precinct 11 began at 5/22/2020 5:34:06 PM
Precinct 12 began at 5/22/2020 5:35:20 PM
Precinct 13 began at 5/22/2020 5:36:15 PM
Precinct 14 began at 5/22/2020 5:37:21 PM
Precinct 15 began at 5/22/2020 5:38:16 PM
Precinct 16 began at 5/22/2020 5:39:13 PM
Precinct 17 began at 5/22/2020 5:40:22 PM
Precinct 18 began at 5/22/2020 5:41:24 PM
Precinct 19 began at 5/22/2020 5:42:24 PM
Precinct 20 began at 5/22/2020 5:42:43 PM
Precinct 21 began at 5/22/2020 5:43:40 PM
Precinct 22 began at 5/22/2020 5:44:40 PM
Precinct 23 began at 5/22/2020 5:45:23 PM
Precinct 24 began at 5/22/2020 5:46:34 PM
Precinct 25 began at 5/22/2020 5:47:21 PM
Precinct 26 began at 5/22/2020 5:48:04 PM
Precinct 27 began at 5/22/2020 5:48:26 PM
Precinct 28 began at 5/22/2020 5:49:45 PM
Precinct 29 began at 5/22/2020 5:50:35 PM
The template and the output sheet are designed to have a VLOOKUP formula in 3 columns. I saw on this forum that dynamic formulas can make the code run slowly so I have removed them for now (before the run that produced these timings.) The template sheet also has automation code in the Worksheet.Change and Worksheet.Selectionchange event handlers but I have removed it until I can figure out the source of my performance issues. This is the only code at the workbook level:
I appreciate your guidance and help.
Precinct 1 began at 5/22/2020 5:24:52 PM
Precinct 2 began at 5/22/2020 5:25:08 PM
Precinct 3 began at 5/22/2020 5:26:54 PM
Precinct 4 began at 5/22/2020 5:28:14 PM
Precinct 5 began at 5/22/2020 5:29:04 PM
Precinct 6 began at 5/22/2020 5:29:51 PM
Precinct 7 began at 5/22/2020 5:30:16 PM
Precinct 8 began at 5/22/2020 5:31:19 PM
Precinct 9 began at 5/22/2020 5:32:35 PM
Precinct 10 began at 5/22/2020 5:33:21 PM
Precinct 11 began at 5/22/2020 5:34:06 PM
Precinct 12 began at 5/22/2020 5:35:20 PM
Precinct 13 began at 5/22/2020 5:36:15 PM
Precinct 14 began at 5/22/2020 5:37:21 PM
Precinct 15 began at 5/22/2020 5:38:16 PM
Precinct 16 began at 5/22/2020 5:39:13 PM
Precinct 17 began at 5/22/2020 5:40:22 PM
Precinct 18 began at 5/22/2020 5:41:24 PM
Precinct 19 began at 5/22/2020 5:42:24 PM
Precinct 20 began at 5/22/2020 5:42:43 PM
Precinct 21 began at 5/22/2020 5:43:40 PM
Precinct 22 began at 5/22/2020 5:44:40 PM
Precinct 23 began at 5/22/2020 5:45:23 PM
Precinct 24 began at 5/22/2020 5:46:34 PM
Precinct 25 began at 5/22/2020 5:47:21 PM
Precinct 26 began at 5/22/2020 5:48:04 PM
Precinct 27 began at 5/22/2020 5:48:26 PM
Precinct 28 began at 5/22/2020 5:49:45 PM
Precinct 29 began at 5/22/2020 5:50:35 PM
VBA Code:
Option Explicit
Sub BuildAllBMDPrecincts()
Dim precincts() As Variant
Dim precinct As String
Dim precinctLocation As String
Dim i As Integer
Dim last As Integer
Dim number_of_BMDs As Integer
Dim outRange As Range
Dim sheetName As String
Dim firstRow As Integer
Dim lastRow As Integer
'Turn off calculations for a bit
Application.Calculation = xlManual
' Turn off screen updating
Application.ScreenUpdating = False
' Turn off events
Application.EnableEvents = False
precincts = Range("Precincts")
last = UBound(precincts)
For i = 1 To last
Debug.Print "Precinct " & i & " began at " & Now
precinct = precincts(i, 1)
precinctLocation = precincts(i, 2)
number_of_BMDs = precincts(i, 5)
DoEvents
Application.StatusBar = "Creating precinct sheet " & i & " of " & last
sheetName = precinct & "-B"
Sheets("BMD Precinct Template").Copy After:=Sheets(Sheets.Count)
Sheets("BMD Precinct Template (2)").Name = sheetName
Sheets(sheetName).PageSetup.LeftHeader = "Polling Place: " & precinct
Sheets(sheetName).Visible = True
' Now set the counters that control the stoplight
Sheets(sheetName).Range("W8").Value = number_of_BMDs * 21 ' Columns A-U times number of rows
firstRow = 4 ' The table starts on row 4
lastRow = 4 + number_of_BMDs - 1 ' The last Row
Sheets(sheetName).Range("W9").FormulaR1C1 = "=COUNTA(R4C1:R" & lastRow & "C2)" & _
"+COUNTIF(R4C3:R" & lastRow & "C17,UNICHAR(254))" & _
"+COUNTIF(R4C16:R" & lastRow & "C16,0)" & _
"+COUNTA(R4C18:R" & lastRow & "C21)"
Sheets(sheetName).Range("A1").Value = precinct
Sheets(sheetName).Range("B1").Value = precinctLocation
' Now only show the rows and columns that are relevant. Don't show or print others.
Sheets(sheetName).Columns("W").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.EntireColumn.Hidden = True
Sheets(sheetName).Range("A" & lastRow + 1 & ":U33").ClearContents
Rows(lastRow + 1).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.EntireRow.Hidden = True
' Now protect the new precinct BMD sheet
Sheets(sheetName).Protect
Next i
Application.StatusBar = "Recalculating workbook."
' Restore automatic calculations
Application.Calculation = xlAutomatic
Application.StatusBar = False
Application.ScreenUpdating = True
' Turn on events
Application.EnableEvents = True
End Sub
The template and the output sheet are designed to have a VLOOKUP formula in 3 columns. I saw on this forum that dynamic formulas can make the code run slowly so I have removed them for now (before the run that produced these timings.) The template sheet also has automation code in the Worksheet.Change and Worksheet.Selectionchange event handlers but I have removed it until I can figure out the source of my performance issues. This is the only code at the workbook level:
VBA Code:
Option Explicit
Sub Workbook_open()
DisplayConstantsHelp.Show
End Sub
I appreciate your guidance and help.