ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   For Loop only executes with F8 (https://www.excelbanter.com/excel-programming/374799-loop-only-executes-f8.html)

Sisilla[_2_]

For Loop only executes with F8
 
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


Ron de Bruin

For Loop only executes with F8
 
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
ResetUsedRange wks


--
Regards Ron de Bruin
http://www.rondebruin.nl



"Sisilla" wrote in message ups.com...
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




Sisilla[_2_]

For Loop only executes with F8
 
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

Ron de Bruin wrote:
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
ResetUsedRange wks


--
Regards Ron de Bruin
http://www.rondebruin.nl



"Sisilla" wrote in message ups.com...
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




All times are GMT +1. The time now is 10:58 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com