我有一个奇怪的问题。只要我没有将单元格地址分配给变量komorka_k
和komorka_y
,该代码就可以正常工作。由于禁用了标有“LINE 1”和“LINE 2”的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
答案 0 :(得分: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