我尝试将j变量从Range
更改为Variant
,但出现溢出错误。如果我更改为Integer
或long
,则会收到编译错误。
Dim j As Variant
Range("D83:D114").Select
With Application.WorksheetFunction
For Each j In Intersect(Selection, ActiveSheet.UsedRange)
j.Value = .Trim(j.Value)
Next j
End With
如何使我的变量没有溢出错误?有办法重置我的记忆吗?
编辑。
我建议进行调整,从代码中删除选择并放入范围。
现在overflow
在我的下一个循环中。所有这些变量都被多次使用并存储为Range
。我怎么不碰到这些问题?
For Each cellAFS In AFS.Cells
For Each cellFV In FVOCI.Cells
If cellFV.Value = cellAFS.Value Then
cellFV.Offset(0, 6).Value = cellAFS.Offset(0, 3).Value / 1000
End If
Next
Next
答案 0 :(得分:1)
下面的子
Sub ChangeValue()
Dim j As Range
For Each j In Range("D83:D114")
j = Trim(j.Value)
Next j
End Sub
答案 1 :(得分:1)
您的代码对我有用
您可以避免选择
Sub ChangeValue()
Dim j As Range
With Application.WorksheetFunction
For Each j In Intersect(Range("D83:D114"), ActiveSheet.UsedRange)
j.Value = .Trim(j.Value)
Next j
End With
End Sub
并检查空隙交叉点:
Sub ChangeValue()
Dim rng As Range, j As Range
Set rng = Intersect(Range("D83:D114"), ActiveSheet.UsedRange)
If rng Is Nothing Then Exit Sub
With Application.WorksheetFunction
For Each j In rng
j.Value = .Trim(j.Value)
Next j
End With
End Sub