1234

ExcelHome技术论坛

用户名  找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[分享] 常用代码归集

  [复制链接]

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-5 08:40 | 显示全部楼层
Sub 打开windows资源管理器()
Dim mysh  As Shell32.Shell
Set mysh = CreateObject("shell.application")
mysh.Explore ThisWorkbook.Path
Set mysh = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-5 08:47 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
Sub 运行程序()
Dim mysh As Shell32.Shell
Dim myflditem As Shell32.FolderItem
Set mysh = CreateObject("shell.application")
For Each myflditem In mysh.Namespace(ssfCONTROLS).Items
    If myflditem.Name = "显示" Then
        myflditem.InvokeVerbEx
        Exit For
    End If
Next
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-5 08:50 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Sub 创建快捷方式()
Dim mywsh As IWshRuntimeLibrary.WshShell
Dim myshtcut As IWshRuntimeLibrary.WshShortcut
Dim mypath As String
Set mywsh = CreateObject("wscript.shell")
mypath = mywsh.SpecialFolders("desktop")
Set myshtcut = mywsh.CreateShortcut(mypath & "\mytest.lnk")
With myshtcut
    .TargetPath = ThisWorkbook.FullName
    .Save
End With
MsgBox "快捷方式创建成功!" & vbCrLf & "名称为:" & myshtcut.FullName
Set myshtcut = Nothing
Set mywsh = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-5 08:52 | 显示全部楼层
Sub 打开指定的网址()
Dim myir As InternetExplorer
Set myIE = New InternetExplorer
With myIE
    .Visible = True
    .Navigate "http://club.excelhome.net/forum.php"
End With
Set myIE = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-5 08:59 | 显示全部楼层
Private Declare Function playwavesound Lib "winmm.dll" Alias "sndplaysoundA" (ByVal lpszsoundname As String, ByVal uflags As Long) As Long
Sub 播放指定的声音文件()
Dim soundname As String
soundname = "C:\WINDOWS\Media\Windows XP 注销音.wav"
playwavesound soundname, 0
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-6 08:45 | 显示全部楼层
Private Declare Function getsystemmetrics Lib "user32" Alias "GetSystemMetrics" (ByVal nindex As Long) As Long
Const xscreen = 0
Const yscreen = 1
Sub 获取电脑屏幕分辨率()
    Dim xval As Long
    Dim yval As Long
    yval = getsystemmetrics(yscreen)
    xval = getsystemmetrics(xscreen)
    MsgBox "您的电脑屏幕分辨率为:" & xval & "X" & yval
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-6 10:10 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Sub 打开邮件客户端()
Dim myie As Object
Set myie = CreateObject("internetexplorer.application")
With myie
    .navigate "mailto:654573995@.qq.com"
    .Quit
End With
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-6 13:42 | 显示全部楼层
Sub 创建word文档()
Dim res, docapp As Word.Application
Application.StatusBar = "创建新的word文档........."
Set docapp = New Word.Application
With docapp
    .Visible = True
    Application.StatusBar = "创建新的word文档....."
    .Documents.Add
    .ActiveDocument.Paragraphs(1).Range.InsertBefore "excel vba 实用操作技巧"
    .Application.StatusBar = "保存文档....."
    .ActiveDocument.SaveAs2 ThisWorkbook.Path & "\excel vba实用操作技巧.doc"
    Application.StatusBar = "退出新建的word文档..."
    res = MsgBox("已经创建了word文档,是否要关闭word文档?", vbYesNo)
    If res = vbYes Then
        .Quit
    End If
End With
Set docapp = Nothing
Application.StatusBar = False
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-6 13:51 | 显示全部楼层
Sub 统计文件夹中的文件数量()
    Dim myfs As FileSearch
    Dim mypath As String
    Dim i As Long
    Dim n As Long
    Set myfs = Application.FileSearch
    mypath = ThisWorkbook.Path
    With myfs
        .NewSearch
        .LookIn = mypath
        .FileType = msoFileTypeAllFiles
        .Filename = "*.*"
        .SearchSubFolders = True
        If .Execute(SortBy:=msoSortByFileName) > 0 Then
            n = .FoundFiles.Count
            MsgBox "该文件夹里有" & n & "个文件"
            ReDim myfile(1 To n) As String
            For i = 1 To n
                myfile(i) = .FoundFiles(i)
                Cells(i, 1) = myfile(i)
            Next
        Else
            MsgBox "该文件夹里没有任何文件!"
        End If
    End With
    Set myfs = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-6 13:56 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
Sub 统计当前工作簿所在的文件夹中工作簿的数量并列出工作簿的名称()
Dim mypath As String
Dim myfilename As String
Dim i As Long
mypath = ThisWorkbook.Path & "\"
myfilename = Dir(mypath, 0)
i = 0
Do While Len(myfilename) > 0
    Cells(i + 1, 1) = mypath & myfilename
    myfilename = Dir()
    i = i + 1
Loop
MsgBox "该文件夹里有" & i & "个文件!"
End Sub
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

1234

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

GMT+8, 2025-4-24 01:02 , Processed in 0.021826 second(s), 6 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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