ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[分享] 常用代码归集

  [复制链接]

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-7 13:50 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Function 个人所得税(curP As Currency)
    Dim curT As Currency
    curP = curP - 1600 '1600为扣除数
    If curP > 0 Then
        Select Case curP
            Case Is <= 500
                curT = curP * 0.05
            Case Is <= 2000
                curT = (curP - 500) * 0.1 + 25
            Case Is <= 5000
                curT = (curP - 2000) * 0.15 + 125
            Case Is <= 20000
                curT = (curP - 5000) * 0.2 + 375
            Case Is <= 40000
                curT = (curP - 20000) * 0.25 + 1375
            Case Is < 60000
                curT = (curP - 40000) * 0.3 + 3375
            Case Is < 80000
                curT = (curP - 60000) * 0.35 + 6375
            Case Is < 100000
                curT = (curP - 80000) * 0.4 + 10375
            Case Else
                curT = (curP - 100000) * 0.45 + 15375
        End Select
        个人所得税 = curT
    Else
        个人所得税 = 0
    End If
End Function

Sub 计算()
    For i = 4 To 9
        Sheets(1).Cells(i, 8).Value = 个人所得税(Sheets(1).Cells(i, 6).Value)
    Next
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-7 13:52 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
Sub 输出ascii码表()
    Dim a As Integer, i As Integer
    i = 3
    For a = 32 To 126
        Sheets(1).Cells(i, 1) = a
        Sheets(1).Cells(i, 2) = Chr(a)
        i = i + 1
    Next
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-7 13:54 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Sub 统计10元钱换为零钱的方法()
    Dim t As Integer
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim l As Integer
    Dim m As Integer
    Dim n As Integer
    For i = 0 To 100    '1角
        For j = 0 To 50 '2角
            For k = 0 To 20 '5角
                For l = 0 To 10 '1元
                    For m = 0 To 5  '2元
                        For n = 0 To 2  '5元
                            If i + 2 * j + 5 * k + 10 * l + 20 * m + 50 * n = 100 Then
                                t = t + 1
                                Sheets(1).Cells(t + 1, 1) = i
                                Sheets(1).Cells(t + 1, 2) = j
                                Sheets(1).Cells(t + 1, 3) = k
                                Sheets(1).Cells(t + 1, 4) = l
                                Sheets(1).Cells(t + 1, 5) = m
                                Sheets(1).Cells(t + 1, 6) = n
                            End If
                        Next
                    Next
                Next
            Next
        Next
    Next
    MsgBox "10元换为零钱共有" & t & "种方法!"
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-7 14:02 | 显示全部楼层
Option Base 1
Sub 用数组填充单元格区域()
Dim i As Long
Dim j As Long
Dim col As Long
Dim row As Long
Dim arr() As Long
row = Application.InputBox(prompt:="输入行数:", Type:=2)
col = Application.InputBox(prompt:="输入列数:", Type:=2)
ReDim arr(row, col)
For i = 1 To row
    For j = 1 To col
        arr(i, j) = (i - 1) * col + j
    Next j
Next i
Set Rng = Sheets(1).Range(Cells(1, 1), Cells(row, col))
Rng.Value = arr
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-7 14:06 | 显示全部楼层
Sub 计算选中区域数值的和()
    Dim r As Variant
    Dim t As Long
    For Each r In Selection
        If IsNumeric(r.Value) Then
            t = t + r.Value
        End If
    Next
    MsgBox "所选区域数值之和为:" & t
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-7 14:12 | 显示全部楼层
Sub 通过密码验证后进入系统()
    Dim strpassword As String
    Dim i As Integer
    Do
        strpassword = InputBox("请输入密码")
        If strpassword = "Adele" Then
            Exit Do
        Else
            MsgBox ("请输入正确的密码!")
        End If
        i = i + 1
    Loop While i < 3
    If i >= 3 Then
        MsgBox "非法用户,系统将退出!"
        Application.Quit
    Else
        MsgBox "欢迎使用本系统!"
    End If
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-7 15:14 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Sub 指定查询条件查找()
    Dim myrange As Range
    Set myrange = Cells.Find(what:="刘方圆", after:=ActiveCell, LookIn:=xlValues, _
    lookat:=xlPart, searchorder:=xlByRows, searchdirection:=xlNext, MatchCase:=False)
    If myrange Is Nothing Then
        MsgBox "没有找到符合条件的单元格!"
    Else
        MsgBox "符合条件的单元格为:" & myrange.Address(False, False)
        myrange.Activate
    End If
    Set myrange = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-7 15:27 | 显示全部楼层
Sub 指定查询范围和查询条件查找()
Dim myrange1 As Range
Dim myrange2 As Range
Dim myrow As Long
Set myrange1 = Columns("B")
On Error Resume Next
myrow = worksheetfuntion.Match("刘方圆", myrange1, 0)
On Error GoTo 0
If myrow = 0 Then
    MsgBox "没有找到符合条件的单元格!"
Else
    Set myrange2 = myrange1.Cells(myrow)
    MsgBox "符合条件的单元格为:" & myrange2.Address(False, False)
    myrange2.EntireRow.Select
End If
    Set myrange1 = Nothing
    Set myrange2 = Nothing
End Sub

TA的精华主题

TA的得分主题

发表于 2017-2-7 15:29 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-8 09:51 | 显示全部楼层
Sub vlookup指定范围和条件查询()
    Dim myrange As Range
    Dim myscore As Single
    Dim mykey As String
    Dim myerrnum As Long
    Set myrange = Columns("b:d")
    mykey = "刘方圆"
    On Error Resume Next
    myscore = WorksheetFunction.VLookup(mykey, myrange, 3, False)
    myerrnum = Err.Number
    On Error GoTo 0
    If myerrnum = 0 Then
        MsgBox mykey & "的语文成绩为:" & myscore
    Else
        MsgBox "没有找到符合条件的单元格!"
    End If
    Set myrange = Nothing
End Sub
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-10-6 06:31 , Processed in 0.043279 second(s), 4 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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