关于Null、Empty、vbNullString、Nothing差别

本文详细介绍了VBA编程语言中Null与Nothing的概念及其应用。Null表示未定义的变量,而Nothing则用于释放对象引用。文章还探讨了Empty、vbNullString等概念,并提供了实用的代码示例。

这篇博客根据网友那里获取来的,不是本人亲写亲测。

Null:Null是一个象Integer或者String一样的变量类型,它表示一个没有合法数据的变量。

       这有别于zero、Nothing、Empty或者vbNullString。许多数值与Null结合在一起,都将产生Null结果。

       比如:    表达式 结果 Null - Null Null Null + 7 7 Null = Null Null   

       你可以使用IsNull语句来判断表达式是否为Null:   

        If IsNull(my_variable) Then ...   

Empty:这也是一个象Integer或者String一样的变量类型,它表示了一个还没有进行初始化的变量。它与Null的意义不同,

       Null表示没有合法数据。    一个没有初始化的变量的数值是Empty。你可以使用IsEmpty语句来判断是否变量进行了初始化:

           If IsEmpty(my_variant) Then ...    

Nothing:这是一个指向空对象的对象引用。将对象引用设置为Nothing,就释放了那个对象。如果没有其他的引用指向对象,

      Visual Basic就将销毁这个对象。    Dim obj As Form1    :    Set obj = Nothing

       注释: Free the object reference.    使用Is Nothing语句来判断是否一个引用为Nothing:   

       If obj Is Nothing Then ...    vbNullString:这是个常量,表示一个empty字符串。它与空白字符串""不同,

       表示什么也没有的字符串(nothing string)。对于许多场合,它被当作一个empty字符串""处理,

       真正使用它的目的是传递null参数给库函数。    Null是一个很奇怪的数值,它不是zero,不是Nothing,不是vbNullString。

       它是没有定义的东西。    判断字符串是否为空白    有以下几种方法判断一个字符串是否为空白:

       Dim txt As String

       Dim blank As String blank = "" : If Len(txt) = 0 Then ...

       If txt = vbNullString Then ... If txt = "" Then ... If txt = blank Then ...   

       经过测试,Len(txt)=0的方法要比其他方法快20%多。

 

 

Nothing在VBA中的用途

我们常在VBA代码中看到Nothing这个单词,如:

    Set d = Nothing

    If d is Nothing

 Nothing到底是什么意思呢?帮助里是这样解释的:

使用 Nothing 关键字被将对象变量从实际对象中分离开来。要使用 Set 语句将 Nothing 赋值给对象变量。

通俗一点就是判断某个对象是否存在(is Nothing)和从内存资源中释放这个对象(= Nothing)

例如我们在EP上找一个会员, 我们就可以这样写

Dim aa as Ep会员   '假设置Ep会员是一个对象类型

Set aa="小A"     '用Set 设置对象变量为会员"小A"

If  aa Is Nothing Then     '如aa不存在

   Msgbox "这个会员不存在"

Else

     Msgbox "这个会员存在"

End If

-----------------------------------------------------------------------------------------------------------------

而且Set 对象变量=Nothing   则是把从非Excel库提库的外来引用对象从内存资源中释放出去.如

Dim d As New Dictionary   '创建一个新的字典对象
Set d = Nothing                '从内存资源中释放出来

下面这段代码有什么问题吗Function GetMergedCellsInfo(filepath As String) As Variant Dim wb As Workbook, ws As Worksheet Dim lastRow As Long, i As Long Dim area As Range, cell As Range Dim JsonData() As Variant Dim resultDict As Object Dim results As New Collection Dim calcState As XlCalculation Dim screenState As Boolean Dim PostData As Object calcState = Application.Calculation screenState = Application.ScreenUpdating Application.Calculation = xlCalculationManual Application.ScreenUpdating = False If Dir(filepath) = "" Then Err.Raise 53, , "文件不存在" Set wb = Workbooks.Open(filepath) Set ws = wb.Sheets(1) lastRow = ws.Cells(ws.Rows.count, "A").End(xlUp).Row i = 1 Do While i <= lastRow If ws.Cells(i, 1).MergeCells Then Set area = ws.Cells(i, 1).MergeArea JsonData = SendGetRequest(ws.Cells(area.Row, 1).value) For Each cell In ws.Range("S" & i, "X" & i + area.Rows.count - 1) Set resultDict = CreateObject("Scripting.Dictionary") resultDict("SAMPLE_ID") = JsonData(0) resultDict("Group_ID") = "2" resultDict("TP_ID") = "11868" resultDict("RESULT") = cell.value results.Add resultDict Next cell PostData("SampleIDList") = JsonData(1) PostData("ResultList") = results i = i + area.Rows.count Else i = i + 1 End If Loop CleanExit: wb.Close False Set ws = Nothing Set wb = Nothing Application.Calculation = calcState Application.ScreenUpdating = True Exit Function End Function 'get request Function SendGetRequest(order_no As String) As Variant() On Error GoTo ErrorHandler Dim httpRequest As Object Dim url As String Dim jsonArray As Object Dim jsonResult(0 To 1) As Variant Dim firstItem As Object jsonResult(0) = vbNullString jsonResult(1) = vbNullString Set httpRequest = CreateObject("MSXML2.XMLHTTP.6.0") url = "http://cn-dc-lims-trg.tuv.group/LabAPI/api/LimsApi/GetSampleIDsBySalesOrderNo/" & order_no With httpRequest .Open "GET", url, False .setTimeouts 5000, 5000, 5000, 5000 .send If .Status = 200 Then Dim validJson As String validJson = Replace(.responseText, "'", """") validJson = Replace(validJson, ":""null""", ":null") Set jsonArray = JsonConverter.ParseJson(validJson) If jsonArray.count > 0 Then Set firstItem = jsonArray(1) jsonResult(0) = firstItem("SAMPLE_ID") jsonResult(1) = jsonArray End If Else Err.Raise vbObjectError, , "HTTP Error " & .Status End If End With CleanExit: SendGetRequest = jsonResult Set httpRequest = Nothing Exit Function ErrorHandler: jsonResult(0) = "ERROR" jsonResult(1) = "Error " & Err.Number & ": " & Err.Description Resume CleanExit End Function
最新发布
12-16
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值