For Loop only executes with F8

S

Sisilla

Hello All,

In my call to the following subroutine, the For Loop is supposed to
iterate 51 times. It does this fine when I run it with "step into," but
does not work when executed any other way.

Sub HideEmptyColumns(wks As Worksheet, lastcolumn As Long, firstrow As
Long)
'Hides all empty columns in the wks worksheet between columns 1 and
lastcolumn
'if the last non-empty row is firstrow
Dim col As Long
Dim LastRow As Long
ResetUsedRange wks
For col = 1 To lastcolumn
LastRow = (wks.Cells(Rows.Count, col).End(xlUp).row)
If LastRow = firstrow Then
Columns(col).Hidden = True
End If
Next col
End Sub

Might Excel not be giving the code enough time to execute? If so, how
do I give the bit of code more time? I appreciate any help.

Thanks,
Sisilla
 
R

Ron de Bruin

Hi Sisilla

Working for me correct if the activesheet is the wks sheet

Change
Columns(col).Hidden = True to
wks.Columns(col).Hidden = True

Do you see a error ?
If you not use
 
S

Sisilla

Thank you, Ron. I'll test out your solution. I turned off
ScreenUpdating before the loop and turned it back on again after the
loop, and it seems to be working. I greatly appreciate your help.

Sisilla
 

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