vba 运行时错误“13”类型不匹配 IF 值 = Then 语句循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19057325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:32:27  来源:igfitidea点击:

Run-time Error '13' Type Mismatch IF value = Then Statement loop

vbaexcel-vbaexcel-2007excel

提问by favoritewordgoeshere

Basically what I'm doing is I have data in columns A-I and I'm trying to copy out data [Columns D-G] by their value in D. In this loop I'm looking for when the entry in D says "1CME Cash-Settled Butter".

基本上我正在做的是我在列 AI 中有数据,我试图通过它们在 D 中的值复制数据 [列 DG]。在这个循环中,我正在寻找当 D 中的条目说“1CME Cash-沉淀的黄油”。

I then starting in column K (=11) I paste it. Then I stack the rows.

然后我从 K 列 (=11) 开始粘贴它。然后我堆叠行。

The loop starts over and starts looking for a different value.

循环重新开始并开始寻找不同的值。

My problem is that I've ran the code fine for many runs. I haven't changed the code since, but now I'm getting Run-Time Error Type '13' Mismatch. Is there someway to fix this or write new code?

我的问题是我已经多次运行代码。从那以后我没有更改代码,但现在我收到运行时错误类型“13”不匹配。有没有办法解决这个问题或编写新代码?

I'm just learning VBA!

我刚学VBA!

Thanks!

谢谢!

iRow = 2
For Each i In Range("D2:D200")
    If i.Value = "1CME Cash-Settled Butter" Then
        Range(i.Offset(0, 0), i.Offset(0, 3)).Copy
        Cells(iRow, 11).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,   SkipBlanks:=False, Transpose:=False
        iRow = iRow + 1
    End If
Next i

iRow = 30
For Each i In Range("D2:D200")
    If i.Value = "-1CME Cash-Settled Butter" Then
        Range(i.Offset(0, 0), i.Offset(0, 3)).Copy
        Cells(iRow, 11).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        iRow = iRow + 1
    End If
Next i

回答by Johnny Bones

Change i.Valueto i.Text. You don't want the Value, because you're comparing it to an integer. You want to check the String, and using i.Textwill do that.

更改i.Valuei.Text。您不需要 Value,因为您将它与整数进行比较。你想检查字符串,使用i.Text就可以了。

You can also cstr(i.Value), but that's not the best way to do it.

你也可以cstr(i.Value),但这不是最好的方法。