2013-10-29 82 views
0

我的目標是合併單元格的高度自動調整爲其內容。這種用於一個小區正常工作與這段代碼:調整兩個合併單元格的高度

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) 
    Dim CurrentRowHeight As Single, MergedCellRgWidth As Single 
    Dim h, rng As Range 

    Set rng = Selection 

    If ActiveCell.MergeCells Then 
     With ActiveCell.MergeArea 
      If .WrapText = True Then 
       With rng 
        .UnMerge 
        .Cells(1).EntireRow.AutoFit 
        h = .Cells(1).RowHeight 
        .Merge 
        .EntireRow.AutoFit 

        With .Cells(1).MergeArea 
         .Cells(.Cells.Count).RowHeight = (h - .Height + 14.25) 
        End With 
       End With 
      End If 
     End With 
    End If 
End Sub 

但是,如果我具有相同的行中的兩個單元,第二個較短它調整到第二個..(參見下面的示例)

picture

我如何能解決這一問題,以便它只有調節,當出現在同一行中與更多的高度沒有任何單元格的想法?

這是一個更新的版本。順便說一句。中的細胞都是在同一列(AS和AU)

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 


Dim CurrentRowHeight As Single, MergedCellRgWidth As Single 
If ActiveCell.MergeCells Then 


Dim heigtAS, heightAU As Integer 

    'AS-Block 
Dim hAS, rngAS As Range 
    Set rngAS = Range("AS10:AS18") 

    With rngAS.MergeArea 
     If .WrapText = True Then 
      With rngAS 
      .UnMerge 
      .Cells(1).EntireRow.AutoFit 
      hAS = .Cells(1).RowHeight 
      .Merge 
      .EntireRow.AutoFit 
      With .Cells(1).MergeArea 
      heightAS = (hAS - .Height + 14.25) 
      'save height of cell 

      End With 
      End With 

     End If 
    End With 

'AU-Block 
Dim hAU, rngAU As Range 
Set rngAU = Range("AU10:AU18") 

    With rngAU.MergeArea 
     If .WrapText = True Then 
      With rngAU 
      .UnMerge 
      .Cells(1).EntireRow.AutoFit 
      hAU = .Cells(1).RowHeight 
      .Merge 
      .EntireRow.AutoFit 
      With .Cells(1).MergeArea 
      heightAU = (hAU - .Height + 14.25) 
      'save height of cell 



      End With 
      End With 

     End If 
    End With 


'Compare height and fit cell height 
If heightAS > heightAU Then 
    .Cells(.Cells.Count).RowHeight = heightAS 
Else 
    .Cells(.Cells.Count).RowHeight = heightAU 
End If 
End If 


End Sub 

我有點不能讓它工作...

+2

對不起......你的例子有什麼問題?它沒有根據內容進行調整嗎? – sam092

+0

這個例子沒有錯,但是如果我在較小的單元格上雙擊,它會調整它的高度,我需要一個解決方案,它不會這樣做,除非它是該行中具有最大高度的單元格。 – MarMarko

+1

似乎沒有安裝在行中的每個單元格,你不會知道哪一個應該是最大的。你必須跑過每一列並跟蹤每一列的高度(或者至少跟蹤最大安裝高度)。如果擬合後續單元格會導致較小的行高,請將其重置爲最大值。 –

回答

0
private void MergeAndFit(Range range) 
    { 
     range.MergeCells = false; 
     double cellWidth = range.Columns[1].ColumnWidth; 
     double mergeWidth = 0; 
     foreach (Range cm in range) 
     { 
      cm.WrapText = true; 
      mergeWidth += cm.ColumnWidth; 
     } 

     mergeWidth = mergeWidth + range.Cells.Count * 0.66; 
     range.Columns[1].ColumnWidth = mergeWidth; 
     range.EntireRow.AutoFit(); 
     double newRowHeight = range.RowHeight; 
     range.Columns[1].ColumnWidth = cellWidth; 
     range.MergeCells = true; 
     range.RowHeight = newRowHeight; 
    } 

上面的函數有一個Excel的範圍對象和合並單元格。將行高設置爲最大以顯示所有內容。我不確定這是否適合您,但您可以將其轉換爲vb.net並告訴我它是否可用。

相關問題