51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 3684|回复: 3
打印 上一主题 下一主题

[原创] 【已解决】判断Excel表格为空的问题

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2010-4-4 23:22:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我想判断Excel表格里面是否有值  怎么判断啊,貌似不等于null;请各位大侠给个建议

[ 本帖最后由 yujie6832 于 2010-4-6 09:29 编辑 ]
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
发表于 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
回复 支持 反对

使用道具 举报

该用户从未签到

3#
发表于 2010-4-6 09:28:46 | 只看该作者
楼上正解
回复 支持 反对

使用道具 举报

该用户从未签到

4#
发表于 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 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-10-6 22:26 , Processed in 0.101412 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

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