Note: The other languages of the website are Google-translated. Back to English
Mewngofnodi  \/ 
x
or
x
Cofrestru  \/ 
x

or

Sut i ffitio uchder rhes celloedd cyfun yn Excel?

Yn Excel, gallwn addasu uchder y rhes yn gyflym i ffitio cynnwys y gell trwy ddefnyddio'r Uchder Row AutoFit nodwedd, ond bydd y swyddogaeth hon yn anwybyddu celloedd unedig yn llwyr. Hynny yw, ni allwch gymhwyso'r Uchder Row AutoFit nodwedd i newid maint rhes celloedd cyfun, mae angen i chi addasu uchder y rhes â llaw ar gyfer celloedd unedig fesul un. Yn yr erthygl hon, gallaf gyflwyno rhai dulliau cyflym i ddatrys y broblem hon.

Uchder rhes ffit awto celloedd unedig â chod VBA

Tab Swyddfa Galluogi Golygu a Pori Tabbed yn y Swyddfa, a Gwneud Eich Gwaith yn Haws Orau ...
Mae Kutools for Excel yn Datrys y rhan fwyaf o'ch Problemau, ac yn Cynyddu Eich Cynhyrchedd 80%
  • Ailddefnyddio Unrhyw beth: Ychwanegwch y fformwlâu, siartiau ac unrhyw beth arall a ddefnyddir fwyaf cymhleth i'ch ffefrynnau, a'u hailddefnyddio'n gyflym yn y dyfodol.
  • Mwy nag 20 o nodweddion testun: Rhif Detholiad o Llinyn Testun; Tynnu neu Dynnu Rhan o Testunau; Trosi Rhifau ac Arian Cyfred yn Eiriau Saesneg.
  • Uno Offer: Llyfrau Gwaith a Thaflenni Lluosog yn Un; Uno Celloedd Lluosog / Rhesi / Colofnau Heb Golli Data; Uno Rhesi a Swm Dyblyg.
  • Hollti Offer: Rhannwch Ddata yn Daflenni Lluosog Yn Seiliedig ar Werth; Un Llyfr Gwaith i Ffeiliau Lluosog Excel, PDF neu CSV; Un Golofn i Golofnau Lluosog.
  • Gludo Sgipio Rhesi Cudd / Hidlo; Cyfrif A Swm yn ôl Lliw Cefndir; Anfon E-byst wedi'u Personoli at Dderbynwyr Lluosog mewn Swmp.
  • Hidlo Gwych: Creu cynlluniau hidlo datblygedig a'u cymhwyso i unrhyw daflenni; Trefnu yn yn ôl wythnos, dydd, amlder a mwy; Hidlo gan feiddgar, fformwlâu, sylw ...
  • Mwy na 300 o nodweddion pwerus; Yn gweithio gyda'r Swyddfa 2007-2019 a 365; Yn cefnogi pob iaith; Defnydd hawdd yn eich menter neu sefydliad.

swigen dde glas saeth Uchder rhes ffit awto celloedd unedig â chod VBA


Gan dybio bod gen i daflen waith gyda rhai celloedd unedig fel y dangosir y llun isod, a nawr mae angen i mi newid maint uchder y rhes gell i arddangos y cynnwys cyfan, efallai y bydd y cod VBA isod yn eich helpu i ffitio uchder rhes celloedd unedig lluosog, gwnewch hynny fel a ganlyn:

doc-autofit-merged-cealla-1

1. Daliwch i lawr y ALT + F11 allweddi, ac mae'n agor y Ffenestr Microsoft Visual Basic for Applications.

2. Cliciwch Mewnosod > Modiwlau, a gludwch y cod canlynol yn y Ffenestr Modiwl.

Cod VBA: Uchder rhes ffit awto celloedd unedig lluosog
Option Explicit
Public Sub AutoFitAll()
  Call AutoFitMergedCells(Range("a1:b2"))
   Call AutoFitMergedCells(Range("c4:d6"))
    Call AutoFitMergedCells(Range("e1:e3"))
