Hi there
I have an print area set up and I want to be able to automatically resize the rows and print area (through a macro) depending on the number of rows that are occupied by data.
The more rows occupied, the smaller the row height and the bigger the print area.
I think I have the macros set up, I just need to find out a way of monitoring the number of rows being used and run the macro as and when required.
Now, I have this setup in a cell that is outside of the print area:
This counts any text in the rows in question and the formula therefore returns a number.
If this formula returns any value less than or equal to 20, I want a macro to run that adjusts the rows as necessary.
Then for every increase to 21,22,23,24.... up to 34, I want a macro to run for each number that adjusts the row sizes and sets the print area.
I thought I had it all set up but it doesn't appear to work. BB52 is the location of the COUNTIF formula above:
Can anyone suggest why this doesn't work?
I've tried some test data to increase the formula result up to 34, but nothing changes.
Thanks for any help,
Liam
I have an print area set up and I want to be able to automatically resize the rows and print area (through a macro) depending on the number of rows that are occupied by data.
The more rows occupied, the smaller the row height and the bigger the print area.
I think I have the macros set up, I just need to find out a way of monitoring the number of rows being used and run the macro as and when required.
Now, I have this setup in a cell that is outside of the print area:
Code:
=COUNTIF(E14:E50,"*")
This counts any text in the rows in question and the formula therefore returns a number.
If this formula returns any value less than or equal to 20, I want a macro to run that adjusts the rows as necessary.
Then for every increase to 21,22,23,24.... up to 34, I want a macro to run for each number that adjusts the row sizes and sets the print area.
I thought I had it all set up but it doesn't appear to work. BB52 is the location of the COUNTIF formula above:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("BB52")) Is Nothing Then
Select Case Target.Value
Case Is = "21":
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Rows("14:50").RowHeight = 19.75
With Range("D36:AQ50").Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Range("D37:AQ37").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
ActiveSheet.PageSetup.PrintArea = "$D$5:$AQ$37"
ActiveSheet.Protect
Application.ScreenUpdating = True
Case Is = "22":
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Rows("14:50").RowHeight = 19
With Range("D36:AQ50").Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Range("D38:AQ38").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
ActiveSheet.PageSetup.PrintArea = "$D$5:$AQ$38"
ActiveSheet.Protect
Application.ScreenUpdating = True
Case Is = "23":
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Rows("14:50").RowHeight = 18.25
With Range("D36:AQ50").Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Range("D39:AQ39").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
ActiveSheet.PageSetup.PrintArea = "$D$5:$AQ$39"
ActiveSheet.Protect
Application.ScreenUpdating = True
Case Is = "24":
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Rows("14:50").RowHeight = 17.5
With Range("D36:AQ50").Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Range("D40:AQ40").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
ActiveSheet.PageSetup.PrintArea = "$D$5:$AQ$40"
ActiveSheet.Protect
Application.ScreenUpdating = True
Case Is = "25":
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Rows("14:50").RowHeight = 16.75
With Range("D36:AQ50").Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Range("D41:AQ41").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
ActiveSheet.PageSetup.PrintArea = "$D$5:$AQ$41"
ActiveSheet.Protect
Application.ScreenUpdating = True
Case Is = "26":
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Rows("14:50").RowHeight = 16.25
With Range("D36:AQ50").Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Range("D42:AQ42").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
ActiveSheet.PageSetup.PrintArea = "$D$5:$AQ$42"
ActiveSheet.Protect
Application.ScreenUpdating = True
Case Is = "27":
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Rows("14:50").RowHeight = 16
With Range("D36:AQ50").Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Range("D43:AQ43").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
ActiveSheet.PageSetup.PrintArea = "$D$5:$AQ$43"
ActiveSheet.Protect
Application.ScreenUpdating = True
Case Is = "28":
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Rows("14:50").RowHeight = 15.25
With Range("D36:AQ50").Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Range("D44:AQ44").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
ActiveSheet.PageSetup.PrintArea = "$D$5:$AQ$44"
ActiveSheet.Protect
Application.ScreenUpdating = True
Case Is = "29":
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Rows("14:50").RowHeight = 14.75
With Range("D36:AQ50").Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Range("D45:AQ45").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
ActiveSheet.PageSetup.PrintArea = "$D$5:$AQ$45"
ActiveSheet.Protect
Application.ScreenUpdating = True
Case Is = "30":
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Rows("14:50").RowHeight = 14.5
With Range("D36:AQ50").Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Range("D46:AQ46").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
ActiveSheet.PageSetup.PrintArea = "$D$5:$AQ$46"
ActiveSheet.Protect
Application.ScreenUpdating = True
Case Is = "31":
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Rows("14:50").RowHeight = 14
With Range("D36:AQ50").Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Range("D47:AQ47").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
ActiveSheet.PageSetup.PrintArea = "$D$5:$AQ$47"
ActiveSheet.Protect
Application.ScreenUpdating = True
Case Is = "32":
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Rows("14:50").RowHeight = 13.75
With Range("D36:AQ50").Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Range("D48:AQ48").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
ActiveSheet.PageSetup.PrintArea = "$D$5:$AQ$48"
ActiveSheet.Protect
Application.ScreenUpdating = True
Case Is = "33":
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Rows("14:50").RowHeight = 13
With Range("D36:AQ50").Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Range("D49:AQ49").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
ActiveSheet.PageSetup.PrintArea = "$D$5:$AQ$49"
ActiveSheet.Protect
Application.ScreenUpdating = True
Case Is = "34":
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Rows("14:50").RowHeight = 12.75
With Range("D36:AQ50").Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
.Borders (xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
ActiveSheet.PageSetup.PrintArea = "$D$5:$AQ$50"
ActiveSheet.Protect
Application.ScreenUpdating = True
Case Is = "35":
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Rows("14:50").RowHeight = 12.75
With Range("D36:AQ50").Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
.Borders (xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
ActiveSheet.PageSetup.PrintArea = "$D$5:$AQ$50"
ActiveSheet.Protect
Application.ScreenUpdating = True
Case Is = "36":
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Rows("14:50").RowHeight = 12.75
With Range("D36:AQ50").Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
.Borders (xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
ActiveSheet.PageSetup.PrintArea = "$D$5:$AQ$50"
ActiveSheet.Protect
Application.ScreenUpdating = True
Case Is = "37":
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Rows("14:50").RowHeight = 12.75
With Range("D36:AQ50").Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
.Borders (xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
ActiveSheet.PageSetup.PrintArea = "$D$5:$AQ$50"
ActiveSheet.Protect
Application.ScreenUpdating = True
Case Else:
Application.ScreenUpdating = False
ActiveSheet.Unprotect
Rows("14:50").RowHeight = 20.5
With Range("D36:AQ50").Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Range("D36:AQ36").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
ActiveSheet.PageSetup.PrintArea = "$D$5:$AQ$36"
ActiveSheet.Protect
Application.ScreenUpdating = True
End Select
End If
End Sub
Can anyone suggest why this doesn't work?
I've tried some test data to increase the formula result up to 34, but nothing changes.
Thanks for any help,
Liam