我在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
。
有人可以引导我朝正确的方向前进吗?
答案 0 :(得分:0)
我认为“ 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