End Sub
Public Sub AutoFitMergedCells(oRange As Range)
  Dim tHeight As Integer
  Dim iPtr As Integer
  Dim oldWidth As Single
  Dim oldZZWidth As Single
  Dim newWidth As Single
  Dim newHeight As Single
  With Sheets("Sheet4")
    oldWidth = 0
    For iPtr = 1 To oRange.Columns.Count
      oldWidth = oldWidth + .Cells(1, oRange.Column + iPtr - 1).ColumnWidth
    Next iPtr
    oldWidth = .Cells(1, oRange.Column).ColumnWidth + .Cells(1, oRange.Column + 1).ColumnWidth
    oRange.MergeCells = False
    newWidth = Len(.Cells(oRange.Row, oRange.Column).Value)
    oldZZWidth = .Range("ZZ1").ColumnWidth
    .Range("ZZ1") = Left(.Cells(oRange.Row, oRange.Column).Value, newWidth)
    .Range("ZZ1").WrapText = True
    .Columns("ZZ").ColumnWidth = oldWidth
    .Rows("1").EntireRow.AutoFit
    newHeight = .Rows("1").RowHeight / oRange.Rows.Count
    .Rows(CStr(oRange.Row) & ":" & CStr(oRange.Row + oRange.Rows.Count - 1)).RowHeight = newHeight
    oRange.MergeCells = True
    oRange.WrapText = True
    .Range("ZZ1").ClearContents
    .Range("ZZ1").ColumnWidth = oldZZWidth
  End With
End Sub

Nodiadau:

(1.) Yn y cod uchod, gallwch ychwanegu copïau o ystodau newydd Ffoniwch AutoFitMergedCells (Ystod ("a1: b2")) sgriptiwch lawer gwaith fel y dymunwch, a newid yr ystodau celloedd unedig i'r rhai sydd eu hangen arnoch chi.

(2.) A dylech chi newid enw'r daflen waith gyfredol Sheet4 i'ch enw dalen a ddefnyddir.

3. Yna pwyswch F5 yn allweddol i redeg y cod hwn, ac yn awr, gallwch weld bod yr holl gelloedd unedig wedi'u gosod yn awtomatig ar gynnwys eu celloedd, gweler y screenshot:

doc-autofit-merged-cealla-1


Erthygl gysylltiedig:

Sut i ffitio lled colofn yn Excel yn awtomatig?


Yr Offer Cynhyrchedd Swyddfa Gorau

Mae Kutools for Excel yn Datrys y rhan fwyaf o'ch Problemau, ac yn Cynyddu Eich Cynhyrchedd 80%

  • Ailddefnyddio: Mewnosod yn gyflym fformwlâu cymhleth, siartiau ac unrhyw beth rydych chi wedi'i ddefnyddio o'r blaen; Amgryptio Celloedd gyda chyfrinair; Creu Rhestr Bostio ac anfon e-byst ...
  • Bar Fformiwla Gwych (golygu llinellau lluosog o destun a fformiwla yn hawdd); Cynllun Darllen (darllen a golygu nifer fawr o gelloedd yn hawdd); Gludo i'r Ystod Hidlo...
  • Uno Celloedd / Rhesi / Colofnau heb golli Data; Cynnwys Celloedd Hollt; Cyfuno Rhesi / Colofnau Dyblyg... Atal Celloedd Dyblyg; Cymharwch y Meysydd...
  • Dewiswch Dyblyg neu Unigryw Rhesi; Dewiswch Blank Rows (mae pob cell yn wag); Darganfyddiad Gwych a Darganfyddiad Niwlog mewn Llawer o Lyfrau Gwaith; Dewis ar Hap ...
  • Copi Union Celloedd Lluosog heb newid cyfeirnod fformiwla; Auto Creu Cyfeiriadau i Daflenni Lluosog; Mewnosod Bwledi, Blychau Gwirio a mwy ...
  • Testun Detholiad, Ychwanegu Testun, Tynnu yn ôl Swydd, Tynnwch y Gofod; Creu ac Argraffu Subtotals Paging; Trosi rhwng Cynnwys a Sylwadau Celloedd...
  • Hidlo Super (arbed a chymhwyso cynlluniau hidlo i ddalenni eraill); Trefnu Uwch yn ôl mis / wythnos / dydd, amlder a mwy; Hidlo Arbennig gan feiddgar, italig ...
  • Cyfuno Llyfrau Gwaith a Thaflenni Gwaith; Uno Tablau yn seiliedig ar golofnau allweddol; Rhannwch Ddata yn Daflenni Lluosog; Trosi Swp xls, xlsx a PDF...
  • Mwy na 300 o nodweddion pwerus. Yn cefnogi Swyddfa / Excel 2007-2019 a 365. Yn cefnogi pob iaith. Defnydd hawdd yn eich menter neu sefydliad. Nodweddion llawn treial am ddim 30 diwrnod. Gwarant arian yn ôl 60 diwrnod.
