我希望通过添加一些单元格地址来更新公式,但当我运行它时,在第一次递增后得到一个类型不匹配错误。
编辑:错误已经被发现,它正在工作,但在另一个工作表中有另一个问题,一个日期变得不正常,并在没有任何原因的情况下变成了#N/A。不管怎样,我现在的问题是:对于i=1 to 3,我得到的结果是=0+$X$3+$AI$3+$AT$3+$BE$3+$BP$3+$CA$3。如何去掉字母周围的$?我想扩展一下这个公式。我知道,只需单击单元格并按F4键3次,我就可以清除所有的$,但是有没有一种自动的方法呢?下面是我的代码:
Public Sub test3()
Dim i As Integer
With Sheet10
.Cells(3, 13).Formula = "=0"
For i = 1 To 100
If .Cells(1, 22 * i - 8) <> 0 Then
.Cells(3, 13).Formula = .Cells(3, 13).Formula & "+" & .Cells(3, 22 * i + 2).Address
End If
If .Cells(1, 22 * i + 3) <> 0 Then
.Cells(3, 13).Formula = .Cells(3, 13).Formula & "+" & .Cells(3, 22 * i + 13).Address
End If
Next i
End With
End Sub
发布于 2017-07-13 12:58:28
我只能通过确保其中一个被检查的单元格(使用单元格<>0
)包含错误来重新创建您提到的错误(类型不匹配)。我通过输入=2/0
来模拟这一过程,以创建一个被零除的结果。(#DIV/0!
)
下面的代码是您的代码,但是两个if语句被包装在另一个IF语句中,该语句首先检查该单元格是否为错误。如果是,则不会将其添加到M3
公式中。如果你需要修改它来包含它,如果它确实包含错误,请让我知道,我会重新起草。
Public Sub test3()
Dim i As Integer
With Sheet10
.Cells(3, 13).Formula = "=0"
For i = 1 To 100
If Not (IsError(.Cells(1, 22 * i - 8))) Then
If .Cells(1, 22 * i - 8) <> 0 Then
.Cells(3, 13).Formula = .Cells(3, 13).Formula & "+" & .Cells(3, 22 * i + 2).Address
End If
End If
If Not (IsError(.Cells(1, 22 * i + 13))) Then
If .Cells(1, 22 * i + 13) <> 0 Then
.Cells(3, 13).Formula = .Cells(3, 13).Formula & "+" & .Cells(3, 22 * i + 13).Address
End If
End If
Next i
End With
End Sub
发布于 2017-07-13 12:40:38
最简单的方法是替换
.Cells(3, 22 * i + 13).Address
使用
Replace(.Cells(3, 22 * i + 13).Address,"$","")
https://stackoverflow.com/questions/45078654
复制相似问题