复制-操作代码
Sub fuzi()
Range("a1:a22").Copy
Range("b5").Select
ActiveSheet.Paste '在选定区域进行粘贴(含格式)'
Application.CutCopyMode = False
‘不带格式粘贴
Range("a1:a22").Copy
Range("c5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
要显示运行多少秒
Sub 运行多少秒()
t = Timer
lrow1 = Range("a1").CurrentRegion.Rows.Count
lrow2 = Range("e1").CurrentRegion.Rows.Count
Range("a2:a" & lrow1).Copy
Range("e2:e" & lrow2).Select
ActiveSheet.Paste
Selection.NumberFormatLocal = "yyyy/m/d"
''--------------------------------------------
lrow3 = Range("b1").CurrentRegion.Rows.Count
lrow4 = Range("f1").CurrentRegion.Rows.Count
Range("b2:b" & lrow3).Copy
Range("f2:f" & lrow4).Select
ActiveSheet.Paste
Selection.NumberFormatLocal = "0.00%"
''---------------------------------------------
lrow5 = Range("c1").CurrentRegion.Rows.Count
lrow6 = Range("g1").CurrentRegion.Rows.Count
Range("c2:c" & lrow5).Copy
Range("g2:g" & lrow6).Select
ActiveSheet.Paste
Selection.NumberFormatLocal = "G/通用格式"
''---------------------------------------------
lrow7 = Range("d1").CurrentRegion.Rows.Count
lrow8 = Range("h1").CurrentRegion.Rows.Count
Range("d2:d" & lrow7).Copy
Range("h2:f" & lrow8).Select
ActiveSheet.Paste
Selection.NumberFormatLocal = "@"
MsgBox Timer - t & "秒完成"
End Sub
向右插入一列-操作代码
Sub 向右插入一列()
Worksheets("向右插入一列").Select
Columns("d:d").Insert shift:=xlToRight
End Sub
要在sheet取消隐藏-操作
Sub 取消隐藏()
Worksheets("取消隐藏").Columns("b:h").EntireColumn.AutoFit '仅仅取消b - h列'
'Worksheets().UsedRange. EntireColumn.AutoFit'--整个表都取消
End Sub
要在单元格隐藏列-操作
Sub 隐藏()
Worksheets("隐藏").Select
Columns("A:H").ColumnWidth = 0
End Sub
判断统计() '等同于sumif功能'
Sub 判断统计() '等同于sumif功能'
Worksheets("判断统计").Select
Application.ScreenUpdating = False '关闭屏幕更新,看不到宏的执行过程,但提高宏运行速度
Application.EnableEvents = False '关闭事件,防止触发事情,提高运行速度
a = Range("a1").CurrentRegion.Rows.Count
For i = 2 To a
If Range("p" & i) > 90 Then
Range("u" & i) = "M3+"
ElseIf Range("p" & i) >= 61 Then
Range("u" & i) = "M3"
ElseIf Range("p" & i) >= 31 Then
Range("u" & i) = "M2"
Else: Range("u" & i) = "M1"
End If
Next
Range("Y21").Select
ActiveCell.FormulaR1C1 = "=SUMIFS(C[-13],C[-5],""M1"")"
Range("Y22").Select
ActiveCell.FormulaR1C1 = "=SUMIFS(C[-13],C[-5],""M2"")"
Range("Y23").Select
ActiveCell.FormulaR1C1 = "=SUMIFS(C[-13],C[-5],""M3"")"
Range("Y24").Select
ActiveCell.FormulaR1C1 = "=SUMIFS(C[-13],C[-5],""M3+"")"
Range("Z21").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(C[-6],""M1"")"
Range("Z22").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(C[-6],""M2"")"
Range("Z23").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(C[-6],""M3"")"
Range("Z24").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(C[-6],""M3+"")"
Application.EnableEvents = True
Application.ScreenUpdating = True '结束屏幕更新
End Sub
excel匹配
Sub 匹配()
Worksheets("匹配").Select
Columns("K").Clear
Range("K2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],C[-10]:C[-9],2,0)"
Selection.AutoFill Destination:=Range("K2:K17"), Type:=xlFillDefault
Range("K2:K17").Select
Selection.Copy
Range("K2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("M8").Select
Application.CutCopyMode = False
End Sub
Sub 匹配1()
Worksheets("匹配1").Select
Columns("B").Clear
Range("B2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],被匹配!C[-1]:C,2,0)"
Selection.AutoFill Destination:=Range("B2:B17"), Type:=xlFillDefault
Range("B2:B17").Select
Selection.Copy
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("M8").Select
Application.CutCopyMode = False
End Sub
排序-操作代码
Sub 排序()
Dim rng As Range
Set rng = Range("A1:A22")
rng.Sort key1:="CONTRACTNO", order1:=xlDescending, Header:=xlYes
End Sub
筛选-操作代码
Sub 筛选()
'筛选
irow = Range("a1").CurrentRegion.Rows.Count - 最大行
ActiveSheet.Range("1:A" & irow).AutoFilter field:=1, Criteria1:= _
"a15hhhhhhhhhh"
'复制A16
Range("A16").Select
Selection.Copy
'在G16的位置粘贴
Range("G16").Select
'带格式粘贴
ActiveSheet.Paste
'退出粘贴
Application.CutCopyMode = False
'退出筛选
ActiveSheet.Range("1:A" & irow).AutoFilter field:=1
End Sub
判断分组-类似excel判断分组
Sub 判断分组()
a = Range("a1").CurrentRegion.Rows.Count
For i = 2 To 43
If Range("A" & i) = "M1-1" Then
Range("D" & i) = Range("b" & i) / Range("c" & i) / 0.815
Else: Range("D" & i) = Range("b" & i) / Range("c" & i) / 0.095
End If
Next
End Sub
单元格相对引用值-excel操作
Sub 单元格相对引用值()
Range("A2").FormulaR1C1 = "=RC[1]&RC[2]"
End Sub
向下填充数据-excel操作代码
Sub 向下填充数据()
Range("b2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]&""200"""
Range("B2").Select
Selection.AutoFill Destination:=Range("B2:B22"), Type:=xlFillDefault
Range("B2:B22").Select
End Sub
表格显示隐藏-操作代码
Sub 表格显示隐藏()
Worksheets("表格显示隐藏").Visible = True '打开隐藏表格'
End Sub
格式调整--类似excel操作
Sub 格式调整()
lrow1 = Range("a1").CurrentRegion.Rows.Count
lrow2 = Range("e1").CurrentRegion.Rows.Count
Range("a2:a" & lrow1).Copy
Range("e2:e" & lrow2).Select
ActiveSheet.Paste
Selection.NumberFormatLocal = "yyyy/m/d"
''--------------------------------------------
lrow3 = Range("b1").CurrentRegion.Rows.Count
lrow4 = Range("f1").CurrentRegion.Rows.Count
Range("b2:b" & lrow3).Copy
Range("f2:f" & lrow4).Select
ActiveSheet.Paste
Selection.NumberFormatLocal = "0.00%"
''---------------------------------------------
lrow5 = Range("c1").CurrentRegion.Rows.Count
lrow6 = Range("g1").CurrentRegion.Rows.Count
Range("c2:c" & lrow5).Copy
Range("g2:g" & lrow6).Select
ActiveSheet.Paste
Selection.NumberFormatLocal = "G/通用格式"
''---------------------------------------------
lrow7 = Range("d1").CurrentRegion.Rows.Count
lrow8 = Range("h1").CurrentRegion.Rows.Count
Range("d2:d" & lrow7).Copy
Range("h2:h" & lrow8).Select
ActiveSheet.Paste
Selection.NumberFormatLocal = "@"
End Sub
透视表-类似excel透视表生成代码
Sub 透视表()
Worksheets("透视表").Select
Columns("J:L").Clear
Columns("A:H").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"透视表!R1C1:R20C8", Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:="透视表!R4C10", TableName:="数据透视表1", DefaultVersion:= _
xlPivotTableVersion15
Sheets("透视表").Select
Cells(4, 10).Select
With ActiveSheet.PivotTables("数据透视表1").PivotFields("组别")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("数据透视表1").AddDataField ActiveSheet.PivotTables("数据透视表1" _
).PivotFields("可算回款金额"), "求和项:可算回款金额", xlSum
ActiveSheet.PivotTables("数据透视表1").AddDataField ActiveSheet.PivotTables("数据透视表1" _
).PivotFields("是否有回退"), "求和项:是否有回退", xlSum
With ActiveSheet.PivotTables("数据透视表1").PivotFields("求和项:是否有回退")
.Caption = "计数项:是否有回退"
.Function = xlCount
End With
ActiveWorkbook.ShowPivotTableFieldList = False
End Sub