0

我有一个奇怪的问题。只要我不将单元格的地址分配给变量komorka_kkomorka_y. 由于两行标有“LINE 1”和“LINE 2”的代码被禁用,VBA 宏工作正常。这种活动的原因是什么?分配一个与子模块的任何其他部分不相关的值怎么可能使它表现不同?

Public stara_wartosc As Variant
Public czy_wiekszy_zakres As Boolean

Private Sub Worksheet_Change(ByVal Target As Range)

Dim x, y As Integer
Dim x_err As Integer
Const y_err = 4
Dim nowa_wartosc As Variant
Dim komorka_x As String
Dim komorka_y As String
Const kon_col = 72

komorka_x = ""
komorka_y = ""

x = Target.row
y = Target.Column

nowa_wartosc = Target.Value

If czy_wiekszy_zakres = True Then
    stara_wartosc = nowa_wartosc
End If

On Error GoTo TypeMismatch

If stara_wartosc <> nowa_wartosc And czy_wiekszy_zakres = False Then

    If Target.Worksheet.Cells(x, 2).Value = "" Or Target.Worksheet.Cells(x, 2).Value = 0 Then

        Application.EnableEvents = False

            Target.ClearContents
            MsgBox Prompt:="Zmieniłeś wartość komórki bez wpisania numeru zlecenia." & vbCrLf & "Wpisz nr zlecenia!", Title:="ZACHOWUJESZ SIĘ NIEWŁAŚCIWIE, MÓJ DROGI!"
            Target.Worksheet.Cells(x, 2).Activate

        Application.EnableEvents = True

            Exit Sub

    End If

    With ActiveWorkbook.Worksheets("Errata")

        komorka_x = .Range("A:A").Find(x, LookIn:=xlValues).Address 'LINE 1
        komorka_y = .Range("B:B").Find(y, LookIn:=xlValues).Address 'LINE 2

        x_err = .Cells(Rows.Count, 1).End(xlUp).row + 1

        If .Cells(x_err, 1).Value = 0 Or .Cells(x_err, 1).Value = "" Then
        .Cells(x_err, 1).Value = x
        End If
        If .Cells(x_err, 2).Value = 0 Or .Cells(x_err, 2).Value = "" Then
        .Cells(x_err, 2).Value = y
        End If

Set_values:
        .Cells(x_err, y_err - 1).Value = stara_wartosc
        .Cells(x_err, y_err).Value = Target.Value
        .Cells(x_err, y_err + 1).Value = Target.Worksheet.Cells(x, 2).Value

    End With

End If

TypeMismatch:
If Err = 13 Then
    Exit Sub
End If

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count = 1 Then
        stara_wartosc = Target.Value
        czy_wiekszy_zakres = False
    Else
        czy_wiekszy_zakres = True
    End If
End Sub
4

1 回答 1

2

大概

komorka_x = .Range("A:A").Find(x, LookIn:=xlValues).Address
komorka_y = .Range("B:B").Find(y, LookIn:=xlValues).Address

什么也没找到。因此.Address失败,因为没有找到结果没有地址。

然后因为On Error GoTo TypeMismatch它跳转到这里的错误处理。

所以确保.Find不是什么都不是:

Dim FoundX As Range
Set FoundX = .Range("A:A").Find(x, LookIn:=xlValues)
If Not FoundX Is Nothing Then
    komorka_x = FoundX.Address
Else
    MsgBox "Nothing found for x=" & x
End If
于 2018-03-19T10:53:24.647 回答