VBA to set row height not working

bisel

Board Regular
Joined
Jan 4, 2010
Messages
249
Office Version
  1. 365
Platform
  1. Windows
Hello All,

Try as I might, I cannot get this macro to execute without an error ...

VBA Code:
Private Sub worksheet_activate()

Dim c As Range
Application.ScreenUpdating = False

' Set Row heights

For Each c In Range("rowheights")
[INDENT]If c.Value = 0 Then ' if zero, then set rowheight to autofit[/INDENT]
[INDENT=2]c.EntireRow.AutoFit[/INDENT]
[INDENT]Else[/INDENT]
[INDENT=2]c.EntireRow.RowHeight = c.Value[/INDENT]
[INDENT]End If[/INDENT]
Next c

End Sub

The macro errors when trying to set the row height = c.value

If I use this statement instead ...
VBA Code:
c.EntireRow.RowHeight = 50

The row height gets set to 50. But I am unable to set the row height equal to the cell's value.

Anyone know what I am doing wrong?

Thanks,

Steve
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try this:
(highlight the range (selection) first)
(yellow is new/changed text from your sample)
1702662317017.png
 
Upvote 0
Hi to all.
@bisel , you didn't let us know what error your macro throws and also you didn't state what sort of values you have in range "rowheights", are you sure that all cells contain 'true' numbers ?
 
Upvote 0
Solution
Hi to all.
@bisel , you didn't let us know what error your macro throws and also you didn't state what sort of values you have in range "rowheights", are you sure that all cells contain 'true' numbers ?

I found my error. One of the cells in the range was formatted as text. After ensuring all the cell values were indeed numeric, the macro works as I would expect.

Thanks,

Steve
 
Upvote 0
If your issue could happen again you can use the CInt or CLng function to convert a string to an integer or long (respectively) data types.
Example, change the line to: c.EntireRow.RowHeight = CInt(c.Value) Anyway thanks for the positive feedback(y), glad we were able to help.
By the way, you probably need to mark this thread as 'Solved'.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top