Cells expanding with text

Olavfinnerud

New Member
Joined
Jun 7, 2022
Messages
16
Office Version
  1. 2021
Platform
  1. Windows
Hi,
I have a problem with the text in the first row, i want to be able to write longer words/sentences in the cell and have the cell expand with the text so it fits in the column. I have tried for some hours now and cant find a solution that works. So when i press the button and add a new column i want to be able to write longer words than "Annleggsleder" and have the column expand to fit the text in the cell. The code i have for the button to add a new column is:

Sub Macro2()

Range("C6:C17").Copy
Cells(6, Columns.Count).End(xlToLeft).Offset(0, 1).Select
ActiveSheet.Paste
Application.CutCopyMode = False

End Sub

- I have attached a picture of the worksheet
 

Attachments

  • picture of worksheet.png
    picture of worksheet.png
    18.4 KB · Views: 9

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Add this event macro to your sheet module if there isn't already one, otherwise you need to integrate this code into your existing code.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    'test if we are in range rows 6 to 17
    If Intersect(Target, Range("6:17")) Is Nothing Then Exit Sub 'exit if not
    'if so, autofit column to longest word entered in range
    Target.EntireColumn.AutoFit
End Sub
 
Upvote 0
Solution
Add this event macro to your sheet module if there isn't already one, otherwise you need to integrate this code into your existing code.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    'test if we are in range rows 6 to 17
    If Intersect(Target, Range("6:17")) Is Nothing Then Exit Sub 'exit if not
    'if so, autofit column to longest word entered in range
    Target.EntireColumn.AutoFit
End Sub
Thank you! Saved me alot of time with frustration :)
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,409
Members
448,959
Latest member
camelliaCase

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