ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
楼主: jsgj2023

[分享] 常用代码归集

  [复制链接]

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-19 13:40 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
Sub 对数组的元素值进行排序()
Dim ws1  As Worksheet
Dim ws2 As Worksheet
Dim myarray As Variant
Set ws1 = Sheets(1)
Set ws2 = Sheets(2)
For i = 1 To 20
    For j = 1 To 2
        ws2.Cells(i, j) = Rnd
    Next j
Next i
myarray = ws2.Range("a1:e20").Value
With ws1
    .Range("a1").Resize(20, 2) = myarray
    .Range("a1:b20").Sort key1:=.Range("a1")
    myarray = .Range("a1:e20").Value
End With
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-19 13:43 | 显示全部楼层
Sub 双条件筛选()
Dim myrange As Range
Set myrange = Range("a1").CurrentRegion
With myrange
    .AutoFilter field:=2, Criteria1:="=李*"
    .AutoFilter field:=3, Criteria1:=">=90"
End With
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-19 13:46 | 显示全部楼层

Sub 判断表格是否有自动筛选()
Dim ws As Worksheet
Dim myautofilter As AutoFilter
Dim myrange As Range
Set ws = ActiveSheet
Set myautofilter = ws.AutoFilter
If Not myautofilter Is Nothing Then
    myautofilter.Range.AutoFilter
Else
    MsgBox "没有自动筛选"
End If
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-19 13:54 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
Sub 在条件区域内设置筛选条件()
Dim myrange1 As Range
Dim myrange2 As Range
Dim mycell As Range
Set myrange1 = Range("a1").CurrentRegion
Set myrange2 = Range("l1:m2")
myrange2.Cells(1, 1) = "总分"
myrange2.Cells(1, 2) = "数学"
myrange2.Cells(2, 1) = ">=360"
myrange2.Cells(2, 2) = ">=90"
MsgBox "条件设置完毕.请仔细察看条件区域."
myrange1.AdvancedFilter Action:=xlFilterInPlace, criteriarange:=myrange2
For Each mycell In myrange2.Cells
    mycell.Clear
Next
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-19 13:57 | 显示全部楼层
Sub 在条件区域呢设置多个筛选条件()
    Dim myRange1 As Range
    Dim myRange2 As Range
    Dim myCell As Range
    Set myRange1 = Range("A1").CurrentRegion
    Set myRange2 = Range("k1:M3")
    myRange2.Cells(1, 1) = "总分"
    myRange2.Cells(1, 2) = "班级"
    myRange2.Cells(1, 3) = "数学"
    myRange2.Cells(2, 1) = ">=360"
    myRange2.Cells(2, 2) = "初一2班"
    myRange2.Cells(3, 2) = "初一2班"
    myRange2.Cells(3, 3) = ">=90"
    MsgBox "条件设置完毕。请仔细察看条件区域。"
    myRange1.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=myRange2
    For Each myCell In myRange2.Cells
        myCell.Clear
    Next
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-19 13:59 | 显示全部楼层
Sub 自动更改指定文字颜色及字体()
     Dim arr, brr, x&, y&, z&, d As Object
     Application.ScreenUpdating = False
     Set d = CreateObject("Scripting.Dictionary")
     With Sheets("Sheet1")
     arr = .Range("b18").CurrentRegion
     brr = .Range("c14").CurrentRegion
     For z = 1 To UBound(brr)
         s1 = brr(z, 1): a = Len(s1)
         For x = 1 To UBound(arr)
           s = arr(x, 2)
            For y = 1 To Len(s)
                 ss = Mid(s, y, a)
                 If ss = s1 Then
                     If Not d.exists(y) Then
                         d(y) = a
                     End If
                 End If
            Next y
            wz = d.keys: cd = d.items
             For i = 0 To UBound(wz)
                 With .Cells(17 + x, 2).Characters(Start:=wz(i), Length:=cd(i)).Font
                     If z = 1 Then
                         .ColorIndex = 5
                     ElseIf z = 2 Then
                         .ColorIndex = 4
                     Else
                         .ColorIndex = 3
                     End If
                         .Bold = True
                 End With
             Next i
         d.RemoveAll
         Next x
     Next z
     End With
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-19 14:00 | 显示全部楼层
自动实现统计三列数据中同一行三个词相同的次数  by liulang0808
Sub 按钮1_Click()   
     Dim brr()
     Application.ScreenUpdating = False
     arr = [b1].CurrentRegion
     Set d1 = CreateObject("scripting.dictionary")
     Set d2 = CreateObject("scripting.dictionary")
     a = 1
     For j = 2 To UBound(arr)
         For i = 1 To 3
             If Not d1.exists(arr(j, i)) Then
                 d1(arr(j, i)) = a
                 a = a + 1
             End If
         Next i
     Next j
     For j = 2 To UBound(arr)
         ReDim brr(1 To d1.Count)
         For i = 1 To 3
             brr(d1(arr(j, i))) = arr(j, i)
         Next i
         d2(Join(brr)) = 1 + d2(Join(brr))
     Next j
     [i2].Resize(d2.Count) = WorksheetFunction.Transpose(d2.keys)
     [j2].Resize(d2.Count) = WorksheetFunction.Transpose(d2.items)
     Application.ScreenUpdating = True
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-19 14:02 | 显示全部楼层
Sub 取消筛选模式显示全部数据()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.ActiveSheet
    With ws
        If .FilterMode Then
            .ShowAllData
        End If
    End With
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-19 14:05 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
Sub 将按条件筛选出的数据复制到新建的表格中()
    Dim myRange As Range
    Dim myRangeD As Range
    Dim myRange1 As Range
    Dim myRange2 As Range
    Dim ws As Worksheet
    Set myRange1 = Range("A1").CurrentRegion
    Set myRange2 = Range("k1:M3")
    Set myRange = Worksheets(1).Range("A1").CurrentRegion
    With Worksheets
        Set ws = .Add(After:=.Item(.Count))
        Set myRangeD = ws.Range("A1")
    End With
    With myRange2
        .Cells(1, 1) = "总分"
        .Cells(1, 2) = "班级"
        .Cells(1, 3) = "数学"
        .Cells(2, 1) = ">=360"
        .Cells(2, 2) = "初一2班"
        .Cells(3, 2) = "初一2班"
        .Cells(3, 3) = ">=90"
    End With
    myRange1.AdvancedFilter Action:=xlFilterCopy, _
        CriteriaRange:=myRange2, CopyToRange:=myRangeD
    myRange2.Clear
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-19 14:07 | 显示全部楼层
Sub RemoveDuplicates法去重复值()
    Dim myRange As Range
    Set myRange = Range("A1:C11")
    myRange.RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlYes
End Sub
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-7-5 02:02 , Processed in 0.033444 second(s), 4 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表