Discussion:
excel後沒自動調整列高功能的問題
(时间太久无法回复)
笨笨魚
2005-09-08 08:31:19 UTC
Permalink
小弟在sheet1將a1、a2儲存格合併
並設定自動換列
但想不調整欄寬條件下
列高能自動調整
目前excel似乎沒這樣功能耶
可以自設excel vba來增加此項功能嗎?
以下為小弟所寫的程式碼
可是自動調整列高部分沒有反應
Sub 列高調整()
Set b = Sheets("a")
b.Activate
For i = 3 To 1000
If b.Cells(i, 2) <> "" Then
b.Range(Cells(i, 1), Cells(i, 2)).MergeCells = True
b.Range(Cells(i, 1), Cells(i, 2)).WrapText = True
b.Range(Cells(i, 1), Cells(i, 2)).EntireRow.AutoFit
End If
Next i
End Sub
可否請各位大大幫小弟解惑?
--
請多多指教
chijanzen
2005-09-08 10:39:01 UTC
Permalink
你好:

請參考微軟知識文件庫:無法自動調整包含合併儲存格的欄寬或列高

http://support.microsoft.com/kb/212010/zh-tw

你就會知道無法自動調整的,所以只好用人工來做了

請參考以下程式碼

Sub 列高調整()
Set b = Sheets("a")
b.Activate
For i = 3 To 20
If b.Cells(i, 2) <> "" Then
b.Range(Cells(i, 1), Cells(i, 2)).MergeCells = True
b.Range(Cells(i, 1), Cells(i, 2)).WrapText = True
Call AutoFitMergedCellRowHeight(b.Range(Cells(i, 1), Cells(i, 2)))
End If
Next i
End Sub

Sub AutoFitMergedCellRowHeight(rng As Range)
rWidth = 0
With rng(1, 1).MergeArea
If .Rows.Count = 1 And .WrapText = True Then
Application.ScreenUpdating = False
CurrentRowHeight = .RowHeight
rWidth = rng.ColumnWidth
For Each CurrCell In rng
MergedCellRgWidth = CurrCell.ColumnWidth + _
MergedCellRgWidth
Next
.MergeCells = False
.Cells(1).ColumnWidth = MergedCellRgWidth
.EntireRow.AutoFit
PossNewRowHeight = .RowHeight
.Cells(1).ColumnWidth = rWidth
.MergeCells = True
.RowHeight = IIf(CurrentRowHeight > PossNewRowHeight, _
CurrentRowHeight, PossNewRowHeight)
End If
End With
End Sub
--
天行健君子以自強不息

http://www.vba.com.tw/plog/


"笨笨魚" 來函:
Post by 笨笨魚
小弟在sheet1將a1、a2儲存格合併
並設定自動換列
但想不調整欄寬條件下
列高能自動調整
目前excel似乎沒這樣功能耶
可以自設excel vba來增加此項功能嗎?
以下為小弟所寫的程式碼
可是自動調整列高部分沒有反應
Sub 列高調整()
Set b = Sheets("a")
b.Activate
For i = 3 To 1000
If b.Cells(i, 2) <> "" Then
b.Range(Cells(i, 1), Cells(i, 2)).MergeCells = True
b.Range(Cells(i, 1), Cells(i, 2)).WrapText = True
b.Range(Cells(i, 1), Cells(i, 2)).EntireRow.AutoFit
End If
Next i
End Sub
可否請各位大大幫小弟解惑?
--
請多多指教
继续阅读narkive:
Loading...