ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[分享] 常用代码归集

  [复制链接]

TA的精华主题

TA的得分主题

发表于 2017-2-22 20:33 | 显示全部楼层
作个WORD文档整理一下,好一些…………

TA的精华主题

TA的得分主题

发表于 2017-2-22 21:44 | 显示全部楼层
建议楼主上传附件,谢谢!

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-23 09:39 | 显示全部楼层
Sub 同兴结算平台型号整理()
    Dim ws  As Worksheet, d As Object, L%, x&, y&, c&, r&
    If MsgBox("请确保活动工作表第一行存在“商品名称”关键字。", vbYesNo + vbQuestion + vbDefaultButton1) = vbYes Then
        GoTo 100
    Else
        Exit Sub
    End If
100:
    On Error GoTo 1000
    L = Application.InputBox("请选择商品名称所在的列", "温馨提示", Type:=8).Column
    On Error GoTo 0
    Application.ScreenUpdating = False
    Set d = CreateObject("scripting.dictionary")
    With GetObject("C:\Users\Administrator\AppData\Roaming\Microsoft\AddIns" & "\单品名称和型号标准数据表.xlsx")
        arr = .Sheets("同兴结算系统型号提取-字典法").[a1].CurrentRegion
        .Close False
    End With
    For x = 2 To UBound(arr)
        If Not d.exists(arr(x, 1)) Then
            d(arr(x, 1)) = Array(arr(x, 2), arr(x, 3))
        End If
    Next x
    Set ws = ActiveSheet
    With ws
        c = .Cells(1, .Columns.Count).End(xlToLeft).Column + 1
        r = .Cells(Rows.Count, L).End(3).Row
        brr = Range(Cells(1, L), Cells(r, L))
        For y = 2 To UBound(brr)
            If d.exists(brr(y, 1)) Then
                sss = d(brr(y, 1))
                Cells(y, c).Resize(1, 2) = d(brr(y, 1))
            End If
        Next y
    End With
1000:
Set d = Nothing
Application.ScreenUpdating = True
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-23 10:28 | 显示全部楼层
Sub 将表格中的图表制作为幻灯片()
    Dim ws As Worksheet
    Dim pptApp As PowerPoint.Application
    Dim pptPrs As PowerPoint.Presentation
    Dim i As Long
    Dim myppt As String
    myppt = ThisWorkbook.Path & "\PowerPoint试验.ppt"
    Set ws = Worksheets(1)    '指定工作表
    ws.ChartObjects(1).Copy    '复制指定的图表
    Set pptApp = New PowerPoint.Application
    pptApp.Visible = True
    Set pptPrs = pptApp.Presentations.Open(myppt)    '打开PowerPoint文档
    With pptPrs    '在文档最后插入一个新幻灯片,并粘贴图表
        .Slides.Add(Index:=.Slides.Count + 1, Layout:=ppLayoutText).Shapes.Paste
        .Save
'        .Close         '设置此语句可关闭创建的文档
    End With
'    pptApp.Quit       '设置此语句可关闭PowerPoint应用程序
    Set ws = Nothing
    Set pptPrs = Nothing
    Set pptApp = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-23 10:31 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
Sub 发送邮件()
    Dim n As Integer, i As Integer
    Dim ws As Worksheet
    Dim OutlookApp As Outlook.Application
    Dim newMail As Outlook.MailItem
    Set OutlookApp = New Outlook.Application
    Set ws = Worksheets("sheet1")
    n = ws.Range("A65536").End(xlUp).Row
    For i = 2 To n
       Set newMail = OutlookApp.CreateItem(olMailItem)    '创建新邮件
       With newMail
            .Subject = "实验"      '设置邮件主题
            .Body = "实验数据:" & ws.Range("B" & i)      '设置邮件正文
            .To = ws.Range("A" & i)       '设置收件人地址
            .Send        '开始发送
        End With
    Next i
    Set ws = Nothing
    Set newMail = Nothing
    Set OutlookApp = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-23 10:34 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Sub 邮件发送_2()
    Dim wbStr As String
    Dim OutlookApp As Outlook.Application
    Dim newMail As Outlook.MailItem
    Dim myAttachments As Outlook.Attachments
    Set OutlookApp = New Outlook.Application
    wbStr = ThisWorkbook.FullName    '指定要发送发工作簿名称字符串
    Set newMail = OutlookApp.CreateItem(olMailItem)    '创建新邮件
    With newMail
        .Subject = "附件实验"      '设置邮件主题
        .Body = "附件工作簿实验:"     '设置邮件正文
        Set myAttachments = newMail.Attachments
        myAttachments.Add wbStr, olByValue, 1, "工作簿"
        .To = "1454870494@qq.com"       '设置收件人地址
        .Send        '开始发送
     End With
    Set newMail = Nothing
    Set myAttachments = Nothing
    Set OutlookApp = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-23 13:28 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Sub 判断shape对象的类型()
    Dim myType As String
    Dim myShape As Shape
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets(1)    '指定工作表
    Set myShape = ws.Shapes(1)       '指定Shape对象
    With myShape
        Select Case .Type
            Case msoShapeTypeMixed
                myType = "混合型图形"
            Case msoAutoShape
                myType = "自选图形"
            Case msoCallout
                myType = "没有边框线的标注"
            Case msoChart
                myType = "图表"
            Case msoComment
                myType = "批注"
            Case msoFreeform
                myType = "任意多边形"
            Case msoGroup
                myType = "图形组合"
            Case msoFormControl
                myType = "窗体控件"
            Case msoLine
                myType = "线条"
            Case msoLinkedOLEObject
                myType = "链接式或内嵌OLE对象"
            Case msoLinkedPicture
                myType = "剪贴画或图片"
            Case msoOLEControlObject
                myType = "ActiveX 控件"
            Case msoPicture
                myType = "图片"
            Case msoTextEffect
                myType = "艺术字"
            Case msoTextBox
                myType = "文本框"
            Case msoDiagram
                myType = "组织结构图或其他图示"
            Case Else
                myType = "其他类型的图形"
        End Select
    End With
    MsgBox "该Shape对象的类型为:" & myType
    Set myShape = Nothing
    Set ws = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-23 14:17 | 显示全部楼层
Sub 判断shape对象的名称()
    Dim myName As String
    Dim myShape As Shape
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets(1)    '指定工作表
    Set myShape = ws.Shapes(2)       '指定Shape对象
    myName = myShape.Name
    MsgBox "该Shape对象的名称为:" & myName
    Set myShape = Nothing
    Set ws = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-23 14:32 | 显示全部楼层
Sub 获取指定shape对象的标题文字内容()
    Dim myCaption As String
    Dim myShape As Shape
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets(1)    '指定工作表
    Set myShape = ws.Shapes(2)       '指定Shape对象
    If myShape.Type = msoTextEffect Then
        myCaption = myShape.TextEffect.Text
    Else
        myCaption = myShape.TextFrame.Characters.Text
    End If
    MsgBox "该Shape对象的标题文字为:" & myCaption
    Set myShape = Nothing
    Set ws = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-23 14:39 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Sub 获取shape对象的高度和宽度()
    Dim myShape As Shape
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets(1)
    Set myShape = ws.Shapes(2)
    MsgBox "该Shape对象的高度:" & myShape.Height & vbCrLf _
        & "该Shape对象的宽度:" & myShape.Width
    Set myShape = Nothing
    Set ws = Nothing
End Sub
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-9-28 23:06 , Processed in 0.036027 second(s), 6 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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