ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
楼主: 621107

[分享] 合并单元格也能"最适合行高"

  [复制链接]

TA的精华主题

TA的得分主题

发表于 2014-2-7 12:13 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
本帖最后由 ythtdewo 于 2014-2-7 12:22 编辑

发现一个问题,想请教大家。
如果内容有上标的话,会出现算出来的高度不够的情况,应该怎么解决好呢?

下面是只选了D2单元格的时候做调整
调整前:
调整前.jpg
调整后:
调整后.png
可以看到最下面不能表示完全。。。

TA的精华主题

TA的得分主题

 楼主| 发表于 2014-6-12 17:05 | 显示全部楼层
本帖最后由 621107 于 2014-6-12 18:14 编辑
ythtdewo 发表于 2014-2-7 12:13
发现一个问题,想请教大家。
如果内容有上标的话,会出现算出来的高度不够的情况,应该怎么解决好呢?

你这个上标是怎么弄出来的.. 上传上来我试试,
我原来的帖子好像没办法编辑了 我修改了下代码 修复了代码中一个复制的问题 你再试试看行不行:

  1. Sub My_MergeCell_AutoHeight()
  2.     Dim rh As Single, mw As Single
  3.     Dim rng As Range, rrng As Range, n1%, n2%
  4.     Dim aw As Single, rh1 As Single
  5.     Dim m$, n$, k
  6.     Dim ir1, ir2, ic1, ic2
  7.     Dim mySheet As Worksheet
  8.     Dim selectedA As Range
  9.     Dim wrkSheet As Worksheet
  10.    
  11.     Application.ScreenUpdating = False
  12.     Set mySheet = ActiveSheet
  13.     On Error Resume Next
  14.     Err.Number = 0
  15.     Set selectedA = Application.Intersect(ActiveWindow.RangeSelection, mySheet.UsedRange)
  16.     selectedA.Activate
  17.     If Err.Number <> 0 Then
  18.     g = MsgBox("请先选择需要'最合适行高'的行!", vbInformation)
  19.     Return
  20.     End If
  21.     selectedA.EntireRow.AutoFit
  22.     Set wrkSheet = ActiveWorkbook.Worksheets.Add
  23.     For Each rrng In selectedA
  24.         If rrng.Address <> rrng.MergeArea.Address Then
  25.             If rrng.Address = rrng.MergeArea.Item(1).Address Then
  26.                
  27.                 'If (Application.Intersect(selectedA, rrng).Address <> rrng.Address) Then
  28.                 '    GoTo gotoNext
  29.                 'End If
  30.                
  31.                 Dim tempCell As Range
  32.                 Dim width As Double
  33.                 Dim tempcol
  34.                 width = 0
  35.                 For Each tempcol In rrng.MergeArea.Columns
  36.                     width = width + tempcol.ColumnWidth
  37.                 Next
  38.                 wrkSheet.Columns(1).WrapText = True
  39.                 wrkSheet.Columns(1).ColumnWidth = width
  40.                 wrkSheet.Columns(1).Font.Size = rrng.Font.Size
  41.                 rrng.Copy Destination:=wrkSheet.Cells(1, 1)
  42.                 'wrkSheet.Cells(1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
  43.                 wrkSheet.Activate
  44.                 wrkSheet.Cells(1, 1).RowHeight = 0
  45.                 wrkSheet.Cells(1, 1).EntireRow.Activate
  46.                 wrkSheet.Cells(1, 1).EntireRow.AutoFit
  47.                 mySheet.Activate
  48.                 rrng.Activate
  49.                 If (rrng.RowHeight < wrkSheet.Cells(1, 1).RowHeight) Then
  50.                     Dim tempHeight As Double
  51.                     Dim tempCount As Integer
  52.                     tempHeight = wrkSheet.Cells(1, 1).RowHeight
  53.                     tempCount = rrng.MergeArea.Rows.Count
  54.                     For Each addHeightRow In rrng.MergeArea.Rows
  55.                     
  56.                         If (addHeightRow.RowHeight < tempHeight / tempCount) Then
  57.                             addHeightRow.RowHeight = tempHeight / tempCount
  58.                         End If
  59.                         tempHeight = tempHeight - addHeightRow.RowHeight
  60.                         tempCount = tempCount - 1
  61.                     Next
  62.                 End If
  63.             End If
  64.         End If
  65.     Next
  66.     Application.DisplayAlerts = False '删除工作表警告提示去消
  67.     wrkSheet.Delete
  68.     Application.DisplayAlerts = True
  69.     Application.ScreenUpdating = True
  70. End Sub
