ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Loop through range, Autofit height (https://www.excelbanter.com/excel-programming/434204-loop-through-range-autofit-height.html)

John[_140_]

Loop through range, Autofit height
 
This is a sub I made to loop through a range and autofit row height of each
row:

Sub AutoHeight()
Dim c As Range
For Each c In Sheet1.Range("AutoFit")
Rows.AutoFit
Next c
End Sub

The macro just keeps running and running. What;'s wrong with my sub?



JLGWhiz[_2_]

Loop through range, Autofit height
 
What are your parameters for the range named "AutoFit", which, by the way,
is a bad practice to use reserved words for names and variables.


"John" wrote in message
...
This is a sub I made to loop through a range and autofit row height of
each row:

Sub AutoHeight()
Dim c As Range
For Each c In Sheet1.Range("AutoFit")
Rows.AutoFit
Next c
End Sub

The macro just keeps running and running. What;'s wrong with my sub?




RB Smissaert

Loop through range, Autofit height
 
Firstly, make sure you always have Option Explicit at the top of every
module and form.
You will see why when you try to run the code you posted with that done.

Then try code like this:

Sub AutoHeight()

Dim r As Long

With Sheets("Sheet1").Range("AutoFit")
For r = 1 To .Rows.Count
.Rows(r).AutoFit
Next r
End With

End Sub


RBS


"John" wrote in message
...
This is a sub I made to loop through a range and autofit row height of
each row:

Sub AutoHeight()
Dim c As Range
For Each c In Sheet1.Range("AutoFit")
Rows.AutoFit
Next c
End Sub

The macro just keeps running and running. What;'s wrong with my sub?



JLGWhiz[_2_]

Loop through range, Autofit height
 
This might work better if the named range is defined.

Sub AutoHeight()
Sheets("Sheet1".Range("AutoFit").Rows.AutoFit
End Sub





"John" wrote in message
...
This is a sub I made to loop through a range and autofit row height of
each row:

Sub AutoHeight()
Dim c As Range
For Each c In Sheet1.Range("AutoFit")
Rows.AutoFit
Next c
End Sub

The macro just keeps running and running. What;'s wrong with my sub?




RB Smissaert

Loop through range, Autofit height
 
Sheets("Sheet1".Range("AutoFit").Rows.AutoFit

Ah, yes, of course no need for the loop as I posted.

RBS


"JLGWhiz" wrote in message
...
This might work better if the named range is defined.

Sub AutoHeight()
Sheets("Sheet1".Range("AutoFit").Rows.AutoFit
End Sub





"John" wrote in message
...
This is a sub I made to loop through a range and autofit row height of
each row:

Sub AutoHeight()
Dim c As Range
For Each c In Sheet1.Range("AutoFit")
Rows.AutoFit
Next c
End Sub

The macro just keeps running and running. What;'s wrong with my sub?





JLGWhiz[_2_]

Loop through range, Autofit height
 

works even better with the parentheses in place:

Sub AutoHeight()
Sheets("Sheet1").Range("AutoFit").Rows.AutoFit
End Sub



"RB Smissaert" wrote in message
...
Sheets("Sheet1".Range("AutoFit").Rows.AutoFit


Ah, yes, of course no need for the loop as I posted.

RBS


"JLGWhiz" wrote in message
...
This might work better if the named range is defined.

Sub AutoHeight()
Sheets("Sheet1".Range("AutoFit").Rows.AutoFit
End Sub





"John" wrote in message
...
This is a sub I made to loop through a range and autofit row height of
each row:

Sub AutoHeight()
Dim c As Range
For Each c In Sheet1.Range("AutoFit")
Rows.AutoFit
Next c
End Sub

The macro just keeps running and running. What;'s wrong with my sub?







RB Smissaert

Loop through range, Autofit height
 
Ah, yes, infinite better!

RBS


"JLGWhiz" wrote in message
...

works even better with the parentheses in place:

Sub AutoHeight()
Sheets("Sheet1").Range("AutoFit").Rows.AutoFit
End Sub



"RB Smissaert" wrote in message
...
Sheets("Sheet1".Range("AutoFit").Rows.AutoFit


Ah, yes, of course no need for the loop as I posted.

RBS


"JLGWhiz" wrote in message
...
This might work better if the named range is defined.

Sub AutoHeight()
Sheets("Sheet1".Range("AutoFit").Rows.AutoFit
End Sub





"John" wrote in message
...
This is a sub I made to loop through a range and autofit row height of
each row:

Sub AutoHeight()
Dim c As Range
For Each c In Sheet1.Range("AutoFit")
Rows.AutoFit
Next c
End Sub

The macro just keeps running and running. What;'s wrong with my sub?









All times are GMT +1. The time now is 07:35 AM.

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