英文:
VBA Excel Hide Column based on ANY value present from range of rows
问题
I saw a post but couldn't comment that I was able to successfully use the VBA code for after hours of attempt. I am on a MAC.
chris neilsen wrote this solution for this post: Link
I changed the original code from the link above to hide a column. It works if the value of 2 is present in my rows. However, the issue is if I change the next row to a 1, the first 2 found is ignored, and the row is hidden again, as if the entire code is only focusing on the new value. Maybe the For each loop is wrong?
The goal of my sheet is a company quote form. Users will select pre-selected options (1 or 2 in this case), and the code is meant to search "If any value of 2 is found in any of these rows, display a conditional column for them to fill out). User forms are not available on my 365Mac version. This code works great except this one bug.
Private Sub Macro2(ByVal Target As Range)
Dim RangeOfInterest As Range
Dim TargetRangeOfInterest As Range
Dim cl As Range
Dim SearchValue As Variant
'I want to search for the value 2
'~~~ you probably want to search for the number 2, not the string "2"
SearchValue = 2
Set RangeOfInterest = Me.Range("I13:I20") '<~~ adjust to suit your needs
Set TargetRangeOfInterest = Application.Intersect(RangeOfInterest, Target)
If Not TargetRangeOfInterest Is Nothing Then
With Me.Columns("U").EntireColumn
For Each cl In TargetRangeOfInterest.Cells
If cl.Value2 = SearchValue Then
' Reads are faster than Writes, so only change the Hidden state if required
If .Hidden Then
.Hidden = False 'I want to reveal the hidden row 10 if Value 2 is found
End If
Else 'If there is no value of 2
If Not .Hidden Then
.EntireColumn.Hidden = True 'hide row 10
End If
End If
Next
End With
End If
End Sub
英文:
I saw a post but couldn't comment that I was able to successfully use the VBA code for after hours of attempt. I am on a MAC.
chris neilsen wrote this solution for this post: Link
I changed the original code from the link above to hide a column. It works if the value of 2 is present in my rows. However, the issue is if I change the next row to a 1, the first 2 found is ignored and the row is hidden again, as if the entire code is only focusing on the new value. Maybe the For each loop is wrong?
The goal of my sheet is a company quote form. Users will select pre-selected options (1 or 2 in this case), and the code is meant to search "If any value of 2 is found in any of these rows, display a conditional column for them to fill out). User forms is not available on my 365Mac version. This code works great except this one bug.
Private Sub Macro2(ByVal Target As Range)
Dim RangeOfInterest As Range
Dim TargetRangeOfInterest As Range
Dim cl As Range
Dim SearchValue As Variant
'I want to search for the value 2
'~~~ you probably want to search for the number 2, not the string "2"
SearchValue = 2
Set RangeOfInterest = Me.Range("I13:I20") '<~~ adjust to suit your needs
Set TargetRangeOfInterest = Application.Intersect(RangeOfInterest, Target)
If Not TargetRangeOfInterest Is Nothing Then
With Me.Columns("U").EntireColumn
For Each cl In TargetRangeOfInterest.Cells
If cl.Value2 = SearchValue Then
' Reads are faster than Writes, so only change the Hidden state if required
If .Hidden Then
.Hidden = False 'I want to reveal the hidden row 10 if Value 2 is found
End If
Else 'If there is no value of 2
If Not .Hidden Then
.EntireColumn.Hidden = True 'hide row 10
End If
End If
Next
End With
End If
End Sub
答案1
得分: 0
工作表更改:隐藏列
表模块,例如Sheet1
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
HideMyColumn Target
End Sub
标准模块,例如Module1
Option Explicit
Sub HideMyColumn(ByVal Target As Range)
Const TARGET_RANGE As String = "I13:I20"
Const SEARCH_VALUE As Variant = 2
Const HIDE_COLUMN As Variant = "U"
Dim ws As Worksheet: Set ws = Target.Worksheet
Dim trg As Range: Set trg = ws.Range(TARGET_RANGE)
Dim irg As Range: Set irg = Intersect(trg, Target)
If irg Is Nothing Then Exit Sub
With ws.Columns(HIDE_COLUMN)
If IsNumeric(Application.Match(SEARCH_VALUE, trg, 0)) Then
If .Hidden Then .Hidden = False
Else
If Not .Hidden Then .Hidden = True
End If
End With
End Sub
英文:
A Worksheet Change: Hide Column
Sheet Module e.g. Sheet1
<!-- language: lang-vb -->
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
HideMyColumn Target
End Sub
Standard Module e.g. Module1
<!-- language: lang-vb -->
Option Explicit
Sub HideMyColumn(ByVal Target As Range)
Const TARGET_RANGE As String = "I13:I20"
Const SEARCH_VALUE As Variant = 2
Const HIDE_COLUMN As Variant = "U"
Dim ws As Worksheet: Set ws = Target.Worksheet
Dim trg As Range: Set trg = ws.Range(TARGET_RANGE)
Dim irg As Range: Set irg = Intersect(trg, Target)
If irg Is Nothing Then Exit Sub
With ws.Columns(HIDE_COLUMN)
If IsNumeric(Application.Match(SEARCH_VALUE, trg, 0)) Then
If .Hidden Then .Hidden = False
Else
If Not .Hidden Then .Hidden = True
End If
End With
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论