With Columns("B:B") .ColumnWidth = 7.5 End With With Columns("E:E") .ColumnWidth = 3.08 End With With Columns("I:I") .ColumnWidth = 3.08 End With 优化
时间: 2023-07-16 14:11:36 浏览: 76
您可以将这些代码优化为以下形式:
```
Columns("B:B").ColumnWidth = 7.5
Columns("E:E").ColumnWidth = 3.08
Columns("I:I").ColumnWidth = 3.08
```
这样可以更简洁地设置列宽,并且不需要使用 "With" 语句。
相关问题
Dim ws As Worksheet Dim lastRow As Long Set ws = ThisWorkbook.Worksheets("Sheet1") '清除格式和删除行列 ws.Cells.ClearFormats ws.Range("1:2").Delete Shift:=xlUp ws.Range("A:A,B:B,C:C,F:F,G:G,I:I,J:J,K:K,M:M,P:P,Q:Q,S:S,T:T").Delete Shift:=xlToLeft '添加新列 ws.Range("H1").Value = "回收时间" ws.Range("K1").Value = "回收人" ws.Range("L1").Value = "复核人" ws.Columns("E:E").Copy Destination:=ws.Columns("I:I") ws.Columns("F:F").Copy Destination:=ws.Columns("J:J") '筛选数据 ws.Range("A:D").AutoFilter ws.Range("A:D").AutoFilter Field:=1, Criteria1:="<>tt", VisibleDropDown:=False ws.Range("A:D").AutoFilter Field:=2, Criteria1:="<>996999", VisibleDropDown:=False ws.Range("A:D").AutoFilter Field:=3, Criteria1:="<>996999", VisibleDropDown:=False ws.Range("A:D").AutoFilter Field:=4, Criteria1:="<>*贴", Operator:=xlAnd, Criteria2:="<>*片", VisibleDropDown:=False '排序数据 With ws.Sort .SortFields.Clear .SortFields.Add2 Key:=ws.Range("A2:A500"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal .SetRange ws.Range("A1:L500") .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With With ActiveSheet .Columns.AutoFit .Rows.AutoFit .Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove .Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove .Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove End With '合并单元格 With ws.Range("A1:L1") .Merge .Value = "yyy" End With '调整列宽 ws.Columns("B:B").ColumnWidth = 7.5 ws.Columns("E:E,I:I").ColumnWidth = 3.08 End Sub 改进
Sub UpdateData()
Dim ws As Worksheet
Dim lastRow As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
'清除格式和删除行列
With ws
.Cells.ClearFormats
.Range("1:2").Delete Shift:=xlUp
.Range("A:A,B:B,C:C,F:F,G:G,I:I,J:J,K:K,M:M,P:P,Q:Q,S:S,T:T").Delete Shift:=xlToLeft
'添加新列
.Range("H1").Value = "回收时间"
.Range("K1").Value = "回收人"
.Range("L1").Value = "复核人"
'复制列
.Columns("E:E").Copy Destination:=.Columns("I:I")
.Columns("F:F").Copy Destination:=.Columns("J:J")
'筛选数据
.Range("A:D").AutoFilter
.Range("A:D").AutoFilter Field:=1, Criteria1:="<>tt", VisibleDropDown:=False
.Range("A:D").AutoFilter Field:=2, Criteria1:="<>996999", VisibleDropDown:=False
.Range("A:D").AutoFilter Field:=3, Criteria1:="<>996999", VisibleDropDown:=False
.Range("A:D").AutoFilter Field:=4, Criteria1:="<>*贴", Operator:=xlAnd, Criteria2:="<>*片", VisibleDropDown:=False
'排序数据
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
With .Sort
.SortFields.Clear
.SortFields.Add Key:=ws.Range("A2:A" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange ws.Range("A1:L" & lastRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'合并单元格
.Range("A1:L1").Merge
.Range("A1").Value = "yyy"
'调整列宽
.Columns("B:B").ColumnWidth = 7.5
.Columns("E:E,I:I").ColumnWidth = 3.08
'调整行高和列宽
.Cells.EntireRow.AutoFit
.Columns.AutoFit
'插入空白行
.Rows("1:3").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End With
End Sub
ActiveSheet.Columns(5).ColumnWidth = 16 ActiveSheet.Columns(6).ColumnWidth = 18 ActiveSheet.Columns(7).ColumnWidth = 14 ActiveSheet.Columns(8).ColumnWidth = 24 ActiveSheet.Cells(2, 5).Value = "泥蜞 镫囹彐?" ActiveSheet.Cells(2, 6).Value = "沛屐. 想囹告" ActiveSheet.Cells(2, 7).Value = "袜麒耠屙磬" ActiveSheet.Cells(2, 8).Value = "务蜞蝾?铖眍忭钽?漕脬?"什么事呀
这也是一段 VBA 代码,用于设置 Excel 表格的列宽和单元格的值。它将第5列宽度设置为16,第6列宽度设置为18,第7列宽度设置为14,第8列宽度设置为24。然后,它在第2行的第5列到第8列的单元格中填充了一些文本值。这些文本值可能是表格中的列标题或者数据。这样可以让表格看起来更整齐、更美观,也方便用户查看和理解数据。
阅读全文