tab kte 201905

Mae Tab Office yn Dod â rhyngwyneb Tabbed i'r Swyddfa, a Gwneud Eich Gwaith yn Haws o lawer

  • Galluogi golygu a darllen tabbed yn Word, Excel, PowerPoint, Cyhoeddwr, Mynediad, Visio a Phrosiect.
  • Agor a chreu dogfennau lluosog mewn tabiau newydd o'r un ffenestr, yn hytrach nag mewn ffenestri newydd.
  • Yn cynyddu eich cynhyrchiant 50%, ac yn lleihau cannoedd o gliciau llygoden i chi bob dydd!
gwaelod officetab

<p >


Yr Offer Cynhyrchedd Swyddfa Gorau

Mae Kutools for Excel yn Datrys y rhan fwyaf o'ch Problemau, ac yn Cynyddu Eich Cynhyrchedd 80%

  • Ailddefnyddio: Mewnosod yn gyflym fformwlâu cymhleth, siartiau ac unrhyw beth rydych chi wedi'i ddefnyddio o'r blaen; Amgryptio Celloedd gyda chyfrinair; Creu Rhestr Bostio ac anfon e-byst ...
  • Bar Fformiwla Gwych (golygu llinellau lluosog o destun a fformiwla yn hawdd); Cynllun Darllen (darllen a golygu nifer fawr o gelloedd yn hawdd); Gludo i'r Ystod Hidlo...
  • Uno Celloedd / Rhesi / Colofnau heb golli Data; Cynnwys Celloedd Hollt; Cyfuno Rhesi / Colofnau Dyblyg... Atal Celloedd Dyblyg; Cymharwch y Meysydd...
  • Dewiswch Dyblyg neu Unigryw Rhesi; Dewiswch Blank Rows (mae pob cell yn wag); Darganfyddiad Gwych a Darganfyddiad Niwlog mewn Llawer o Lyfrau Gwaith; Dewis ar Hap ...
  • Copi Union Celloedd Lluosog heb newid cyfeirnod fformiwla; Auto Creu Cyfeiriadau i Daflenni Lluosog; Mewnosod Bwledi, Blychau Gwirio a mwy ...
  • Testun Detholiad, Ychwanegu Testun, Tynnu yn ôl Swydd, Tynnwch y Gofod; Creu ac Argraffu Subtotals Paging; Trosi rhwng Cynnwys a Sylwadau Celloedd...
  • Hidlo Super (arbed a chymhwyso cynlluniau hidlo i ddalenni eraill); Trefnu Uwch yn ôl mis / wythnos / dydd, amlder a mwy; Hidlo Arbennig gan feiddgar, italig ...
  • Cyfuno Llyfrau Gwaith a Thaflenni Gwaith; Uno Tablau yn seiliedig ar golofnau allweddol; Rhannwch Ddata yn Daflenni Lluosog; Trosi Swp xls, xlsx a PDF...
  • Mwy na 300 o nodweddion pwerus. Yn cefnogi Swyddfa / Excel 2007-2019 a 365. Yn cefnogi pob iaith. Defnydd hawdd yn eich menter neu sefydliad. Nodweddion llawn treial am ddim 30 diwrnod. Gwarant arian yn ôl 60 diwrnod.
