[英]Excel VBA-code to alter a cell if a line has been edited
I'm doing some data validation work and basically, whenever I alter a line in the data I also need to write "Yes" in J-column on the same line.我正在做一些数据验证工作,基本上,每当我更改数据中的一行时,我还需要在同一行的 J 列中写“是”。 I figured this should be automatizable, but I had some trouble with the code.
我认为这应该是可自动化的,但我在代码上遇到了一些问题。
What I want it to do is to check for changes in CH rows, and if there's a numeral (0-99) on the B-column, then replace the text on the J-column of the same line with "Yes" (without the quotation marks).我想要它做的是检查 CH 行的变化,如果 B 列上有数字(0-99),则将同一行的 J 列上的文本替换为“是”(没有引号)。 The "numeral"-part can be abbreviated to having length of 1-2 (not 0 and not more) in this case.
在这种情况下,“数字”部分可以缩写为长度为 1-2(不是 0 也不是更多)。
Here's what I have thus far, but I can't seem to figure out how to do the combination of absolute and relative reference, ie "check B of the active row" or "alter J of the active row" (that is to say, none of the codes I've tried thus far have been valid according to VBA; I have very little VBA experience so this code feels alien to me):这是我到目前为止所拥有的,但我似乎无法弄清楚如何结合绝对和相对引用,即“检查活动行的 B”或“更改活动行的 J”(也就是说,到目前为止,根据 VBA,我尝试过的所有代码都不是有效的;我的 VBA 经验很少,所以这段代码对我来说很陌生):
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("C:H")) Is Nothing Then Exit Sub
If Not Len("B" & ActiveCellRow) = "1" Then
If Not Len("B" & ActiveCellRow) = "2" Then
Exit Sub
End If
With Application
.EnableEvents = False
.ScreenUpdating = False
"J" & ActiveCellRow = "Yes"
.EnableEvents = True
.ScreenUpdating = True
End With
You can do something like this:你可以这样做:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, c As Range, v
'in our range of interest?
Set rng = Application.Intersect(Target, Me.Range("C:H"))
If Not rng Is Nothing Then
'get the corresponding cells in ColB
For Each c In Application.Intersect(rng.EntireRow, Me.Range("B:B")).Cells
v = c.Value
'flag required?
If Len(v) > 0 And Len(v) < 3 And IsNumeric(v) Then
c.EntireRow.Cells(1, "J").Value = "Yes"
End If
Next c
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.