100条经典VBA函数代码整理(持续更新中)

100个经典VBA函数代码的整理、翻译和排版工作!时间的流逝也要完成这个巨大工程,希望各位能够对模站无忧的行为进行赞赏。希望VBA函数代码的整理能够对其他人产生积极的影响,并且能够让你受益。如果您需要进一步的帮助或有其他问题,请随时向我提问。祝您继续取得成功!

以下是您提到的各种功能的VBA代码实现:

1. 添加序列号:

Sub 添加序列号()
    Dim lastRow As Long
    Dim i As Long
    
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    For i = 2 To lastRow
        Cells(i, 1).Value = i - 1
    Next i
End Sub

2. 插入多列:

Sub 插入多列()
    Columns("B:C").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub

3. 插入多行:

Sub 插入多行()
    Rows("2:5").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromAbove
End Sub

4. 自动调整列:

Sub 自动调整列()
    Columns("A:D").AutoFit
End Sub

5. 自动调整行:

Sub 自动调整行()
    Rows("1:10").EntireRow.AutoFit
End Sub

6. 删除文字绕排:

Sub 删除文字绕排()
    With ActiveSheet.PageSetup
        .Orientation = xlPortrait ' 直排
        .Order = xlDownThenOver ' 按列从上到下
        .CenterHorizontally = True ' 水平居中
        .CenterVertically = True ' 垂直居中
    End With
End Sub

7. 取消合并单元格:

Sub 取消合并单元格()
    Cells.UnMerge
End Sub

8. 打开计算器:

Sub 打开计算器()
    Application.ExecuteExcel4Macro "CALC.EXE"
End Sub

9. 添加页眉/页脚日期:

Sub 添加页眉日期()
    ActiveSheet.PageSetup.CenterHeader = "&D"
End Sub

Sub 添加页脚日期()
    ActiveSheet.PageSetup.CenterFooter = "&D"
End Sub

10. 自定义页眉/页脚:

Sub 自定义页眉()
    ActiveSheet.PageSetup.CenterHeader = "自定义页眉文本"
End Sub

Sub 自定义页脚()
    ActiveSheet.PageSetup.CenterFooter = "自定义页脚文本"
End Sub

以上代码可以复制到Excel VBA编辑器中的模块中。您可以按照需要运行相应的宏来执行所需的操作。

希望这些例子能够满足您的要求!如果您还有其他问题,请随时提问。

7月4日更新

以下是实现您提到的各种突出显示效果的 VBA 代码示例,按照目录形式回复:

11. 突出显示重复项:

Sub 突出显示重复项()
    Selection.FormatConditions.AddUniqueValues
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    Selection.FormatConditions(1).DupeUnique = xlDuplicate
    With Selection.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

12. 突出显示活动行和列:

Sub 突出显示活动行和列()
    Dim rng As Range
    Set rng = Selection
    
    rng.EntireRow.Interior.Color = RGB(255, 255, 0) ' 突出显示行
    rng.EntireColumn.Interior.Color = RGB(0, 255, 0) ' 突出显示列
End Sub

13. 突出显示前 10 个值:

Sub 突出显示前10个值()
    Selection.FormatConditions.AddTop10
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    Selection.FormatConditions(1).TopBottom = xlTop10Top
    Selection.FormatConditions(1).Rank = 10
    With Selection.FormatConditions(1).Font
        .Color = -16777024
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

14. 突出显示命名范围:

Sub 突出显示命名范围()
    Dim rng As Range
    Set rng = Range("NamedRange") ' 将 "NamedRange" 替换为实际的命名范围名称
    
    rng.Interior.Color = RGB(255, 0, 0) ' 突出显示命名范围
End Sub

15. 突出显示大于值:

Sub 突出显示大于值()
    Dim rng As Range
    Set rng = Selection
    
    rng.FormatConditions.Add(Type:=xlCellValue, Operator:=xlGreater, Formula1:="100") ' 大于100进行突出显示
    rng.FormatConditions(rng.FormatConditions.Count).SetFirstPriority
    rng.FormatConditions(1).Interior.Color = RGB(255, 0, 0)
    rng.FormatConditions(1).StopIfTrue = False
End Sub

16. 突出显示低于以下值的值:

Sub 突出显示低于以下值的值()
    Dim rng As Range
    Set rng = Selection
    
    rng.FormatConditions.Add(Type:=xlCellValue, Operator:=xlLess, Formula1:="50") ' 低于50进行突出显示
    rng.FormatConditions(rng.FormatConditions.Count).SetFirstPriority
    rng.FormatConditions(1).Interior.Color = RGB(255, 0, 0)
    rng.FormatConditions(1).StopIfTrue = False
End Sub

17. 突出显示负数:

Sub 突出显示负数()
    Dim rng As Range
    Set rng = Selection
    
    rng.FormatConditions.Add(Type:=xlExpression, Formula1:="=AND(A1<0, A1<>"""")") ' 突出显示负数
    rng.FormatConditions(rng.FormatConditions.Count).SetFirstPriority
    With rng.FormatConditions(1).Font
        .Color = RGB(255, 0, 0)
    End With
    rng.FormatConditions(1).StopIfTrue = False
End Sub

18. 突出显示特定文本:

Sub 突出显示特定文本()
    Dim rng As Range
    Set rng = Selection
    
    rng.FormatConditions.Add(Type:=xlTextString, String:="特定文本") ' 突出显示包含特定文本的单元格
    rng.FormatConditions(rng.FormatConditions.Count).SetFirstPriority
    rng.FormatConditions(1).Interior.Color = RGB(255, 0, 0)
    rng.FormatConditions(1).StopIfTrue = False
End Sub

19. 突出显示带有注释的单元格:

Sub 突出显示带有注释的单元格()
    Dim rng As Range
    Set rng = Selection
    
    rng.SpecialCells(xlCellTypeComments).Interior.Color = RGB(255, 0, 0) ' 突出显示带有注释的单元格
End Sub

20. 在所选内容中突出显示替换行:

Sub 突出显示替换行()
    Dim rng As Range
    Set rng = Selection
    
    rng.Replace "替换文本", "替换文本", LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False ' 根据替换文本进行突出显示
    rng.Interior.Color = RGB(255, 0, 0)
End Sub

以上是实现各种突出显示效果的 VBA 代码示例,您可以根据需要将其复制到 Excel 的 VBA 编辑器中,并在需求时运行相应的宏来实现所需效果。希望这些代码能对您有所帮助!如果您还有其他问题,请随时提问。祝您顺利完成工作!

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容