簡體   English   中英

無法從VB.net的字符串中刪除字符

[英]Having trouble removing characters from string in VB.net

我正在VB.net中使用Excel Interop。 我的問題是使用string.Remove。 我試圖從始終包含11個字符的字符串的單元格中刪除最后3個字符。

我已經編寫了一些代碼來更改某個范圍內的單元格的值。

    Dim lastrow5 As Integer

    lastrow5 = xlWsheet2.UsedRange.Rows.Count

    Dim myRange5, z As Excel.Range

    myRange5 = xlWsheet2.Range("E1:E" & lastrow5)

    For Each z In myRange5

        z.Value = z.Value.remove(8, 3)

    Next

但是,當我嘗試使用Remove時,出現了錯誤: Object Reference not set to an instance of an Object

有人可以指引我正確的方向嗎?

我認為“ the_lotus”是正確的。 這樣可以解決錯誤嗎?

    Dim lastrow5 As Integer

    lastrow5 = xlWsheet2.UsedRange.Rows.Count

    Dim myRange5, z As Excel.Range

    myRange5 = xlWsheet2.Range("E1:E" & lastrow5)

    For Each z In myRange5

        If cell.Value Is Not Nothing Then z.Value = z.Value.remove(8, 3)

    Next

編輯:Ed的解決方案(使用格式)是:

Dim lastrow5 As Integer lastrow5 = xlWsheet2.UsedRange.Rows.Count 
Dim myRange5, z As Excel.Range myRange5 = xlWsheet2.Range("E1:E" & lastrow5) 
For Each z In myRange5 
        If (z.Value Is Nothing) Then 
        ElseIf z.Value.ToString = "null" Then 
        Else z.Value = z.Value.ToString.Remove(8, 3) 
        End If 
Next

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM