英文:
For all active sheets in a workbook
问题
我有一个VB脚本来自动调整合并单元格的高度,但它只适用于工作簿的第一个工作表/当前工作表。
这个VBA脚本最初来自于使用VBA自动调整合并单元格,我稍微改变了一些范围。
我想让所有可用/活动工作表运行相同的VB脚本。所以,我在"AutofixMergedCellHeight" VB脚本中添加了以下"SelectedSheets"代码,但它没有起作用。
有人可以帮忙吗?非常感谢!
AutofixMergedCellHeight:
Sub FixMerged()
Dim mw As Single
Dim cM As Range
Dim rng As Range
Dim cw As Double
Dim rwht As Double
Dim ar As Variant
Dim i As Integer
Application.ScreenUpdating = False
'下面是单元格范围,根据需要更改。
ar = Array("B32", "B33")
For i = 1 To UBound(ar)
On Error Resume Next
Set rng = Range(Range(ar(i)).MergeArea.Address)
With rng
.MergeCells = False
cw = .Cells(1).ColumnWidth
mw = 0
For Each cM In rng
cM.WrapText = True
mw = cM.ColumnWidth + mw
Next
mw = mw + rng.Cells.Count * 0.66
.Cells(1).ColumnWidth = mw
.EntireRow.AutoFit
rwht = .RowHeight
.Cells(1).ColumnWidth = cw
.MergeCells = True
.RowHeight = rwht
End With
Next i
Application.ScreenUpdating = True
End Sub
我还添加了一个新的脚本,通过使用'For Each'循环遍历工作簿中的所有工作表,并插入了对"SelectSheets"和"FixMerged"的调用。但这种方式也只适用于当前单个工作表。
Sub WorksheetLoop()
Dim Current As Worksheet
For Each Current In Worksheets
SelectSheets
FixMerged
Next
End Sub
英文:
I have a VB script to autofit merged cells height, but it only works for the first sheet/current sheet of the workbook.
The VBA Script is originally from Autofit Merged Cells with VBA, and I made some slightly range change.
I'd like to make all my available/active sheets to run this same VB script. So, I added the following SlectedSheets code to the AutofixMergedCellHeigh VB script, but it didn't work.
Does anyone could help out? Thanks a lot!
AutofixMergedCellHeight:
Sub FixMerged()
Dim mw As Single
Dim cM As Range
Dim rng As Range
Dim cw As Double
Dim rwht As Double
Dim ar As Variant
Dim i As Integer
Application.ScreenUpdating = False
'Cell Ranges below, change to suit.
ar = Array("B32", "B33")
For i = 1 To UBound(ar)
On Error Resume Next
Set rng = Range(Range(ar(i)).MergeArea.Address)
With rng
.MergeCells = False
cw = .Cells(1).ColumnWidth
mw = 0
For Each cM In rng
cM.WrapText = True
mw = cM.ColumnWidth + mw
Next
mw = mw + rng.Cells.Count * 0.66
.Cells(1).ColumnWidth = mw
.EntireRow.AutoFit
rwht = .RowHeight
.Cells(1).ColumnWidth = cw
.MergeCells = True
.RowHeight = rwht
End With
Next i
Application.ScreenUpdating = True
End Sub
I also add a new script to loop through all of the worksheets in the workbook by using a 'For Each' loop, and inserted calls for SelectSheets and FixMerged. But this way only works on the current single sheet too.
Sub WorksheetLoop()
Dim Current As Worksheet
For Each Current In Worksheets
SelectSheets
FixMerged
Next
End Sub
答案1
得分: 2
将工作表对象传递给另一个过程
调用过程
Sub FixMergedAll()
Dim wb As Workbook: Set wb = ThisWorkbook ' 包含此代码的工作簿
Application.ScreenUpdating = False
Dim ws As Worksheet
For Each ws In wb.Worksheets
FixMergedCells ws
Next ws
Application.ScreenUpdating = True
End Sub
被调用的过程
Sub FixMergedCells(ByVal ws As Worksheet) ' ***
Dim mw As Single
Dim cM As Range
Dim rng As Range
Dim cw As Double
Dim rwht As Double
Dim ar As Variant
Dim i As Long ' ***
' 下面是单元格范围,请根据需要进行更改。
ar = Array("B32", "B33")
For i = LBound(ar) To UBound(ar) ' ***
On Error Resume Next
Set rng = ws.Range(ar(i)).MergeArea ' ***
With rng
.MergeCells = False
cw = .Cells(1).ColumnWidth
mw = 0
For Each cM In rng
cM.WrapText = True
mw = cM.ColumnWidth + mw
Next
mw = mw + rng.Cells.Count * 0.66
.Cells(1).ColumnWidth = mw
.EntireRow.AutoFit
rwht = .RowHeight
.Cells(1).ColumnWidth = cw
.MergeCells = True
.RowHeight = rwht
End With
Next i
End Sub
英文:
Passing a Worksheet Object to Another Procedure
The Calling Procedure
<!-- language: lang-vb -->
Sub FixMergedAll()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Application.ScreenUpdating = False
Dim ws As Worksheet
For Each ws In wb.Worksheets
FixMergedCells ws
Next ws
Application.ScreenUpdating = True
End Sub
The Called Procedure
' ***
designates the locations of the introduced changes. For the rest, you're on your own.
<!-- language: lang-vb -->
Sub FixMergedCells(ByVal ws As Worksheet) ' ***
Dim mw As Single
Dim cM As Range
Dim rng As Range
Dim cw As Double
Dim rwht As Double
Dim ar As Variant
Dim i As Long ' ***
'Cell Ranges below, change to suit.
ar = Array("B32", "B33")
For i = LBound(ar) To UBound(ar) ' ***
On Error Resume Next
Set rng = ws.Range(ar(i)).MergeArea ' ***
With rng
.MergeCells = False
cw = .Cells(1).ColumnWidth
mw = 0
For Each cM In rng
cM.WrapText = True
mw = cM.ColumnWidth + mw
Next
mw = mw + rng.Cells.Count * 0.66
.Cells(1).ColumnWidth = mw
.EntireRow.AutoFit
rwht = .RowHeight
.Cells(1).ColumnWidth = cw
.MergeCells = True
.RowHeight = rwht
End With
Next i
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论