Skip to content

Instantly share code, notes, and snippets.

@DuongAQ
Created February 21, 2018 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DuongAQ/2244a97db5a5afa6c3422a74ab07f054 to your computer and use it in GitHub Desktop.
Save DuongAQ/2244a97db5a5afa6c3422a74ab07f054 to your computer and use it in GitHub Desktop.
Private Sub Worksheet_Change(ByVal Target As Range) 'Tự động giãn dòng vùng đã trộn ô
Dim MergeWidth As Single
Dim cM As Range
Dim AutoFitRng As Range
Dim CWidth As Double
Dim NewRowHt As Double
Dim str01 As String
str01 = "OrderNote"
'OrderNote = Tên vùng trộn ô
If Not Intersect(Target, Range(str01)) Is Nothing Then
Application.ScreenUpdating = False
On Error Resume Next
Set AutoFitRng = Range(Range(str01).MergeArea.Address)
With AutoFitRng
.MergeCells = False
CWidth = .Cells(1).ColumnWidth
MergeWidth = 0
For Each cM In AutoFitRng
cM.WrapText = True
MergeWidth = cM.ColumnWidth + MergeWidth
Next
'Điều chỉnh 1 chút về độ rộng tạm thời:
MergeWidth = MergeWidth + AutoFitRng.Cells.Count * 0.66
.Cells(1).ColumnWidth = MergeWidth
.EntireRow.AutoFit
NewRowHt = .RowHeight
.Cells(1).ColumnWidth = CWidth
.MergeCells = True
.RowHeight = NewRowHt
End With
Application.ScreenUpdating = True
End If
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment