Worksheet change event to change font, wrap text and shrink to fit in second cell

dakota727

Board Regular
Joined
Dec 3, 2006
Messages
164
Office Version
  1. 365
OK I have not been able to get this working. What I am trying to do is to when the value in B8 is changed the format of cell C6 on sheet Diagram has the font, wrap and shrink to fit changed based on the length of the entry. Values greater than 16 small font and wrap text. Values less than 16 large font with shrink to fit.

I'm not sure where I am going wrong.


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$8" Then
     
    If Len(Target) > 16 Then

Application.EnableEvents = False
Worksheets("Diagram").Range("C6").Activate


With ActiveCell
    .ShrinkToFit = False
    .Font.Size = 11
    .WrapText = True
End With
Else
 Application.EnableEvents = False
Worksheets("Diagram").Range("C6").Activate
With ActiveCell
.Font.Size = 16
.WrapText = False
.ShrinkToFit = True
End With
End If

stoppit:
Application.EnableEvents = True
End If
  
  

End Sub

I must admit I always struggle with on change events. Any help would be appreciated.

Thanks
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I think I found my error. I hade the last enable events inside the IF statement. Looks like this is working,
Code:
 Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$8" Then
   Application.EnableEvents = False
    If Len(Target) > 16 Then
        Worksheets("Diagram").Range("C6").WrapText = True
        Worksheets("Diagram").Range("C6").Font.Size = 11
        Worksheets("Diagram").Range("C6").ShrinkToFit = False
        
    Else

        Worksheets("Diagram").Range("C6").WrapText = False
        Worksheets("Diagram").Range("C6").Font.Size = 24
        Worksheets("Diagram").Range("C6").ShrinkToFit = True
End If
Else

Application.EnableEvents = True
End If
stoppit:
Application.EnableEvents = True
 
End Sub
 
Last edited:
Upvote 0
For the structure you have used, I would think this would be "cleaner"...
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Address = "$B$8" Then
    Application.EnableEvents = False
    If Len(Target) > 16 Then
      Worksheets("Diagram").Range("C6").WrapText = True
      Worksheets("Diagram").Range("C6").Font.Size = 11
      Worksheets("Diagram").Range("C6").ShrinkToFit = False
    Else
      Worksheets("Diagram").Range("C6").WrapText = False
      Worksheets("Diagram").Range("C6").Font.Size = 24
      Worksheets("Diagram").Range("C6").ShrinkToFit = True
    End If
    Application.EnableEvents = True
  End If
End Sub
I believe this more compact version will do the same thing as the above code does...
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Test As Boolean
  If Target.Address = "$B$8" Then
    Test = Len(Target) > 16
    Application.EnableEvents = False
    Worksheets("Diagram").Range("C6").WrapText = Test
    Worksheets("Diagram").Range("C6").Font.Size = 24 + 13 * Test
    Worksheets("Diagram").Range("C6").ShrinkToFit = Not Test
    Application.EnableEvents = True
  End If
End Sub
 
Upvote 0
I think I follow what you are doing. One typo though, to get to the 11 font I think you meant (24-13 * Test).

I figured there would probably be an easier way and I think you have provided it. Thank you very much.
 
Upvote 0
I think I follow what you are doing. One typo though, to get to the 11 font I think you meant (24-13 * Test).

I figured there would probably be an easier way and I think you have provided it. Thank you very much.

No, the plus sign was intentional... in VB, True equates to minus one (-1), not plus one. So Test will either be the value 0 or -1.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,154
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