复制代码


评分

2

查看全部评分

TA的精华主题

TA的得分主题

发表于 2014-7-24 10:01 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2014-8-21 23:10 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 zhouandke 于 2014-8-21 23:21 编辑

看了大家的思路, 我也写了一个函数, 方便大家阅读理解
思路:
1. 将合并区域的内容复制到一个新表的 range("A1")
2. 拆分 Range("A1"), 让所有内容都集中在 cells(1,1)
3. 让cells(1,1)的列宽等于 源range所有column的列宽
4. 让cells(1,1)自动调整行高
5. 调整合并区域的行高

下面的代码适用win7 + excel 2007, 写的是过程, 调用时, r 是要调整的合并区域, minHeight 是最小的行高
适用于宏的代码, 请自行修改一下

  1. Sub AdjuestRangeHeight(r As Range, minHeight As Integer)
  2.     Dim TempSheet As Worksheet
  3.     Set TempSheet = Worksheets.Add()
  4.     TempSheet.Visible = xlSheetHidden
  5.    
  6.     '带格式的复制所有内容到 TempSheet.Range("A1")
  7.     r.Copy
  8.     TempSheet.Range("A1").PasteSpecial xlPasteAll
  9.     '拆分 TempSheet.Range("A1"), 让所有内容都集中在 cells(1,1)
  10.     TempSheet.Range("A1").UnMerge
  11.     '让cells(1,1)的列宽等于 源range所有column的列宽
  12.     TempSheet.Cells(1, 1).ColumnWidth = 0
  13.     For c = r.Row To r.MergeArea.Count + r.Row - 1
  14.         TempSheet.Cells(1, 1).ColumnWidth = TempSheet.Range("A1").ColumnWidth + r.ColumnWidth
  15.     Next
  16.     '让cells(1,1)自动调整行高
  17.     TempSheet.Cells(1, 1).EntireRow.AutoFit
  18.    
  19.     If TempSheet.Cells(1, 1).RowHeight > minHeight Then
  20.         r.RowHeight = TempSheet.Cells(1, 1).RowHeight
  21.     Else
  22.         r.RowHeight = minHeight
  23.     End If
  24.    
  25.     Application.DisplayAlerts = False
  26.     TempSheet.Delete
  27.     Application.DisplayAlerts = True
  28. End Sub
复制代码

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2014-8-22 06:22 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
好贴,多谢分享!

TA的精华主题

TA的得分主题

发表于 2014-9-2 10:11 | 显示全部楼层
zhouandke 发表于 2014-8-21 23:10
看了大家的思路, 我也写了一个函数, 方便大家阅读理解
思路:
1. 将合并区域的内容复制到一个新表的 range ...

更新以下代码
Sub AdjuestRangeHeight(r As Range, minHeight As Integer)
    Dim TempSheet As Worksheet
    Set TempSheet = Worksheets.Add()
    TempSheet.Visible = xlSheetHidden
   
    '带格式的复制所有内容到 TempSheet.Range("A1")
    r.Copy
    TempSheet.Range("A1").PasteSpecial xlPasteAll
    '拆分 TempSheet.Range("A1"), 让所有内容都集中在 cells(1,1)
    TempSheet.Range("A1").UnMerge
    '让cells(1,1)的列宽等于 源range所有column的列宽
    TempSheet.Cells(1, 1).ColumnWidth = 0
    For c = 1 To r.MergeArea.Count
       TempSheet.Cells(1, 1).ColumnWidth = TempSheet.Cells(1, 1).ColumnWidth + r.Cells(1, c).ColumnWidth
    Next
    '让cells(1,1)自动调整行高
    TempSheet.Cells(1, 1).EntireRow.AutoFit
   
    If TempSheet.Cells(1, 1).RowHeight > minHeight Then
        r.RowHeight = TempSheet.Cells(1, 1).RowHeight
    Else
        r.RowHeight = minHeight
    End If
   
    Application.DisplayAlerts = False
    TempSheet.Delete
    Application.DisplayAlerts = True
   
