设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 4649|回复: 4
打印 上一主题 下一主题

[分享] 在编程中,一些实用的自定义函数

[复制链接]
跳转到指定楼层
1#
发表于 2005-6-27 18:24:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在编程时,时常需要知道工作表是否存在,文件是否存在等,这时候,以下这些自定义函数就能派上用场了:

Private Function FileExists(fname) As Boolean

'当文件存在时返回true

    Dim x As String

    x = Dir(fname)

    If x <> "" Then FileExists = True _

        Else FileExists = False

End Function



Private Function FileNameOnly(pname) As String

'返回路径pname的文件名

    Dim i As Integer, length As Integer, temp As String

    length = Len(pname)

    temp = ""

    For i = length To 1 Step -1

        If Mid(pname, i, 1) = Application.PathSeparator Then

            FileNameOnly = temp

            Exit Function

        End If

        temp = Mid(pname, i, 1) & temp

    Next i

    FileNameOnly = pname

End Function



Private Function PathExists(pname) As Boolean

'如果路径pname存在则返回true

    Dim x As String

    On Error Resume Next

    x = GetAttr(pname) And 0

    If Err = 0 Then PathExists = True _

      Else PathExists = False

End Function



Private Function RangeNameExists(nname) As Boolean

'如果一个名称存在则返回true

    Dim n As Name

    RangeNameExists = False

    For Each n In ActiveWorkbook.Names

        If UCase(n.Name) = UCase(nname) Then

            RangeNameExists = True

            Exit Function

        End If

    Next n

End Function



Private Function SheetExists(sname) As Boolean

'如果活动工作簿中存在表SNAME则返回真

    Dim x As Object

    On Error Resume Next

    Set x = ActiveWorkbook.Sheets(sname)

    If Err = 0 Then SheetExists = True _

        Else SheetExists = False

End Function



Private Function WorkbookIsOpen(wbname) As Boolean

'如果工作簿WBNAME打开着,则返回true

    Dim x As Workbook

    On Error Resume Next

    Set x = Workbooks(wbname)

    If Err = 0 Then WorkbookIsOpen = True _

        Else WorkbookIsOpen = False

End Function

本帖被以下淘专辑推荐:

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖1 订阅订阅

点击这里给我发消息

2#
发表于 2005-6-28 04:11:00 | 只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
3#
发表于 2005-7-3 22:47:00 | 只看该作者
[em17]
4#
发表于 2009-6-24 12:16:16 | 只看该作者
不错, 好文
5#
发表于 2009-6-25 12:13:38 | 只看该作者
整理了一下,不错,收藏备用.

Private Function FileExists(fname) As Boolean '当文件存在时返回true
Dim x As String
x = Dir(fname)
If x <> "" Then
    FileExists = True
    Else
    FileExists = False
End if
End Function

Private Function FileNameOnly(pname) As String  '返回路径pname的文件名
Dim i As Integer, length As Integer, temp As String
length = Len(pname)
temp = ""
For i = length To 1 Step -1
    If Mid(pname, i, 1) = Application.PathSeparator Then
        FileNameOnly = temp
        Exit Function
     End If
   temp = Mid(pname, i, 1) & temp
  Next i
FileNameOnly = pname
End Function

Private Function PathExists(pname) As Boolean '如果路径pname存在则返回true
Dim x As String
On Error Resume Next
x = GetAttr(pname) And 0
If Err = 0 Then PathExists = True Else PathExists = False
End Function

Private Function RangeNameExists(nname) As Boolean '如果一个名称存在则返回true
Dim n As Name
RangeNameExists = False
For Each n In ActiveWorkbook.Names
  If UCase(n.Name) = UCase(nname) Then
     RangeNameExists = True
      Exit Function
   End If
Next n
End Function

Private Function SheetExists(sname) As Boolean '如果活动工作簿中存在表SNAME则返回true
Dim x As Object
On Error Resume Next
Set x = ActiveWorkbook.Sheets(sname)
If Err = 0 Then
   SheetExists = True
   Else
   SheetExists = False
End if
End Function

Private Function WorkbookIsOpen(wbname) As Boolean '如果工作簿WBNAME打开着,则返回true
Dim x As Workbook
On Error Resume Next
Set x = Workbooks(wbname)
If Err = 0 Then WorkbookIsOpen = True  Else WorkbookIsOpen = False
End Function
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-11-29 02:10 , Processed in 0.093559 second(s), 30 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

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