简体   繁体   English

Excel VBA Range.Value类型不匹配?

[英]Excel VBA Range.Value type mismatch?

I've written code to delete a row if a cell in column G has the value " - ". 如果G列中的单元格值为“-”,我已经编写了删除行的代码。 However, I'm getting a type mismatch when referring to the cell in the for each loop. 但是,当引用每个循环中的单元格时,我遇到类型不匹配的情况。 I know the problem is coming from "curr.Value," but I don't know how to fix it. 我知道问题出在“ curr.Value”,但我不知道如何解决。 Any ideas? 有任何想法吗?

Here's my code: 这是我的代码:

Sub deleteRows()
Dim curr As Range
Dim ws As Worksheet
Set ws = Workbooks("ExtractedColumns").Sheets("practice")
For Each curr In
    Workbooks("ExtractedColumns").Sheets("practice").Range("G:G")
     If curr.Value = " - " Then
        EntireRow.Delete
     End If
Next
End Sub

I'm sorry, but the reason for your errors is not what you accepted as "Answer". 抱歉,您输入错误的原因不是您接受的“答案”。

First, when you loop : For Each curr In Workbooks("ExtractedColumns").Sheets("practice").Range("G:G") 首先,当您循环时: For Each curr In Workbooks("ExtractedColumns").Sheets("practice").Range("G:G")

the syntax suppose to be in 1 line : 语法假定为1行:

For Each curr In Workbooks("ExtractedColumns").Sheets("practice").Range("G:G")

(unless you had - between the 2 lines of code, or you didn't share your code as you have it). (除非您有-在两行代码之间,否则就不会共享您的代码)。

However, you already assigned Workbooks("ExtractedColumns").Sheets("practice") to ws in the previous line, so why not use For Each curr In ws.Range("G:G") . 但是,您已经在上一行中为ws分配了Workbooks("ExtractedColumns").Sheets("practice") ,所以为什么不使用For Each curr In ws.Range("G:G")

Furthermore, looping through your entire column G will take forever, loop through only occupied cells in Column G, with: 此外,遍历整个G列将永远花费,仅遍历G列中已占用的单元格,其中包括:

For Each curr In ws.Range("G1:G" & ws.Cells(ws.Rows.Count, "G").End(xlUp).Row)

2ns reason, once you find the line where curr.Value = " - " , and you want to delete that entire row, you need to use curr.EntireRow.Delete . 2ns原因,一旦找到curr.Value = " - " ,并且想要删除整个行,则需要使用curr.EntireRow.Delete

And last, like mentioned by @Comintern, you need to trap cells with errors, you don't need to assign another variable to the curr.Value , just use If IsError(curr.Value) Then . 最后,就像@Comintern提到的那样,您需要捕获有错误的单元格,不需要为curr.Value分配另一个变量,只需使用If IsError(curr.Value) Then

Code

Sub deleteRows()

Dim curr As Range
Dim ws As Worksheet

Set ws = Workbooks("ExtractedColumns").Sheets("practice")

' loop through only occupied cells in Column G
For Each curr In ws.Range("G1:G" & ws.Cells(ws.Rows.Count, "G").End(xlUp).Row)
    On Error Resume Next
    If IsError(curr.Value) Then ' check if cell.value return an error
        If Err.Number <> 0 Then
            Err.Clear
            On Error GoTo 0
        End If
    Else ' current cell.value doesn't return an error
        If curr.Value = " - " Then
            curr.EntireRow.Delete '<-- delete entire row
        End If
    End If
Next curr

End Sub

You could try to assign the Value to a variant and checking the type. 您可以尝试将值分配给变量并检查类型。

Dim vVal as Variant


.....

vVal = curr.Value
if IsError(vVal) then 

etc
.... 

I suspect the type mismatch error is being caused by curr containing a numeric value, being compared to a string... hard to know for sure without being able to see the rest of the code and how curr is populated but try using curr.text which returns a string for your If test, instead of curr.value . 我怀疑类型不匹配错误是由包含数字值的curr引起的,与字符串进行比较...很难确定,但无法看到其余代码以及如何填充curr,但尝试使用curr.text它为您的If测试返回一个字符串,而不是curr.value

Hope this helps, TheSilkCode 希望这会有所帮助,TheSilkCode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM