VBA Excel基于行范围中的任何值隐藏列

huangapple go评论104阅读模式
英文:

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
英文:

VBA Excel基于行范围中的任何值隐藏列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
    
    &#39;I want to search for the value 2
    &#39;~~~ you probably want to search for the number 2, not the string &quot;2&quot;
    SearchValue = 2
    
    Set RangeOfInterest = Me.Range(&quot;I13:I20&quot;) &#39;&lt;~~ adjust to suit your needs
    
    Set TargetRangeOfInterest = Application.Intersect(RangeOfInterest, Target)
    If Not TargetRangeOfInterest Is Nothing Then
        With Me.Columns(&quot;U&quot;).EntireColumn
            For Each cl In TargetRangeOfInterest.Cells
                If cl.Value2 = SearchValue Then
                    &#39; Reads are faster than Writes, so only change the Hidden state if required
                    If .Hidden Then
                        .Hidden = False &#39;I want to reveal the hidden row 10 if Value 2 is found
                    End If
                Else  &#39;If there is no value of 2
                    If Not .Hidden Then
                        .EntireColumn.Hidden = True &#39;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 = &quot;I13:I20&quot;
    Const SEARCH_VALUE As Variant = 2
    Const HIDE_COLUMN As Variant = &quot;U&quot;
    
    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

huangapple
  • 本文由 发表于 2023年2月24日 15:48:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553838.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定