even the codes in microsoft's help section don't work

K

kyle

i found this code by pressing screenupdating in the help section of vba. it
doesn't work. it says i is undefined. so i write dim i as integer. then it
says starttime is undefined.

Dim elapsedTime(2)
Application.screenupdating = True

For i = 1 To 2
If i = 2 Then Application.screenupdating = False
startTime = Time
Worksheets("Sheet1").Activate
For Each c In ActiveSheet.Columns
If c.Column Mod 2 = 0 Then
c.Hidden = True
End If
Next c
stopTime = Time
elapsedTime(i) = (stopTime - startTime) * 24 * 60 * 60
Next i
Application.screenupdating = True
MsgBox "Elapsed time, screen updating on: " & elapsedTime(1) & _
" sec." & Chr(13) & _
"Elapsed time, screen updating off: " & elapsedTime(2) & _
" sec."
 
R

Robert Crandal

I pasted this code into a new module and it seemed to work just
fine for me. I don't know what the hell it did, but it seemed to
work. The sheet flickered and the columns changed really quickly...
then it showed a dialog box that stated elapsed time and
screen updating numbers.

The only reason I can think why Excel is telling you that your variables
are undefined is probably because you have a line of code at the
top of your module that says "Option Explicit"??? just my first
guess
 
O

OssieMac

Hi Kyle,

You have Option Explicit at the top of the VBA editor screen. This demands
that all variables be declared. It is the preferred professional way to go.
Try the following with the variables declared.

Sub test()

Dim i As Integer
Dim StartTime As Date
Dim StopTime As Date
Dim c As Range
Dim elapsedTime(2)

Application.ScreenUpdating = True

For i = 1 To 2
If i = 2 Then Application.ScreenUpdating = False
StartTime = Time
Worksheets("Sheet1").Activate
For Each c In ActiveSheet.Columns
If c.Column Mod 2 = 0 Then
c.Hidden = True
End If
Next c
StopTime = Time
elapsedTime(i) = (StopTime - StartTime) * 24 * 60 * 60
Next i
Application.ScreenUpdating = True
MsgBox "Elapsed time, screen updating on: " & elapsedTime(1) & _
" sec." & Chr(13) & _
"Elapsed time, screen updating off: " & elapsedTime(2) & _
" sec."

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top