我有一个VBA代码,可以自动拟合具有较大值的合并单元格,但它不起作用
Sub test1()
Sheets(1).Cells(2, 1).Value = " Testing Testing TestingTestingTesting Testing Testing TestingTestingTesting Testing TestingTesting Testing Testing TestingTesting Testing Testing Testing TestingTesting Testing TestingTesting Testing Testing Testing Testing Testing Testing Testing TestingTestingTesting Testing Testing Testing TestingTestingTesting Testing Testing TestingTestingTesting Testing TestingTesting Testing Testing TestingTesting Testing Testing Testing TestingTesting Testing TestingTesting Testing Testing Testing Testing Testing Testing Testing TestingTestingTesting Testing Testing Testing TestingTestingTesting Testing Testing TestingTestingTesting Testing TestingTesting Testing Testing TestingTesting Testing Testing Testing TestingTesting Testing TestingTesting Testing Testing Testing Testing Testing Testing Testing TestingTestingTesting Testing"
Range("A2:C2").Merge
Range("A2:C2").WrapText = True
Range("A2:C2").Rows.AutoFit
End Sub
当前工作输出:
需要输出:
答案 0 :(得分:0)
更改工作表的名称和范围,然后尝试以下代码(该代码不会合并但不会自动合并。对于本文https://support.microsoft.com/en-us/help/212010/you-cannot-use-the-autofit-feature-for-rows-or-columns-that-contain-me,您不能自动调整合并的列或行)。
Sub Test()
'
With Sheet2.Range("A1")
.Value = "Process inform HMI finish oil fiber display not show we check found HMI damage after we check no have spare after we move HMI alarm display at control spinning to install. But the type of HMI not same. To convert graphic display.After transfer to HMI and test operation finished. Test system with process is working normal."
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = True
End With
Sheet2.Columns("A:A").EntireColumn.AutoFit
End Sub
答案 1 :(得分:0)
您可以检查以下代码以自动拟合合并的单元格。
A到C列的单元格宽度分别设置为12,对于高度,文本长度的45%的比例取决于此宽度,如果要更改宽度,还必须更改比例
Sub test1()
Sheets(1).Cells(2, 1).Value = " Testing Testing TestingTestingTesting Testing Testing TestingTestingTesting Testing TestingTesting Testing Testing TestingTesting Testing Testing Testing TestingTesting Testing TestingTesting Testing Testing Testing Testing Testing Testing Testing TestingTestingTesting Testing"
Range("A2:C2").Merge
Range("A2:C2").WrapText = True
Columns("A:C").ColumnWidth = 12
text_length = Len(Sheets(1).Cells(2, 1).Value)
Rows("2:2").RowHeight = text_length * 0.45
End Sub