前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >VBA收藏一常用的自定义函数

VBA收藏一常用的自定义函数

作者头像
哆哆Excel
发布2022-10-25 14:03:07
5680
发布2022-10-25 14:03:07
举报
文章被收录于专栏:哆哆Excel
代码语言:javascript
复制
Sub 测试()
If IsFileExists("D:\new_temp\") Then
Debug.Print "存在"
Else
Debug.Print "不存在"
End If
End Sub

'参数名称    含义    说明
'strShtName  指定工作表名称  必选
'strWbName   指定工作簿名称  可选
'Sub Demo()
'    Debug.Print udfSheetExists("Sheet1")
'    Debug.Print udfSheetExists("Sheet1", "MyData.xlsx")
'End Sub
Function udfSheetExists(strShtName As String, Optional strWbName As String) As Boolean
    On Error Resume Next
    If strWbName = "" Then
        Set objWb = ActiveWorkbook
    Else
        Set objWb = Workbooks(strWbName)
    End If
    udfSheetExists = CBool(Not objWb.Sheets(strShtName) Is Nothing)
    On Error GoTo 0
End Function
'vba判断文件是否存在的两种方法
Function IsFileExists(ByVal strFileName As String) As Boolean
    If Dir(strFileName, 16) <> Empty Then
        IsFileExists = True
    Else
        IsFileExists = False
    End If
End Function

'Function IsFileExists(ByVal strFileName As String) As Boolean
'    Dim objFileSystem As Object
'    Set objFileSystem = CreateObject("Scripting.FileSystemObject")
'    If objFileSystem.fileExists(strFileName) = True Then
'        IsFileExists = True
'    Else
'        IsFileExists = False
'    End If
'End Function

'判断是否为字母
Public Function isABC(ByVal a)
    If a Like "[A-Za-z]*" Then
        isABC = True
    Else
        isABC = False
    End If
End Function
'vba判断文件是否是xls xlsx xlam文件
Public Function textNorY(str)
    Dim tarr
    tarr = VBA.Split(str, ".")
    s = tarr(UBound(tarr))
    Debug.Print s
    s_num = InStr(str, ":")
    If s = "xls" Or s = "xlsx" Or s = "xlsm" Then
        If s_num = 2 Then
        textNorY = True
        Else
            textNorY = False
        End If
    Else
        textNorY = False
    End If
End Function
'工作表,开始数,终止数,工作表说明,要的数组,要的数组开始行数,0列,1列,2列,3列,4列,5列
Sub into_arr(sht, star_n, end_n, sht_str, temparr, arr_star_n, n0, n1, n2, n3, n4, n5)
    jj = arr_star_n
    With sht
        For i = star_n To end_n
            If .Cells(i, n1) <> "" And .Cells(i, n2) <> "" Then
            temparr(jj, 1) = .Cells(i, n0)
            temparr(jj, 2) = .Cells(i, n1)
            temparr(jj, 3) = .Cells(i, n2)
            temparr(jj, 4) = .Cells(i, n3)
            temparr(jj, 5) = .Cells(i, n4)
            temparr(jj, 6) = .Cells(i, n5)
            temparr(jj, 7) = sht_str
            jj = jj + 1
            End If
        Next i
        
    End With
End Sub
'当前工作表中的删除空行
Sub 删除空行()
    Dim LastRow As Long
    Dim nowRow As Long
    LastRow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
    With Application
            .Calculation = xlCalculationManual
            .ScreenUpdating = False        '将屏幕更新关掉
        For nowRow = LastRow To 1 Step -1
           If Application.WorksheetFunction.CountA(Rows(nowRow)) = 0 Then
              Rows(nowRow).Delete
           End If
        Next nowRow
      .Calculation = xlCalculationAutomatic
      .ScreenUpdating = True
    End With
End Sub

Sub 删除空行2(sht)
    Dim LastRow As Long
    Dim nowRow As Long
    With sht
        LastRow = .UsedRange.Row - 1 + .UsedRange.Rows.Count
        For nowRow = LastRow To 1 Step -1
           If Application.WorksheetFunction.CountA(.Rows(nowRow)) = 0 Then
              .Rows(nowRow).Delete
           End If
        Next nowRow
    End With
End Sub
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-10-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 哆哆Excel 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档