Macro to set the row height

D

Derk

I need to set the row height depending on the content of a cell.

If Cell A1 = "x", I want to set rowheight of A to 50, if A1 = y , then I
want to set rowheight of A to 75

I want to repeat this check for 100 rows.

Could many one help please

Many thanks
Derrick
 
M

Micah

You need VBA code to do this. Within Excel, type ALT-F11, which brings
you to the VBA editor. Then

1. For each sheet that you want this re-row-height to take place, copy
this code to that sheet's code window:

Private Sub Worksheet_Change(ByVal Target As Range)
ReRowHeight ActiveSheet
End Sub

2. Insert a new module (under Worksheet) and paste this code:

Const InterestingRange$ = "$A$1:$A$100"

Sub ReRowHeight(ws As Worksheet)

Dim r As Range, c As Range

Set r = ActiveSheet.Range(InterestingRange)
For Each c In r
If UCase(Trim(c.Value)) = "X" Then
c.RowHeight = 50
ElseIf UCase(Trim(c.Value)) = "Y" Then
c.RowHeight = 75
End If
Next c

End Sub

At this point, every time you make a change to a sheet (where you put
the first bit of code), the procedure will run. It will go through and
identify each of the values in the interesting range (here, A1:A100),
and, for each X or Y in the range, will re-do the height. It is NOT
case-sensitive and won't pay attention to leading or trailing spaces.

Good luck,

Micah
 

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