End Sub

TA的精华主题

TA的得分主题

发表于 2014-9-2 11:45 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
收藏学习!!!!!!

TA的精华主题

TA的得分主题

发表于 2014-10-27 14:21 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2014-11-12 11:10 | 显示全部楼层
谢谢 621107的辛苦付出及分享,赞一个。

TA的精华主题

TA的得分主题

发表于 2014-12-27 20:11 | 显示全部楼层
根据楼主程序略改了一下,适用于所有类型单元格的行高自适应

Sub 行高自适应()
'
' 行高(包括合并单元格的行高)自适应 宏
'
    On Error Resume Next
   
    If ActiveWorkbook.Final = False Then

        Application.ScreenUpdating = False

        If Selection.MergeCells Or IsNull(Selection.MergeCells) Then    '如果选定区域里有合并单元格
               
               Dim rrng As Range, n1%
               Dim mySheet As Worksheet
               Dim selectedA As Range
               Dim wrkbookTemp As Workbook
               Dim wrkSheet As Worksheet
        
               Set mySheet = ActiveSheet
               Set selectedA = Application.Intersect(ActiveWindow.RangeSelection, mySheet.UsedRange)
               Selection.EntireRow.AutoFit
               selectedA.Activate
               selectedA.EntireRow.AutoFit
               
               Set wrkbookTemp = Workbooks.Add
               Set wrkSheet = wrkbookTemp.Sheets("Sheet1")
               
               For Each rrng In selectedA
                   If rrng.Address <> rrng.MergeArea.Address Then
                       If rrng.Address = rrng.MergeArea.Item(1).Address Then
                           Dim width1 As Double
                           Dim Height1 As Double
                           Dim tempCol
                           Dim tempRow
                           width1 = 0
                           Height1 = 0
                           For Each tempCol In rrng.MergeArea.Columns
                               width1 = width1 + tempCol.ColumnWidth
                           Next
                           For Each tempRow In rrng.MergeArea.Rows
                               Height1 = Height1 + tempRow.RowHeight
                           Next
                           
                           rrng.Copy Destination:=wrkSheet.Cells(1, 1)
                           wrkSheet.Activate
                           Cells(1, 1).Select
                           With Selection
                                .WrapText = True
                                .ColumnWidth = width1
                                .EntireRow.AutoFit
                           End With
                           Dim Height2 As Double
                           Height2 = Cells(1, 1).RowHeight
                           
                           mySheet.Activate
                           rrng.Activate
                           
                           Dim Count1 As Integer
                           Count1 = rrng.MergeArea.Rows.Count
                           
                           If (Height1 < Height2) Then
                               For Each addHeightRow In rrng.MergeArea.Rows
        
                                   If (addHeightRow.RowHeight < Height2 / Count1) Then
                                       addHeightRow.RowHeight = Height2 / Count1
                                   End If
                                   Height2 = Height2 - addHeightRow.RowHeight
                                   Count1 = Count1 - 1
                               Next
                           End If
                           
                       End If
                   End If
               Next
               
               wrkbookTemp.Close False
        
            Set rrng = Nothing
            Set mySheet = Nothing
            Set selectedA = Nothing
            Set wrkbookTemp = Nothing
            Set wrkSheet = Nothing
            Set tempCol = Nothing
            Set tempRow = Nothing
        
        Else
            
            Selection.EntireRow.AutoFit
                       
        End If
        
        Application.ScreenUpdating = ture
   
    End If

End Sub

评分

3

查看全部评分

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-6-18 19:48 , Processed in 0.048762 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表