1234

ExcelHome技术论坛

用户名  找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[分享] 常用代码归集

  [复制链接]

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-3 13:15 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
Sub 新建文件夹_()
Dim sfolder  As String
sfolder = Application.InputBox(prompt:="请输入新建文件夹的名称:", _
Title:="输入文件夹名称", Type:=2)
If sfolder = "False" Or sfolder = "" Then Exit Sub
If Len(Dir(sfolder, vbDirectory)) > 0 Then
    MsgBox "文件夹" & sfolder & "已存在!"
Else
    MkDir sfolder
    MsgBox "文件夹" & sfolder & "创建成功!"
End If
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-3 13:22 | 显示全部楼层
Sub 复制文件_()
Dim sourcefile As String
Dim destinationfile As String
On Error GoTo err1
sourcefile = Application.InputBox(prompt:="请输入当前文件夹中源文件的名称:", _
Title:="源文件", Type:=2)
destinationfile = Application.InputBox(prompt:="请输入目标文件的名称", _
Title:="目标文件", Type:=2)
sourcefile = CurDir & "\" & sourctfile
If Len(Dir(sourcefile, vbDirectory)) > 0 Then
    FileCopy sourctfile, destinationfile
    MsgBox "复制成功!"
Else
    MsgBox "源文件" & sourcefile & "不存在!"
End If
err1:
If Err.Number <> 0 Then
    MsgBox "无法复制该文件!"
End If
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-3 13:30 | 显示全部楼层
Sub 重命名文件或者文件夹()
Dim sourctfile As String
Dim destinationfile As String
Dim spath  As String
SourceFile = Application.InputBox(prompt:="请输入需要重命名文件或文件夹的名称:", _
Title:="源文件", Type:=2)
Destination = Application.InputBox(prompt:="请输入文件或文件夹的新名称:", _
Title:="目标文件", Type:=2)
spath = ThisWorkbook.Path & "\"
SourceFile = spath & sourctfile
Destination = spath & destinationfile
If Len(Dir(SourceFile, vbDirectory)) > 0 Then
    Name SourceFile As destinationfile
    MsgBox "重命名成功!"
Else
    MsgBox "文件或文件夹" & SourceFile & "不存在!"
End If
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-3 13:36 | 显示全部楼层
Sub 删除文件_()
Dim sfilename As String
sfilename = Application.InputBox(prompt:="请输入需要删除的文件名:", _
Title:="输入文件名", Type:=2)
sfilename = ThisWorkbook.Path & "\" & sfilename
On Error Resume Next
Kill sfilename
On Error GoTo 0
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-3 13:40 | 显示全部楼层
Sub 删除文件夹_()
Dim sfolder   As String
sfolder = Application.InputBox(prompt:="请输入需要删除的文件夹名称:", _
Title:="输入文件夹", Type:=2)
sfolder = ThisWorkbook.Path & "\" & sfolder
If Len(Dir(sfolder & "\")) > 0 Then
    MsgBox "文件夹" & sfolder & "中有文件,不能删除该文件夹!"
Else
    RmDir sfolder
    MsgBox "文件夹" & sfolder & "已经删除!"
End If
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-4 09:22 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Sub 判断文件是否存在()
Dim fso  As New Scripting.FileSystemObject
Dim sfilename   As String
sfilename = Application.InputBox(prompt:="请输入文件名称", _
Default:=ThisWorkbook.FullName, Title:="输入文件名称", Type:=2)
If sfilename = "False" Then Exit Sub
If fso.FileExists(sfilename) Then
    MsgBox "文件" & sfilename & "存在!"
Else
    MsgBox "文件" & sfilename & "不存在!"
End If
Set fso = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-4 09:26 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
Sub 判断文件夹是否存在()
Dim fso As New Scripting.FileSystemObject
Dim sfolder As String
sfolder = Application.InputBox(prompt:="请输入文件夹名称", _
Default:=ThisWorkbook.Path, Title:="输入文件夹名称", Type:=2)
If sfolder = "False" Then Exit Sub
If fso.FolderExists(sfolder) = True Then
    MsgBox "文件夹" & sfolder & "存在!"
Else
    MsgBox "文件夹" & sfolder & "不存在!"
End If
Set fso = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-4 09:35 | 显示全部楼层
Sub 分离文件名和扩展名_()
Dim fso As New Scripting.FileSystemObject
Dim sfilename As String
Dim str1 As String
sfilename = ThisWorkbook.FullName
str1 = "本工作簿文件的全名:" & fso.GetFileName(sfilename)
str1 = str1 & vbCrLf & vbCrLf
str1 = str1 & "基础名:" & fso.GetBaseName(sfilename)
str1 = str1 & vbCrLf & vbCrLf
str1 = srt1 & "扩展名:" & fso.GetExtensionName(sfilename)
MsgBox str1, vbInformation + vbOKOnly, "提示"
Set fso = Nothing
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-4 11:41 | 显示全部楼层
Sub 查看文件属性()
    Dim sfilename As String
    Dim stype As String
    Dim itype As Integer
    sfilename = Application.InputBox(prompt:="请输入文件名", _
    Title:="输入文件名", Default:=ThisWorkbook.FullName, Type:=2)
    If sfilename = "False" Then Exit Sub
    With ActiveSheet
        .Range(Cells(4, 1), Cells(7, 2)).Clear
        .Cells(4, 1) = sfilename
        .Cells(4, 1).Font.Bold = True
        .Cells(5, 1) = "文件大小:"
        .Cells(5, 2) = FileLen(sfilename) & "byte"
        .Cells(6, 1) = "文件类型:"
        itype = GetAttr(sfilename)
        If itype And vbNormal = 0 Then
            stype = "常规文件"
        ElseIf itype And vnreadonly Then
            stype = "只读文件"
        ElseIf itype And vbHidden Then
            stype = "隐藏文件"
        ElseIf itype And vbSystem Then
            stype = "系统文件"
        ElseIf itype And vbArchive Then
            stype = "备份文件"
        ElseIf itype And vbDirectory Then
            stype = "文件夹"
        End If
        .Cells(6, 2) = stype
        .Cells(7, 1) = "最后修改时间:"
        .Cells(7, 2) = FileDateTime(sfilename)
    End With
End Sub

TA的精华主题

TA的得分主题

发表于 2017-2-4 11:43 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

1234

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

GMT+8, 2025-4-2 23:43 , Processed in 0.021123 second(s), 4 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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