I have a macro that generates a table of contents in excel. Basically I have a code that creates an array and im using that array to fill cell content. The code works but its very slow. Im wondering if anyone has a way to speed it up a bit.
I have multiple worksheets named 1,2,3.. etc. the sheets have the same layout and looks like this:
Paragraph | some text | appendix no.
example:
1.0 | Preface | Appendix A1
So i have a code that looks for non empty cells and if the paragraph cell is not empty it adds it to an array (using multiple arrays). Here is my code below, any help will be greatly appreciated!
Option Explicit
Sub inhoudsopgave()
Application.ScreenUpdating = False
Dim SheetNameArray() As Variant
Dim shts As Variant
SheetNameArray = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50")
Dim Paragraaf As Variant
Dim Array2 As Variant
Dim Array3 As Variant
Dim Array4 As Variant
Dim i As Integer
Dim NoBlankSize As Long
Dim sh As Integer
Dim n As Integer
Dim rng As Range
Set rng = Sheets("INHHULP").Range("A2:J100")
rng.ClearContents
NoBlankSize = 0
n = 0
ReDim Paragraaf(0 To 0)
ReDim Array2(0 To 0)
ReDim Array3(0 To 0)
ReDim Array4(0 To 0)
For Each shts In SheetNameArray()
Sheets(shts).Activate
On Error Resume Next
For i = 5 To 70
If Cells(i, 1) <> "" Then
NoBlankSize = NoBlankSize + 1
ReDim Preserve Paragraaf(1 To NoBlankSize)
Paragraaf(NoBlankSize) = Cells(i, 1)
ReDim Preserve Array2(1 To NoBlankSize)
Array2(NoBlankSize) = Cells(i, 4)
ReDim Preserve Array3(1 To NoBlankSize)
Array3(NoBlankSize) = Cells(i, 39)
ReDim Preserve Array4(1 To NoBlankSize)
Array4(NoBlankSize) = Cells(3, 35)
Debug.Print Paragraaf(NoBlankSize)
Debug.Print Array2(NoBlankSize)
Debug.Print Array3(NoBlankSize)
Debug.Print Array3(NoBlankSize)
ElseIf Cells(i, 1) = "" Then
End If
Next i
Next shts
For n = 1 To NoBlankSize
With Sheets("INHHULP")
.Cells(n + 1, 1) = Paragraaf(n)
.Cells(n + 1, 2) = Array2(n)
.Cells(n + 1, 3) = Array3(n)
.Cells(n + 1, 4) = Array4(n)
.Cells(n + 1, 6) = n
End With
Next n
Sheets("A").Activate
Application.ScreenUpdating = True
MsgBox ("Inhoudsopgave is gegenereerd")
End Sub