VBA 判断工作簿是否包含公式

1

Option Explicit
'----------------------------------------------------------------------
'MAIN
'----------------------------------------------------------------------
Sub Button1_Click()
    Dim i As Integer
    Dim strInputPath As String
    Dim fileList() As String

    '----------------------------------------------------------------------
    'INPUT PATH
    '----------------------------------------------------------------------
    strInputPath = ThisWorkbook.Sheets("tool").Range("B1").Value
    ThisWorkbook.Sheets("tool").Range("F:H").ClearContents
    
    Application.ScreenUpdating = False
    
    '----------------------------------------------------------------------
    '設計書を取得
    '----------------------------------------------------------------------
    fileList = GetFile(strInputPath)
    
    ThisWorkbook.Sheets("tool").Range("F1").Value = "詳細仕様書名"
    ThisWorkbook.Sheets("tool").Range("G1").Value = "公式有無"
    
    For i = 0 To UBound(fileList)
        If fileList(i) <> "" Then
            ThisWorkbook.Sheets("tool").Range("F" & i + 2).Value = fileList(i)
            ThisWorkbook.Sheets("tool").Range("G" & i + 2).Value = Excel2SQL(strInputPath, fileList(i))
        End If
    Next

    Application.DisplayAlerts = True
    Application.AskToUpdateLinks = True
    Application.ScreenUpdating = True
End Sub

'----------------------------------------------------------------------
'Get Execl Files List
'----------------------------------------------------------------------
Function GetFile(ByVal strInputPath As String) As String()

    '----------------------------------------------------------------------
    '変数定義
    '----------------------------------------------------------------------
    Dim strFileName As String
    Dim i As Integer
    Dim fileList() As String
    
    '----------------------------------------------------------------------
    '初期化
    '----------------------------------------------------------------------
    i = 0
    strFileName = Dir(strInputPath & "\*設計書*.xlsx")
    
    Do While strFileName <> ""
        ReDim Preserve fileList(i)
        fileList(i) = strFileName
        i = i + 1
        strFileName = Dir
    Loop
    GetFile = fileList
End Function

'----------------------------------------------------------------------
'Exccl To Sql File
'----------------------------------------------------------------------
Function Excel2SQL(ByVal strInputPath As String, ByVal strFile As String)
    
    '----------------------------------------------------------------------
    '変数定義
    '----------------------------------------------------------------------
    Dim objWk As Workbook
    If strFile <> "" Then
        Application.DisplayAlerts = False
        Application.AskToUpdateLinks = False
        Set objWk = Workbooks.Open(strInputPath & "\" & strFile)

        Excel2SQL = ParseDescSQLs(objWk)
        objWk.Close
    End If

End Function

Function ParseDescSQLs(ByRef wk As Workbook)
    On Error GoTo line1
    
    Dim sh As Worksheet
    Dim iRow As Integer
    Dim iCol As Integer
    Dim i As Integer
    Dim j As Integer
    
    For Each sh In wk.Worksheets
        sh.Cells.SpecialCells(xlCellTypeLastCell).Activate
        iRow = Selection.Row
        iCol = Selection.Row

        For i = 1 To iRow
            For j = 1 To iCol
                If sh.Cells(i, j).HasFormula = True Then
                    ParseDescSQLs = "有"
                    Exit Function
                End If
            Next j
        Next i
    Next
line1:
    ParseDescSQLs = "無"
    Resume Next
End Function

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值