Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel &引用;方法“对象范围默认值失败”;在工作表中更改代码_Excel_Vba_Runtime Error - Fatal编程技术网

Excel &引用;方法“对象范围默认值失败”;在工作表中更改代码

Excel &引用;方法“对象范围默认值失败”;在工作表中更改代码,excel,vba,runtime-error,Excel,Vba,Runtime Error,当我更改指定列(第19列)中的值时,代码需要一段时间才能运行,但似乎工作正常,然后抛出错误。这也会导致Excel在调试代码时反复崩溃 Private Sub Worksheet_Change(ByVal Target As Range) If Target.Column = 19 Then If Target <> "yes" Then Target = "no" End If End If End Sub P

当我更改指定列(第19列)中的值时,代码需要一段时间才能运行,但似乎工作正常,然后抛出错误。这也会导致Excel在调试代码时反复崩溃

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 19 Then
        If Target <> "yes" Then
            Target = "no"
        End If
    End If
End Sub
Private子工作表\u更改(ByVal目标作为范围)
如果Target.Column=19,则
如果目标为“是”,则
Target=“否”
如果结束
如果结束
端接头
我尝试过将目标更改为activecell,使用显式引用等,但似乎所有操作都会抛出相同的错误并导致Excel崩溃


有什么想法吗?

是否要将每个非“是”元素更改为“否”

Sub-stringToNo()
列数=19
lastrow=fLastRow
对于i=1到最后一行
如果单元格(i,列号)“是”,则
单元格(i,列号)=“否”
如果结束
接下来我
端接头
函数fLastRow()的长度为
fLastRow=0
如果工作表function.CountA(单元格)>0,则
fLastRow=Cells.Find(What:=“*”,After:=[A1]_
搜索顺序:=xlByRows_
搜索方向:=xlPrevious).行
如果结束
端函数

Target=“no”
导致事件在无限循环中触发:
Target“yes”
将失败,如果
Target
对应于一个多单元格范围。@BigBen更改为
如果Target“yes”和Target“no”,则
成功!你的另一个评论是我没有想到的另一个问题,但修复它需要更多的工作,所以我会记住这一点。谢谢这是有道理的,
worksheet\u Change
方法通过更改工作表来触发自身。编辑:你提供的链接使它实际上很容易,再次感谢!注意:如果您想知道
Range.\u Default
失败的成员调用来自哪里,这是因为
Range
类有一个默认成员,并且
Target“yes”和
Target=“no”
涉及一个;您可以使用
Target.Value“yes”和
Target.Value=“no”
显式调用。我主要是这样使用的,如果我想删除“yes”,我只需在单元格上按delete键,它就会将其改回“no”,或者我想如果有人试图在列中输入一个模糊的值,它只会保持“no”
Sub stringToNo()
    columnNumber = 19
    lastrow = fLastRow

    For i = 1 To lastrow
        If Cells(i, columnNumber) <> "yes" Then
            Cells(i, columnNumber) = "no"
        End If
    Next i
End Sub
Function fLastRow() As Long
    fLastRow = 0
    If WorksheetFunction.CountA(Cells) > 0 Then
        fLastRow = Cells.Find(What:="*", After:=[A1], _
              SearchOrder:=xlByRows, _
              SearchDirection:=xlPrevious).Row
    End If
End Function