我正在尝试在excel中制作销售点以进行销售。我需要:
当条形码进入单元格A5之后,我希望单元格D5以默认数量“1”更新,并且
如果删除条形码,也应清除数量。
我能够达到#1但不能达到#2。到目前为止,我有以下代码,有人可以帮忙吗?
Sub SetQuantity()
Dim LastRow As Long
Dim i As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 5 To LastRow
If Not IsEmpty(Range("A" & i).Value) Then
Range("D" & i).Value = 1
Else: DeleteQuantity
End If
Next i
End Sub
Sub DeleteQuantity()
Dim LastRow As Long
Dim i As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 5 To LastRow
If Range("A" & i).Value = "" Then
Range("D" & i).Value = ""
End If
Next i
End Sub
答案 0 :(得分:3)
这样的事情可以避免所有循环,并可能更好地工作 -
Sub worksheet_change(ByVal Target As Range)
Dim lastrow As Integer
lastrow = Range("A" & Rows.Count).End(xlUp).Row
If Not Intersect(Target, Range("A5:A" & lastrow)) Is Nothing Then
If IsEmpty(Target) Then
Target.Offset(, 3) = vbNullString
Else: Target.Offset(, 3) = 1
End If
End If
End Sub
您只需将其放在要查看的工作表的工作表模块中即可。
答案 1 :(得分:1)
将此代码放入工作表模块中:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 And Target.Rows.Count = 1 And Target.Columns.Count = 1 Then
If Not IsEmpty(Target.Value) Then
Cells(Target.Row, 4) = 1
Else
Cells(Target.Row, 4).ClearContents
End If
End If
End Sub