tab kte 201905

Mae Tab Office yn Dod â rhyngwyneb Tabbed i'r Swyddfa, a Gwneud Eich Gwaith yn Haws o lawer

  • Galluogi golygu a darllen tabbed yn Word, Excel, PowerPoint, Cyhoeddwr, Mynediad, Visio a Phrosiect.
  • Agor a chreu dogfennau lluosog mewn tabiau newydd o'r un ffenestr, yn hytrach nag mewn ffenestri newydd.
  • Yn cynyddu eich cynhyrchiant 50%, ac yn lleihau cannoedd o gliciau llygoden i chi bob dydd!
gwaelod officetab
</ p >

Say something here...
symbols left.
You are guest
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Art · 1 years ago
    Dang it, copy/paste bit me. Also, with explicit sheet references the With isn't needed:

    Public Sub AutoFitMergedCells(oRange As Range, ByVal dblWidth As Double)

    oRange.MergeCells = False
    Sheet1.Range("A1") = oRange.Cells(1, 1).Value
    Sheet1.Range("A1").WrapText = True
    Sheet1.Columns(1).ColumnWidth = dblWidth
    Sheet1.Rows(1).EntireRow.AutoFit
    oRange.Parent.Rows(oRange.Row).Resize(oRange.Rows.Count).RowHeight _
    = Sheet1.Rows(1).RowHeight / oRange.Rows.Count
    oRange.MergeCells = True
    oRange.WrapText = True

    End Sub
  • To post as a guest, your comment is unpublished.
    Art · 1 years ago
    Thank you, that helped me with a sheet I've not been happy with for years.

    I did change things around a bit, my merged cells are all in one column so I calculated that outside the loop and passed it. I also inserted a Sheet1 that is hidden, and manipulated the columns/rows there so as to not affect the sheet I'm working on. The references should probably be more explicit:

    Public Sub AutoFitMergedCells(oRange As Range, ByVal dblWidth As Double)

    Dim dblHeight As Double

    With oRange.Parent
    oRange.MergeCells = False
    Sheet1.Range("A1") = oRange.Cells(1, 1).Value
    Sheet1.Range("A1").WrapText = True
    Sheet1.Columns(1).ColumnWidth = dblWidth
    Sheet1.Rows(1).EntireRow.AutoFit
    dblHeight = Sheet1.Rows(1).RowHeight / oRange.Rows.Count
    oRange.Parent.Rows(oRange.Row).Resize(oRange.Rows.Count).RowHeight = newHeight
    oRange.MergeCells = True
    oRange.WrapText = True
    Sheet1.Range("A1").ClearContents
    End With

    End Sub
  • To post as a guest, your comment is unpublished.
    José · 2 years ago
    This not work for me}
  • To post as a guest, your comment is unpublished.
    saravanan · 2 years ago
    not working , ye password set in your code not working in your code
  • To post as a guest, your comment is unpublished.
    dougs · 2 years ago
    I believe the reason that the row heights do not calculate properly is related to these lines of code
    For iPtr = 1 To oRange.Columns.Count
    oldWidth = oldWidth + .Cells(1, oRange.Column + iPtr - 1).ColumnWidth
    Next iPtr
    oldWidth = .Cells(1, oRange.Column).ColumnWidth + .Cells(1, oRange.Column + 1).ColumnWidth

    The variable OldWidth gets set to the sum of the column widths in the range, but for some reason it gets reset to only the width of the first two columns. The first 3 lines of code are therefore made redundant by the 4th line. When I removed the line it was much better, but the other issue I found was that you have to make sure that the font and font size of the temporary cell (ZZ1 in the example code) must match the font and size of the merged cells; otherwise, text will not wrap in the same way as the merged cells wrap and may not be the correct height.
  • To post as a guest, your comment is unpublished.
    toowaki · 3 years ago
    I made add-in for Auto fit row height of multiple merged cells.
    Please use this, if you want to autofit row hight.
    [Release Ver2.6 · toowaki/AutoFitRowEx · GitHub]
    https://github.com/toowaki/AutoFitRowEx/releases/tag/2.6.2
    • To post as a guest, your comment is unpublished.
      ABC · 3 years ago
      This is pretty helpful, thanks!
  • To post as a guest, your comment is unpublished.
    Krishna · 3 years ago
    I am trying to understand the necessity of Line 19. You are assigning a value again to OldWidth. Can you please explain?
  • To post as a guest, your comment is unpublished.
    Chris · 4 years ago
    My code will not even run I just get a compile error when I try to call the AutoFitMergedCells - Expected Function or variable?
  • To post as a guest, your comment is unpublished.
    Aperture · 4 years ago
    Because the "helper" cell of ZZ1 is using the first row (column ZZ, row 1), if there is ANYTHING in row 1 taller than the text in the row you want to adjust, your resulting height will be taller than what you want.

    To fix this, I made the helper cell the same column as the first column in the oRange and set the row number to the very last row in Excel. Hope this helps you like it does me. 8)

    [u]My Code:[/u]

    Option Explicit
    Public Sub AutoFitAll()
    Call AutoFitMergedCells(Range("A2:Z2"))

    End Sub
    Public Sub AutoFitMergedCells(oRange As Range)
    Dim tHeight As Integer
    Dim iPtr As Integer
    Dim oldWidth As Single
    Dim oldZZWidth As Single
    Dim newWidth As Single
    Dim newHeight As Single
    With Sheets("Sheet1")
    oldWidth = 0
    For iPtr = 1 To oRange.Columns.Count
    oldWidth = oldWidth + .Cells(1, oRange.Column + iPtr - 1).ColumnWidth
    Next iPtr
    oldWidth = .Cells(1, oRange.Column).ColumnWidth + .Cells(1, oRange.Column + 1).ColumnWidth
    oRange.MergeCells = False
    newWidth = Len(.Cells(oRange.Row, oRange.Column).Value)
    oldZZWidth = .Cells("1048576", oRange.Column).ColumnWidth
    .Cells("1048576", oRange.Column) = Left(.Cells(oRange.Row, oRange.Column).Value, newWidth)
    .Cells("1048576", oRange.Column).WrapText = True
    .Columns(oRange.Column).ColumnWidth = oldWidth
    .Rows("1048576").EntireRow.AutoFit
    newHeight = .Rows("1048576").RowHeight / oRange.Rows.Count
    .Rows(CStr(oRange.Row) & ":" & CStr(oRange.Row + oRange.Rows.Count - 1)).RowHeight = newHeight
    oRange.MergeCells = True
    oRange.WrapText = True
    .Cells("1048576", oRange.Column).ClearContents
    .Cells("1048576", oRange.Column).ColumnWidth = oldZZWidth
    End With
    End Sub
  • To post as a guest, your comment is unpublished.
    Retha · 4 years ago
    Thank you for the code. However, the height of my rows do adjust, but now enough. How can I rectify this?
  • To post as a guest, your comment is unpublished.
    Jeff · 4 years ago
    Thanks for posting this, I'm decent with excel and can usually figure out my adjustments but I can't seem to figure out a fix for an issue I'm having, or if one even exists. I have huge amounts of data in the cells (exceeding the single cell height limit of 409.5). The problem is this VBA runs with that same limitation. So some of my data gets cut off even though the rows are merged and the combined cell height allowance is 819, since the VBA adjusts the cell height based off the single ZZ1 cell. Is there anyway to adjust the code to get it to allow the adjusted cell height to include the available height in the merged rows or am I asking for the impossible? Thanks.
  • To post as a guest, your comment is unpublished.
    Vivi · 4 years ago
    Works perfect, but rows are too high! Can we fix this?
  • To post as a guest, your comment is unpublished.
    Hana · 4 years ago
    Brilliant but exactly the same problem as Danielle, rows are too high now. Please someone help!
  • To post as a guest, your comment is unpublished.
    Susan · 4 years ago
    Thanks so much for the code. Is there any way to make the macro run as soon as you type text in a field and hit enter?
  • To post as a guest, your comment is unpublished.
    Viktor · 4 years ago
    Thanks for the code, pretty much what I needed.

    Two remarks, though:
    1) when I run the macro in the same row as the "helper"cell (ZZ1), autofit (line 26) will mess up, because the whole string is fitted into one narow cell. I recommend adding

    oRange.WrapText = False

    in the beginning (or moving the helper cell somewhere out of the way, if possible).
    2) what's the purpose of line 19? You claculate oldWidth in lines 16-18, but then override the calculation in line 19, using only two columns. When I tried the sub on a three-column-wide merged cells, it worked better when I ignored the line...

    Thanks again
  • To post as a guest, your comment is unpublished.
    Afshin · 5 years ago
    Thanks a lot for the code! I have same problem with this code such as DANIËLLE_01.
  • To post as a guest, your comment is unpublished.
    Daniëlle_01 · 5 years ago
    Thanks a lot for the code! It finally works, but... My row height becomes too height. Is there a solution for?

    Thanks a lot!

    [u]This is my code:[/u]

    Option Explicit
    Public Sub AutoFitAll()
    Call AutoFitMergedCells(Range("b162:i162"))
    Call AutoFitMergedCells(Range("b166:i166"))
    Call AutoFitMergedCells(Range("b168:i168"))
    Call AutoFitMergedCells(Range("b170:i170"))
    Call AutoFitMergedCells(Range("b172:i172"))
    End Sub
    Public Sub AutoFitMergedCells(oRange As Range)
    Dim tHeight As Integer
    Dim iPtr As Integer
    Dim oldWidth As Single
    Dim oldZZWidth As Single
    Dim newWidth As Single
    Dim newHeight As Single
    With Sheets("Rapport")
    oldWidth = 0
    For iPtr = 1 To oRange.Columns.Count
    oldWidth = oldWidth + .Cells(1, oRange.Column + iPtr - 1).ColumnWidth
    Next iPtr
    oldWidth = .Cells(1, oRange.Column).ColumnWidth + .Cells(1, oRange.Column + 1).ColumnWidth
    oRange.MergeCells = False
    newWidth = Len(.Cells(oRange.Row, oRange.Column).Value)
    oldZZWidth = .Range("ZZ1").ColumnWidth
    .Range("ZZ1") = Left(.Cells(oRange.Row, oRange.Column).Value, newWidth)
    .Range("ZZ1").WrapText = True
    .Columns("ZZ").ColumnWidth = oldWidth
    .Rows("1").EntireRow.AutoFit
    newHeight = .Rows("1").RowHeight / oRange.Rows.Count
    .Rows(CStr(oRange.Row) & ":" & CStr(oRange.Row + oRange.Rows.Count - 1)).RowHeight = newHeight
    oRange.MergeCells = True
    oRange.WrapText = True
    .Range("ZZ1").ClearContents
    .Range("ZZ1").ColumnWidth = oldZZWidth
    End With
    End Sub
    • To post as a guest, your comment is unpublished.
      Aperture · 4 years ago
      Because the "helper" cell of ZZ1 is using the first row (column ZZ, row 1), if there is ANYTHING in row 1 taller than the text in the row you want to adjust, your resulting height will be taller than what you want.

      To fix this, I made the helper cell the same column as the first column in the oRange and set the row number to the very last row in Excel. Hope this helps you like it does me. 8)

      [u]My Code:[/u]

      Option Explicit
      Public Sub AutoFitAll()
      Call AutoFitMergedCells(Range("A2:Z2"))

      End Sub
      Public Sub AutoFitMergedCells(oRange As Range)
      Dim tHeight As Integer
      Dim iPtr As Integer
      Dim oldWidth As Single
      Dim oldZZWidth As Single
      Dim newWidth As Single
      Dim newHeight As Single
      With Sheets("Sheet1")
      oldWidth = 0
      For iPtr = 1 To oRange.Columns.Count
      oldWidth = oldWidth + .Cells(1, oRange.Column + iPtr - 1).ColumnWidth
      Next iPtr
      oldWidth = .Cells(1, oRange.Column).ColumnWidth + .Cells(1, oRange.Column + 1).ColumnWidth
      oRange.MergeCells = False
      newWidth = Len(.Cells(oRange.Row, oRange.Column).Value)
      oldZZWidth = .Cells("1048576", oRange.Column).ColumnWidth
      .Cells("1048576", oRange.Column) = Left(.Cells(oRange.Row, oRange.Column).Value, newWidth)
      .Cells("1048576", oRange.Column).WrapText = True
      .Columns(oRange.Column).ColumnWidth = oldWidth
      .Rows("1048576").EntireRow.AutoFit
      newHeight = .Rows("1048576").RowHeight / oRange.Rows.Count
      .Rows(CStr(oRange.Row) & ":" & CStr(oRange.Row + oRange.Rows.Count - 1)).RowHeight = newHeight
      oRange.MergeCells = True
      oRange.WrapText = True
      .Cells("1048576", oRange.Column).ClearContents
      .Cells("1048576", oRange.Column).ColumnWidth = oldZZWidth
      End With
      End Sub
    • To post as a guest, your comment is unpublished.
      Viktor · 4 years ago
      Hi Danielle,

      I ran into the same problem, when running the macro a second time in the first row. The code uses

      .Rows("1").EntireRow.AutoFit (line 26)

      and if you run in on, say A1:B1, your A1 cell has WordWrapping set to ON from line 30.

      The easiest solution seems to be switching WordWrapping off at the beginning of the sub. Add

      oRange.WrapText = True

      between lines 13 and 14 and you should be OK.
  • To post as a guest, your comment is unpublished.
    Dina · 5 years ago
    I have the following entered, but I get an error message "Run-time error '13': Type mismatch" Help?


    Option Explicit
    Public Sub AutoFitAll()
    Call AutoFitMergedCells(Range("a8:h8"))
    Call AutoFitMergedCells(Range("a10:h10"))
    Call AutoFitMergedCells(Range("a11:h11"))
    Call AutoFitMergedCells(Range("b17:h17"))
    Call AutoFitMergedCells(Range("b22:h22"))
    Call AutoFitMergedCells(Range("b24:h24"))
    Call AutoFitMergedCells(Range("a26:h26"))
    Call AutoFitMergedCells(Range("a28:h28"))

    End Sub
    Public Sub AutoFitMergedCells(oRange As Range)
    Dim tHeight As Integer
    Dim iPtr As Integer
    Dim oldWidth As Single
    Dim oldZZWidth As Single
    Dim newWidth As Single
    Dim newHeight As Single
    With Sheets("Offer Letter")
    oldWidth = 0
    For iPtr = 1 To oRange.Columns.Count
    oldWidth = oldWidth + .Cells(1, oRange.Column + iPtr - 1).ColumnWidth
    Next iPtr
    oldWidth = .Cells(1, oRange.Column).ColumnWidth + .Cells(1, oRange.Column + 1).ColumnWidth
    oRange.MergeCells = False
    newWidth = Len(.Cells(oRange.Row, oRange.Column).Value)
    oldZZWidth = .Range("ZZ1").ColumnWidth
    .Range("ZZ1") = Left(.Cells(oRange.Row, oRange.Column).Value, newWidth)
    .Range("ZZ1").WrapText = True
    .Columns("ZZ").ColumnWidth = oldWidth
    .Rows("1").EntireRow.AutoFit
    newHeight = .Rows("1").rowHeight / oRange.Rows.Count
    .Rows(CStr(oRange.Row) & ":" & CStr(oRange.Row + oRange.Rows.Count - 1)).rowHeight = newHeight
    oRange.MergeCells = True
    oRange.WrapText = True
    .Range("ZZ1").ClearContents
    .Range("ZZ1").ColumnWidth = oldZZWidth
    End With
    End Sub
  • To post as a guest, your comment is unpublished.
    Joe Dell · 5 years ago
    This code causes additional rows to be deleted. I have numbers on left side and columns next to it are merged/wrapped data. For example, in a Job description, list responsibilities with numbers followed by explanation of duty. Any thoughts? Thanks.