[Visual Basic] 纯文本查看 复制代码 Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet
Dim rng As Range
Dim col As Range
Dim beforeWidth As Double
Dim afterWidth As Double
Dim columnCounter As Integer
' 循环遍历所有工作表
For Each ws In ThisWorkbook.Worksheets
' 检查是否为当前工作表
If ws.Name <> "客户总览" Then
' 设置要调整列宽的范围
Set rng = ws.UsedRange
' 检查每一列是否需要自适应列宽
For columnCounter = 1 To rng.Columns.Count
' 获取调整列宽前的宽度
beforeWidth = rng.Columns(columnCounter).ColumnWidth
' 自适应当前列的列宽
rng.Columns(columnCounter).AutoFit
' 获取调整列宽后的宽度
afterWidth = rng.Columns(columnCounter).ColumnWidth
' 如果自适应后的宽度小于或等于调整前的宽度,则恢复调整前的宽度
If afterWidth <= beforeWidth Then
rng.Columns(columnCounter).ColumnWidth = beforeWidth
End If
Next columnCounter
End If
Next ws
End Sub
在客户总览的数据改变事件下,写入以下代码
PS: 可以继续优化修改第几行只判断第几个表
示例文件如下: 示例文件 密码:52pj |