VBA隐藏列,如果 [英] VBA hide column if

查看:49
本文介绍了VBA隐藏列,如果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望您可以帮助我解决Excel VBA中遇到的这一挑战.我不确定如何解释这一点,但我会尽力而为.

I hope you can help me with this challenge that I have in Excel VBA. I'm not sure how to explain this, but I'll do my best.

如果两列在单元格"D4"和单元格"E4"中包含相同的文本,我想隐藏"两列.

I want to "hide" two columns if, the two columns contains the same text in both cell "D4" and cell "E4".

列中的文本将始终不相同.有时在两个"D4"和""E4",有时两个单元格中的文本均为"NOTEST",有时再次可能只有两个单元格中的一个具有文本,这意味着不应隐藏该列.

The text in the columns will not always be the same. Sometimes it's fx "TEST" in both of them "D4" & "E4", sometimes the text is "NOTEST" in both cells, and sometimes again it might only have the text in one out of the two cells, which means the column shouldn't be hidden.

如果解释不力,请让我知道,我将尝试以不同的方式解释它.

If the explanation is to weak, please let me know and I'll try to explain it differently.

提前谢谢!

推荐答案

您可以使用以下Sub.

You can use the following Sub.

Sub ShowColumns()
Dim firstCaseToCheck As String
Dim secondCaseToCheck As String
Dim nameOfYourSheet As String

firstCaseToCheck = "D4"
secondCaseToCheck = "E4"
nameOfYourSheet = "Name Of Your Sheet"

With ThisWorkbook.Sheets(nameOfYourSheet)
    If (.range(firstCaseToCheck) = .range(secondCaseToCheck)) Then
        .range(Split(Cells(1, .range(firstCaseToCheck).Column).Address(True, False), "$")(0) & ":" & _
                Split(Cells(1, .range(secondCaseToCheck).Column).Address(True, False), "$")(0)).EntireColumn.Hidden = True
    Else
        .range(Split(Cells(1, .range(firstCaseToCheck).Column).Address(True, False), "$")(0) & ":" & _
                Split(Cells(1, .range(secondCaseToCheck).Column).Address(True, False), "$")(0)).EntireColumn.Hidden = False
    End If
End With
End Sub

您可以在 firstCaseToCheck secondCaseToCheck 中选择两个单元格,并使用 nameOfYourSheet 选择工作表.这样您就可以毫无问题地进行更改.

You can choose your two cells in firstCaseToCheck and secondCaseToCheck and choose your sheet with nameOfYourSheet. This will let you change all this without problem.

这篇关于VBA隐藏列,如果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