ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[分享] 常用代码归集

  [复制链接]

TA的精华主题

TA的得分主题

发表于 2017-2-12 15:10 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
赞一个,值得拥有

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-12 15:16 | 显示全部楼层
Sub 从word文档中提取数据()
On Error Resume Next
Dim wdapp As Word.Application
Dim wddoc As Document
Set wdapp = New Word.Application
wdapp.Documents.Open Filename:=ThisWorkbook.Path & "\DEMO\9-4.demo.docx"
With wdapp.Documents(1).Tables(1).Range
    For i = 1 To .Cells.Count
        u = Left(.Cells(i).Range, Len(.Cells(i).Range) - 1)
        If IsNumeric(u) Then
             n = n + 1
             Cells(n + 1, 2) = u
             Cells(n + 1, 1) = Left(.Cells(i - 1).Range, Len(.Cells(i - 1).Range) - 1)
         End If
    Next
End With
    Cells(1, 1) = "姓名"
    Cells(1, 2) = "分数"
wdapp.Quit
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-12 15:18 | 显示全部楼层
Sub 从word文字中提取数据()
Dim wdapp As Word.Application
Set wdapp = New Word.Application
wdapp.Documents.Open ThisWorkbook.Path & "\DEMO\9-5.demo.docx"
With wdapp
c = .Documents(1).Range
    Set regx = CreateObject("vbscript.regexp")
        With regx
            .Global = True
            .Pattern = "\d+、\S+。"
            Set Mat = .Execute(c)
            For Each m In Mat
                n = n + 1
                Cells(n, 1) = m.Value
            Next
        End With
End With
wdapp.Quit
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-12 15:27 | 显示全部楼层
Sub 提取excel数据到word()
For i = 1 To 4
    j = Application.CountIf([a:a], i & "车间")
    Dim wdapp As Word.Application
    Set wdapp = New Word.Application
    With wdapp
        .Documents.Add
        .Visible = True
        .Documents(1).Tables.Add .Selection.Range, j, 5
        .Documents(1).Tables(1).Style = "网格型"
        For Each Rng In Range("a2:a18")
            If Rng.Value = i & "车间" Then
                arr = Rng.EntireRow.Range("a1:e1")
                For Each ar In arr
                    n = n + 1
                    .Documents(1).Tables(1).Range.Cells(n).Range = ar
                Next
            End If
        Next
        n = 0
        .Documents(1).SaveAs ThisWorkbook.Path & "\Adele\9-6\" & i & "车间.docx"
    End With
Next i
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-12 16:03 | 显示全部楼层
'自定义函数_提取不重复值
Function 不重复值(rng As Range)
Set d = CreateObject("scripting.dictionary")
For Each rn In rng
    d(rn.Value) = ""
Next
不重复值 = d.keys
End Function
Function 不重复2(rng As Range, Optional num As Integer = 0)
Set d = CreateObject("scripting.dictionary")
Set regx = CreateObject("vbscript.regexp")
With regx
        .Global = True
    If num = 0 Then
        .Pattern = ".+" '所有值的不重复
    ElseIf num = 1 Then
        .Pattern = "[一-龢]+" '汉字不重复
    ElseIf num = 2 Then
        .Pattern = "[a-zA-Z]+" '字母不重复
    ElseIf num = 3 Then
        .Pattern = "\d+" '数字不重复
    End If
For Each rn In rng
    For Each m In .Execute(rn)
        d(m.Value) = ""
    Next
Next
不重复2 = d.keys
End With
End Function

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-12 16:05 | 显示全部楼层
'自定义函数_反转字符
Function DD(rng As Range)
For i = Len(rng) To 1 Step -1
    a = Mid(rng, i, 1)
    b = b & a
Next
    DD = b
End Function

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-12 16:06 | 显示全部楼层
'自定义函数_求和
Function 求和(rng As Range, Optional s As String = "")
Application.Volatile
Set regx = CreateObject("vbscript.regexp")
With regx
    .Global = True
    .Pattern = "\d" & s
   Set mat = .Execute(rng)
End With
For Each m In mat
n = n + m * 1
Next
求和 = n
End Function

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-12 16:07 | 显示全部楼层

'自定义函数_按单元格颜色求和
Function COLORSUM(单元格区域 As range, 汇总的颜色 As range)
Set d = CreateObject("Scripting.Dictionary")
For Each Rng In 汇总的颜色
    d(Rng.Interior.ColorIndex) = ""
Next
For Each ci In d.keys
    For Each Rng In 单元格区域
        If Rng.Interior.ColorIndex = ci Then
            r = r + Rng.Value
        End If
    Next
    Next
COLORSUM = r
End Function
Sub test()
Set d = CreateObject("Scripting.Dictionary")
Set 区域 = Application.InputBox("区域选择", , , , , , , 8)
Set 颜色 = Application.InputBox("颜色选择", , , , , , , 8)
For Each Rng In 颜色
    d(Rng.Interior.ColorIndex) = ""
Next
For Each ci In d.keys
    For Each Rng In 区域
        If Rng.Interior.ColorIndex = ci Then
            r = r + Rng.Value
        End If
    Next
    Next
MsgBox r

End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-12 16:13 | 显示全部楼层
'自定义函数_身份证信息提取
Function idCard(rng As Range, Optional Key As String = "年龄")
    If Key = "年龄" Then
        idCard = Year(Now()) - (19 & Mid(rng, Len(rng) / 2, 2))
    ElseIf Key = "性别" Then
        idCard = IIf(Mid(rng, 15, 3) Mod 2, "男", "女")
    End If
End Function

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-13 09:56 | 显示全部楼层
Dim wdapp As Word.Application
Sub 如何往word表中写入数据()
Dim wdapp As Word.Application
Dim wddoc As Document
Set wdapp = New Word.Application
wdapp.Documents.Open Filename:=ThisWorkbook.Path & "\Adele\9-3.demo.docx"
wdapp.Visible = True
With wdapp.Documents(1).Tables(1).Range
    For i = 1 To .Cells.Count
        .Cells(i).Range = i
    Next
End With
With wdapp.Documents(1).Tables(2).Range
    For i = 1 To .Cells.Count
        .Cells(i).Range = "与Adele第" & i & "次见面!"
    Next i
End With
End Sub
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-7-7 20:30 , Processed in 0.033462 second(s), 4 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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