[英]Changing a positive numeric cell input into a negative numeric value in the same cell based on criteria in another cell
我想通过引用另一个单元格范围中的条件将一个范围内的正值单元格输入更改为负值。 因此,例如单元格范围 A1:A10 包含值“B”或“S”。 单元格区域 B1:B10 是输入数值的位置。 根据已在相应单元格 A1:A10 中输入的数据,输入这些值时将其设为正值或负值。 因此,在 B1 中输入任何值(无论是正值还是负值)为 1234 或 -1234,其中 A1 的值为“B”将导致 B1 显示 -1234。 相反,如果在单元格 B1:B10 中输入任何正值或负值,并且 A 列中相应行的值为“S”,则 B 列中的值将始终为正值,无论原始输入是负值还是正值。
如果 A1:A10 范围内的特定单元格中没有值对应于 B 列中的同一行,则应向用户显示一条消息,说“请在 A 列的相应行中输入一个值。
我是 VBA 编码的完全新手,到目前为止,我正在查看其他帖子已经拼凑了以下代码,但我不知道如何完成它才能成功工作。
任何帮助将不胜感激。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A1 As Range
Set A1 = Range("A1:A10")
Dim A2 As Range
Set A2 = Range("B1:B10")
If Intersect(Target, A2) Is Nothing Then Exit Sub
If IsText(Target, A1) Then
If A1 = "S" Then
Application.EnableEvents = False
B1 = -B1
Application.EnableEvents = True
End If
End Sub
这应该是您需要的:
For
循环允许您一次更改多个B列值。
如果 A 列的值既不是“B”也不是“S”,则不采取任何措施。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim B As Range, Intersection As Range, cell As Range
Dim v As String
Set B = Range("B1:B10")
Set Intersection = Intersect(Target, B)
If Intersection Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each cell In Intersection
v = cell.Offset(0, -1).Value
If v = "B" Then
cell.Value = -Abs(cell.Value)
ElseIf v = "S" Then
cell.Value = Abs(cell.Value)
End If
Next cell
Application.EnableEvents = True
End Sub
Sheet1
)中。FirstRow
中的单元格开始到最后一个非空单元格的列范围。A
列中的值自动修改B
列中输入的值。编码
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Const Proc As String = "Worksheet_Change"
On Error GoTo cleanError
Const FirstRow As Long = 1
Dim Criteria As Variant: Criteria = Array("S", "B", "")
Dim Cols As Variant: Cols = Array(1, 2) ' or Array("A", "B")
Dim rngS As Range: Set rngS = getColumnRange(Me, Cols(1), FirstRow)
If rngS Is Nothing Then Exit Sub
Dim rngT As Range: Set rngT = Intersect(Target, rngS)
If rngT Is Nothing Then Exit Sub
Dim ColOffset As Long
ColOffset = Columns(Cols(0)).Column - Columns(Cols(1)).Column
Application.EnableEvents = False
Dim cel As Range
For Each cel In rngT.Cells
Select Case cel.offset(, ColOffset).Value
Case Criteria(0): cel.Value = Abs(cel.Value)
Case Criteria(1): cel.Value = -Abs(cel.Value)
Case Criteria(2): cel.Value = "Please enter a value into cell '" _
& cel.offset(, ColOffset).Address(0, 0) & "'."
Case Else ' Maybe the same as the previous!?
End Select
Next
CleanExit:
Application.EnableEvents = True
Exit Sub
cleanError:
MsgBox "An unexpected error occurred in '" & Proc & "'." & vbCr _
& "Run-time error '" & Err.Number & "':" & vbCr _
& Err.Description, vbCritical, Proc & " Error"
On Error GoTo 0
GoTo CleanExit
End Sub
Function getColumnRange(Sheet As Worksheet, _
ByVal AnyColumn As Variant, _
Optional ByVal FirstRow As Long = 1) _
As Range
Dim rng As Range
Set rng = Sheet.Columns(AnyColumn).Find("*", , xlValues, , , xlPrevious)
If rng Is Nothing Then Exit Function
If rng.Row < FirstRow Then Exit Function
Set getColumnRange = Sheet.Range(Sheet.Cells(FirstRow, AnyColumn), rng)
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.