[英]Change range of cells base on another cell value
I want to write a VBA code that applies to 1000
rows in excel.我想编写一个适用于 excel 中1000
行的 VBA 代码。 And in each row, once I change the cell value in column G
to No
, the respective row (ie column HZ
) will fill in with X
automatically.在每一行中,一旦我将列G
的单元格值更改为No
,相应的行(即列HZ
)将自动填充X
。
I tried to write the code below, but it doesn't work all the time, only appear occasionally, can someone help me with this, please?我试着写下面的代码,但它并不总是有效,只是偶尔出现,有人可以帮我吗?
Private Sub Worksheet_Change(ByVal Target As Range)
'MsgBox Target.Address
If Not Application.Intersect(Range("B1:B100"), Range(Target.Address)) Is Nothing Then
Call Change_value
End If
End Sub
Sub Change_value()
If ActiveCell.Value = "No" Then
Cells(ActiveCell.Row, 4).Value = "Ok"
End If
End Sub
@Aiden – here's what's wrong with your question: @Aiden – 您的问题有什么问题:
It's contradictory.这是矛盾的。
You say you want the code to apply to “1000 rows in excel” but your code refers to 100 rows only (“B1:B100”)您说您希望代码应用于“excel 中的 1000 行”,但您的代码仅指 100 行(“B1:B100”)
You say you want it to trigger the sub when a cell in “column G” changes – but your code refers to column B您说您希望它在“G 列”中的单元格发生更改时触发子 - 但您的代码引用了 B 列
As such, it's difficult for people to understand what your intention really is因此,人们很难理解你的真正意图
Your code should be formatted as code – by which I mean that when you enter your question, you highlight all your code and select {} to mark it as such您的代码应该格式化为代码——我的意思是,当您输入问题时,您突出显示所有代码并选择 {} 将其标记为这样
Having said that, I'll give you a solution on the assumption that you did take @horst advice and read https://stackoverflow.com/help/how-to-ask .话虽如此,我会假设您确实接受了 @horst 建议并阅读了https://stackoverflow.com/help/how-to-ask ,我会给您一个解决方案。
You don't need to call a sub from within a Private Sub when the task is as simple as this one.当任务如此简单时,您不需要从 Private Sub 中调用 sub。 Assuming you meant Column G, and 1000 rows, this should work for you:假设您的意思是 G 列和 1000 行,这应该适合您:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("G1:G1000"), Target) Is Nothing Then
If Target.Rows.Count > 1 Then Exit Sub
If Target.Value = "No" Then
Range(Target.Offset(0, 1), Target.Offset(0, 19)).Value = "X"
End If
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.