简体   繁体   English

根据另一个单元格值更改单元格范围

[英]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.

相关问题 根据另一个单元格的值更新一系列单元格 - Updating a range of cells based on the value of another cell 如果另一个单元格的值为“ x”,则将一系列单元格的格式设置为未锁定 - Format a range of cells as unlocked if another cell has a value of “x” 从另一个单元格值中突出显示单元格(动态范围) - Highlight cell from another cells value (dyanmic range) 可以根据另一个单元格的值锁定单元格范围吗? - Possible to Lock Range of cells based on another cell's value? 根据另一个像元范围的值填充像元 - Populating cells based on another cell range's value 如何计算值小于excel中另一个单元格的范围内的单元格? - How to count cells in a range with a value less than another cell in excel? Select 基于在另一个单元格中找到的值的存在的单元格范围 - Select range of cells based on existence of a value found in another cell 循环遍历一系列单元格并更改单元格值的语法 - Syntax to loop through a range of cells and change a cell value 如果范围内只有一个单元格有值,如何将单元格范围内的值复制到另一个单元格? - How to copy the value in a range of cells to another cell if only one cell in the range has a value? 如果区域中只有一个单元格具有值,则将单元格区域中的值复制到另一个单元格 - Copy the value in a range of cells to another cell if only one cell in the range has a value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM