VBA - Autofit Row Height

ScottishPeter

New Member
Joined
Apr 22, 2023
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hello, i'm trying to change some script to ensure rows automatically adjust height based on the text that populates the cell.
On some occasions the cells wont have to be adjusted, but sometimes they will. Hence trying to do it automatically.

The AutoFit code is towards the bottom.

Currently VBA is:

VBA Code:
Sub listWine()
    Dim n As Long
    Dim tcbCode As String
    Dim findRange As Range
    Dim wsData As Worksheet
    Dim wsList As Worksheet
    
    Set wsData = Worksheets("Wine Data")
    Set wsList = Worksheets("Wine List")
    
    tcbCode = Range("B" & ActiveCell.Row).Value
    If tcbCode = "" Then
        MsgBox "kindly select Proper Wine!", vbCritical
        Exit Sub
    End If
    
    Set findRange = wsData.Range("B:B").Find(tcbCode, , xlValues, xlWhole)
    If Not findRange Is Nothing Then
        n = wsList.Range("A" & Rows.Count).End(xlUp).Row + 1
        
        wsList.Range("A" & n).Value = wsData.Range("B" & findRange.Row).Value
        wsList.Range("B" & n).Value = wsData.Range("I" & findRange.Row).Value
        wsList.Range("C" & n).Value = wsData.Range("C" & findRange.Row).Value
        wsList.Range("D" & n).Value = wsData.Range("F" & findRange.Row).Value
        wsList.Range("E" & n).Value = wsData.Range("T" & findRange.Row).Value
        wsList.Range("F" & n).Value = wsData.Range("D" & findRange.Row).Value
        wsList.Range("G" & n).Value = wsData.Range("P" & findRange.Row).Value
        wsList.Range("H" & n).Value = wsData.Range("R" & findRange.Row).Value

        wsList.Range("J" & n).Formula = "=IF(I" & n & "<>0,1 - ((H" & n & " * 1.2) / I" & n & "), """")"
        wsList.Range("K" & n).Formula = "=IF(I" & n & "<>0,1 + ((I" & n & " / 1.2) - H" & n & ") - 1, """")"
        wsList.Range("M" & n).Formula = "=IF(L" & n & "<>0,1 - (((H" & n & " / 6) * 1.2) / L" & n & "), """")"
        wsList.Range("O" & n).Formula = "=IF(N" & n & "<>0,1 - (((H" & n & " / 4.28571) * 1.2) / N" & n & "), """")"
        wsList.Range("Q" & n).Formula = "=IF(P" & n & "<>0,1 - (((H" & n & " / 3) * 1.2) / P" & n & "), """")"

        
        
        
        wsList.Range("A" & n + 1).Value = wsData.Range("S" & findRange.Row).Value
        
        wsList.Range("A" & n + 1 & ":Q" & n + 1).Merge
        wsList.Range("A" & n + 1).WrapText = True
        wsList.Range("A" & n + 1).EntireRow.AutoFit
        wsList.Range("A" & n + 1 & ":Q" & n + 1).Interior.ColorIndex = 15
        wsList.Range("A" & n + 1).HorizontalAlignment = xlCenter
        
        
    Else
        MsgBox "Wine Associated with this code not found in data!", vbInformation
    End If
End Sub

Thanks
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Forum statistics

Threads
1,223,893
Messages
6,175,239
Members
452,621
Latest member
Laura_PinksBTHFT

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