ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[分享] 常用代码归集

  [复制链接]

TA的精华主题

TA的得分主题

发表于 2017-2-8 13:48 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
!!!!!如果能写个扩展库,如jquery对js一样,那将是一件很厉害的事!!!!!!

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-8 14:06 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
Sub 添加辅助列排序()
Dim ws As Worksheet
Dim myrange As Range
Dim mycolumn As Long
Dim myrow As Long
Set ws = Worksheets(1)
With ws
    .Select
    With .Range("a1").CurrentRegion
        mycolumn = .Columns.Count
        myrow = .Rows.Count
    End With
    .Columns(mycolumn + 1).Insert
    With .Cells(1, mycolumn + 1)
        .Value = 1
        .AutoFill Destination:=.Resize(myrow), Type:=xlFillSeries
    End With
    Set myrange = .Range("a1").CurrentRegion
    With myrange
        MsgBox "按姓名排序"
        .Sort key1:="姓名", order1:=xlAscending, Header:=xlYes
        MsgBox "恢复原状"
        .Sort key1:=.Cells(2, .Columns.Count), order1:=xlAscending, Header:=xlYes
    End With
    .Columns(mycolumn + 1).Delete
End With
Set myrange = Nothing
Set ws = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-8 14:24 | 显示全部楼层
Sub 按单元格颜色排序()
Dim ws As Worksheet
Dim myrnage As Range
Dim myrow As Long
Dim mycolumn As Long
Dim i As Long
Set ws = Worksheets(1)
With ws
    With .Range("a1").CurrentRegion
        mycolumn = .Columns.Count
        myrow = .Rows.Count
    End With
    .Columns(mycolumn + 1).Insert
    For i = 2 To myrow
        .Cells(i, mycolumn + 1).Value = .Cells(i, 1).Interior.ColorIndex
    Next
    Set myRange = .Range("a1").CurrentRegion
    With myRange
        .Sort key1:=.Cells(1, mycolumn + 1), order1:=xlAscending, Header:=xlYes
    End With
    .Columns(mycolumn + 1).Delete
End With
Set myRange = Nothing
Set ws = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-8 14:40 | 显示全部楼层
Sub 按字符长度排序()
Dim ws As Worksheet
Dim myrange As Range
Dim myrow As Long
Dim mycolumn As Long
Dim i As Long
Set ws = Worksheets(1)
With ws
    With .Range("a1").CurrentRegion
        mycolumn = .Columns.Count
        myrow = .Rows.Count
    End With
    .Columns(mycolumn + 1).Insert
    For i = 2 To myrow
        .Cells(i, mycolumn + 1).Value = Len(.Cells(i, 1).Value)
    Next
    Set myrange = .Range("a1").CurrentRegion
    With myrange
        .Sort key1:=.Cells(1, mycolumn + 1), order1:=xlAscending, Header:=xlYes
    End With
    .Columns(mycolumn + 1).Delete
End With
Set myrange = Nothing
Set ws = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-8 16:05 | 显示全部楼层
Sub 按单元格字符串中的数字升序排列()
Dim ws As Worksheet
Dim myrange As Range
Dim myrow As Long
Dim mycolumn As Long
Dim i  As Long
Set ws = Worksheets(1)
With ws
    With .Range("a1").CurrentRegion
        mycolumn = .Columns.Count
        myrow = .Rows.Count
    End With
    .Columns(mycolumn + 1).Insert
    For i = 2 To myrow
        .Cells(i, mycolumn + 1).Value = findnumber(.Cells(i, 1).Text)
    Next i
    Set myrange = .Range("a1").CurrentRegion
    With myrange
        .Sort key1:=.Cells(1, mycolumn + 1), order1:=xlAscending, key2:=.Cells(1, 1), order2:=xlAscending, Header:=xlYes
    End With
    .Columns(mycolumn + 1).Delete
End With
Set myrange = Nothing
Set ws = Nothing
End Sub

Function findnumber(mystring As String)
Dim i As Integer
Dim j  As Integer
Dim mynum As String
For i = 1 To Len(mystring)
    If IsNumeric(Mid(mystring, i, 1)) Then
        j = j + 1
        mynum = mynum & Mid(mystring, i, 1)
    End If
    If j = 1 Then mynum = CInt(Mid(mystring, i, 1))
Next i
findnumber = CLng(mynum)
End Function

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-8 16:18 | 显示全部楼层
Sub 取消指定单元格的合并区域()
With Range("a3")
    If .MergeCells = True Then
        .MergeArea.UnMerge
    Else
        MsgBox "没有合并的区域!"
    End If
End With
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-8 17:05 | 显示全部楼层

Sub 复制和移动单元格区域并对值累加()
    With Sheets("Sheet1")
        .Range("c1:c10").Copy
        .Range("d1:d10").PasteSpecial operation:=xlPasteSpecialOperationAdd
    End With
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-8 17:24 | 显示全部楼层
Sub 有效遍历选中单元格区域()
    Dim cell As Range
    If TypeName(Selection) <> "Range" Then Exit Sub
    Const redindex = 3
    Application.ScreenUpdating = False
    For Each cell In Selection
        If cell.Value < 45 Then
            cell.Interior.ColorIndex = 4
        Else
            cell.Interior.ColorIndex = 0
        End If
    Next
    Application.ScreenUpdating = True
End Sub

TA的精华主题

TA的得分主题

发表于 2017-2-8 22:05 来自手机 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-9 22:07 | 显示全部楼层
'遍历某文件夹及子文件夹中的所有文件
Sub sosuofile(mypath As String)
Dim myname   As String
Dim a As String
Dim b() As String
Dim dir_i() As String
Dim i As Long
Dim idir As Long
If Right(mypath, 1) <> "\" Then mypath = mypath + "\"
myname = Dir(mypath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)
Do While myname <> ""
    If myname <> "." And myname <> ".." Then
        If (GetAttr(mypath & myname) And vbDirectory) = vbDirectory Then
            idir = idir + 1
            ReDim Preserve dir_i(idir) As String
            dir_i(idir - 1) = myname
        Else
            list1.AddItem mypath & myname
        End If
    End If
    myname = Dir
Loop
For i = 0 To idir - 1
    Call sosuofile(mypath + dir_i(i))
Next i
ReDim dir_i(0) As String
End Sub
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-10-6 06:30 , Processed in 0.041779 second(s), 5 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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