[英]Having trouble removing characters from string in VB.net
I'm using Excel Interop with VB.net. 我正在VB.net中使用Excel Interop。 My problem is using string.Remove.
我的问题是使用string.Remove。 I am trying to remove the last 3 characters from cells that always contain strings with 11 characters.
我试图从始终包含11个字符的字符串的单元格中删除最后3个字符。
I have written some code to change the value of the cells in a range. 我已经编写了一些代码来更改某个范围内的单元格的值。
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
But when I try to use Remove, I'm getting the error: Object Reference not set to an instance of an Object
. 但是,当我尝试使用Remove时,出现了错误:
Object Reference not set to an instance of an Object
。
Can someone lead me in the right direction? 有人可以指引我正确的方向吗?
I think "the_lotus" is correct. 我认为“ the_lotus”是正确的。 Does this fix the error?
这样可以解决错误吗?
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
Edit: Ed's solution (with formatting) was: 编辑: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.