zq861010 发表于 2010-4-4 23:22:21

【已解决】判断Excel表格为空的问题

我想判断Excel表格里面是否有值怎么判断啊,貌似不等于null;请各位大侠给个建议

[ 本帖最后由 yujie6832 于 2010-4-6 09:29 编辑 ]

TIB 发表于 2010-4-5 12:37:49

用Excel的COM编程就可以了

例如:
Dim oExcel
    Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = 1
oExcel.Workbooks.Add
oExcel.Cells(1,1).Value = "one"
oExcel.Cells(1,2).Value = "two"

用Cells对象的Value来取到值判断:
If oExcel.Cells(1,3)=vbEmpty Then
        Msgbox "Empty!"
End If
或者
If oExcel.Cells(1,3)="" Then
        Msgbox "空!"
End If

yujie6832 发表于 2010-4-6 09:28:46

楼上正解

ecust 发表于 2010-4-6 10:29:06

可创建一个新的空excel然后做对比.

Function CompareSheets(sheet1, sheet2, startColumn, numberOfColumns, startRow, numberOfRows, trimed) 'As Boolean
    Dim returnVal 'As Boolean
    returnVal = True

    'In case that one of the sheets doesn't exists, don't continue the process
    If sheet1 Is Nothing Or sheet2 Is Nothing Then
      CompareSheets = False
      Exit Function
    End If

    'loop through the table and fill values into the two worksheets
    For r = startRow to (startRow + (numberOfRows - 1))
      For c = startColumn to (startColumn + (numberOfColumns - 1))
            Value1 = sheet1.Cells(r, c)
            Value2 = sheet2.Cells(r, c)

            'if 'trimed' equels True then used would like to ignore blank spaces
            If trimed Then
                Value1 = Trim(Value1)
                Value2 = Trim(Value2)
            End If

            'in case that the values of a cell are not equel in the two worksheets
            'create an indicator that the values are not equel and set return value
            'to False
            If Value1 <> Value2 Then
                Dim cell 'As Excel.Range
                sheet2.Cells(r, c) = "Compare conflict - Value was '" & Value2 & "', Expected value is '" & Value1 & "'."
                Set cell = sheet2.Cells(r, c)
                cell.Font.Color = vbRed
                returnVal = False
            End If
      Next
    Next
    CompareSheets = returnVal
End Function
页: [1]
查看完整版本: 【已解决】判断Excel表格为空的问题