Ryflex
Ryflex

Reputation: 5769

VBA right way of clearing/resetting an excel document?

So, I found a bit of code on a website a few weeks ago that worked perfectly for what I need but I am just wondering what exactly the following bit of code does because to me it makes more sense to clear the document using MainSheet1.Cells.Clear and then just do a series of Range("Z1").Value = "Group" for example

Why has this person chose to make an array and fill it in that way...?

Sub CLEAR_DOCUMENT()

    '
    Dim headers(1 To 4, 1 To 15) As Variant

    MainSheet1.Cells.Clear

    headers(4, 1) = "Group"
    headers(4, 2) = "Look"
    headers(4, 3) = "Time"
    headers(4, 4) = "Tie"
    headers(4, 5) = "Type"
    headers(4, 6) = "Name"
    headers(4, 7) = "Num"
    headers(1, 8) = "School 2"
    headers(1, 9) = "Group 2"
    headers(2, 8) = "*"
    headers(2, 9) = ">0"
    headers(1, 11) = "Time"
    headers(1, 12) = "Language"
    headers(1, 13) = "School 1"
    headers(1, 14) = "Age"
    headers(1, 15) = "Eng"

    MainSheet1.Range("Z1:AN4").Value = headers

    Application.EnableEvents = True

End Sub

Upvotes: 1

Views: 44

Answers (1)

Gary's Student
Gary's Student

Reputation: 96753

The code you posted is very very slightly faster than filling the cells one at a time.

Upvotes: 3

Related Questions