'如果'Do循环中的语句类型不匹配

时间:2015-09-24 02:24:05

标签: excel vba excel-vba

我在网上试图弄清楚为什么我在我的If语句中遇到类型不匹配错误,但似乎无法让我的脑袋缠绕它。任何帮助将不胜感激,并提前感谢。顺便说一句,i整数在前一段代码中声明。

Do While i < 25000
j = 4
Range("AV4:AV550").Select
Selection.Copy
Range("AU4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    'Check for a new high value
    Do Until j = 551
        If Range("AX" & j).Value < Range("AS" & j).Value Then
        Range("AX" & j) = Range("AS" & j)
        End If
        j = j + 1
    Loop

Range("AL4:AR550").Calculate
i = i + 1
Loop

2 个答案:

答案 0 :(得分:3)

更新If语句以仅比较值:

<强> If Val(Range("AX" & j).Value) < Val(Range("AS" & j).Value) Then

Option Explicit

Public Sub doStuff()

    Dim i As Long, j As Long

    For i = 0 To 25000
        Range("AV4:AV550").Copy
        Range("AU4").PasteSpecial Paste:=xlPasteValues

        'Check for a new high value
        For j = 4 To 551

            'check for NULL
            If IsNull(Range("AX" & j).Value) Then Range("AX" & j).Value = vbNullString
            If IsNull(Range("AS" & j).Value) Then Range("AS" & j).Value = vbNullString

            'remove special characters
            Range("AX" & j).Value = WorksheetFunction.Clean(Trim(Range("AX" & j).Value))
            Range("AS" & j).Value = WorksheetFunction.Clean(Trim(Range("AS" & j).Value))

            If Val(Range("AX" & j).Value) < Val(Range("AS" & j).Value) Then
                Range("AX" & j) = Range("AS" & j)
            End If
        Next
        Range("AL4:AR550").Calculate
    Next

End Sub
'Val() extracts any numbers it finds at the start of a string and it stops at first alpha

Public Sub testVal()
    Debug.Print "test"          '---> "test"
    Debug.Print Val("test")     '---> 0
    Debug.Print Val("123test")  '---> 123
    Debug.Print Val("test456")  '---> 0
End Sub

答案 1 :(得分:0)

On Error Resume Next做了伎俩。