[英]How to automatically hide column based on row value, which is based on an if function from an opposing worksheet?
Please bear with me as I am self-taught and very new to VBA coding. 因为我是自学成才的,并且对VBA编码还很陌生,所以请多多包涵。
I need help with a code for a work project to automatically/dynamically hide a column based on a row value within that column. 我需要有关工作项目代码的帮助,该代码可以基于该列中的行值自动/动态地隐藏该列。 In my example, based on a Yes/No data validation list in an alternate worksheet, the current worksheet will update and return the same "Yes" or "No" value.
在我的示例中,基于备用工作表中的“是/否”数据验证列表,当前工作表将更新并返回相同的“是”或“否”值。 If the answer is "No", I need that column to automatically disappear once the user has chosen that option from the Data validation list.
如果答案为“否”,则当用户从“数据验证”列表中选择该选项后,我需要该列自动消失。
The data set is as follows: The Yes/No result is on Row 3, in the column range B:AR
. 数据集如下:是/否结果在行3中的列范围
B:AR
。 The results of Row 3 are as a result of a transcode formula from another worksheet. 第3行的结果是来自另一个工作表的转码公式的结果。
Bonus points if the coding is truly dynamic, in that was I to add rows above Row 3, the code would automatically move to Row 4. 如果编码是真正动态的,则加分点在于,如果我在第3行上方添加行,则代码将自动移至第4行。
I have scoured the google realms and most code either doesn't update or is strenuously slow. 我已经搜索了Google领域,大多数代码要么没有更新,要么非常缓慢。 An example of several codes I have attempted are below:
我尝试过的几个代码示例如下:
Sub Hide_Columns_Containing_Value()
Dim c As Range
For Each c In Range("B3:AR3").Cells
If c.Value = "No" Then
c.EntireColumn.Hidden = True
End If
Next c
End Sub
So this is what you need to Do: 所以这是您需要做的:
First Define a Named Range
in the worksheet for the Rows you want to check No
in. Benefit of Named Range
is it is Dynamic
, so if you add another row before 3rd row, it will dynamically move the Named Range to 4th Row. 首先在工作表中为要在其中检查
No
的行定义一个Named Range
。 Named Range
好处是它是Dynamic
,因此,如果在第三行之前添加另一行,它将动态将命名范围移动到第四行。
Like this: 像这样:
After Making the named range add this Code to the Worksheet you are on: 在完成命名范围后,将此代码添加到工作表中:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("roww")) Is Nothing Then
For Each cel In Range("roww")
If cel.Value = "No" Then Columns(cel.Column).EntireColumn.Hidden = True
Next
End If
End Sub
-Second Code to be added below the First One. -将第二个代码添加到第一个下面。
Sub hdd()
For Each cel In Range("roww")
If cel.Value = "No" Then Columns(cel.Column).EntireColumn.Hidden = True
Next
End Sub
Make Sure code is on the Correct sheet to work: 确保代码在正确的工作表上可以工作:
This will be a complete Dynamic code. 这将是完整的动态代码。
Hope a little faster 希望更快一点
Sub Hide_Columns_Containing_Value( _
ByVal msg As String, _
ByVal r As Range)
Dim c As Range
Set c = r.Find(msg)
If Not c Is Nothing Then
c.EntireColumn.Hidden = True
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.