June 18, 2018

Autofit Data Automatically

How to autofit data automatically and wrap text in an Excel worksheet automatically with VBA. As we type data into an Excel worksheet we note that the content can be of different length and therefore we may need to auto-fit the content manually. To save time we can automate the complete process with a single line of code in VBA. The same applies to content that needs to be wrapped for better readability. Watch the video below:

Watch this video on YouTube.

Here’s the complete VBA code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Range(“D3”).ColumnWidth = 20
Worksheets(“sheet1”).Range(“D3”).Value = “Total due in 15 days. Overdue accounts subject to a service charge of 1.5% per month.”
Worksheets(“sheet1”).Range(“D3”).WrapText = True

End Sub

2 thoughts on “Autofit Data Automatically

  1. Mera naam : Mahendra Bhavsar.

    Mumbai se hu. Mo. 9967262682

    aap Close STR-9 please chang colour 10943.46 <10919.6 color is Red(10943.46), 2 row down colour 10943.40 <10950.9 color is Green(10950.9) contined Down Raw With color formeting. my mo no. 9967262682 mumbai

    Close STR-9
    10919.6 10943.46
    10950.9 10943.4
    10880.35 10943.36
    10911.8 10943.3
    10907.95 10943.29
    10876.2 10943.29
    10892.45 10943.26
    10856.2 10928.07
    10854.05 10919.65
    10841.4 10899.4
    10830.35 10888.89
    10834.8 10882.87
    10855.1 10882.87
    10854.95 10882.87
    10845.3 10882.87
    10876.05 10882.87
    10894.05 10827.91
    10878.85 10837.86
    10867.75 10837.86

    Colour colam drag karne se value & clour same rahate hai.


  2. Dear Sir,
    Great thanks for solving my problem of autofit column by vba.Further pl. solve my big problemthat when i am using worksheet in project explorer foruserform the written code is not working for chart in excel but individually both are working . how i can combined both . your steps are required to solve the problem.
    for your information i am not from this field but from pharmacy.
    s.m. sharma

Comments are closed.