我试图遍历一列,并将每个值增加一个,直到它为空,但出现运行时错误:
运行时错误'13':类型不匹配
请帮助。
Private Sub CommandButton1_Click()
Start = Val(Range("H2").Value)
LastRow = Cells(Rows.Count, 8).End(xlUp).Row
For Row = 2 To LastRow
If Not IsEmpty(Range("H" & Row)) Then
Range("H" & Row).Value = Range("H" & Row).Value + 1
End If
Next
End Sub
答案 0 :(得分:2)
这里有一些未经测试的代码可以隔离您的问题。设置范围的方式有些奇怪。试试看。
Dim ws As Worksheet: Set ws = ActiveSheet 'i assume this is correct
Dim r As Long
Dim cNumber As Long
cNumber = Range("h2").Column 'just for illustration
For r = 2 To ws.UsedRange.Cells(ws.UsedRange.Rows.Count, 1).Row
If Not IsEmpty(ws.Cells(r, cNumber)) Then
If IsNumeric(ws.Cells(r, cNumber)) Then
ws.Cells(r, cNumber).Value = ws.Cells(r, cNumber).Value + 1
Else
'cell is not empty but does not have a numeric value
'Stop
End If
Else
Exit For 'ends the loop
End If
Next r