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 改进
时间: 2023-07-14 17:14:30 浏览: 482
Excel-VBA宏编程实例源代码-合并sheet.zip
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
阅读全文