简体   繁体   English

Excel VBA-隐藏/取消隐藏“ na”列

[英]Excel VBA - hide/unhide “na” columns

First off, i want you to know that im very new to VBA. 首先,我想让您知道VBA的新功能。 I can manage to copy and paste certain data and adapt ranges according to my needs but that is all. 我可以根据需要复制和粘贴某些数据并调整范围,仅此而已。 Now to my problem: 现在我的问题:

I have a row consisting of 31 columns (E27:AI27). 我有31列(E27:AI27)组成的行。 All the cells within this range get their input (a number from 1-31) from other cells outside of this range, Let's say from a couple of drop down lists. 这个范围内的所有单元格都从该范围外的其他单元格获取输入(从1-31的数字),比方说从几个下拉列表中获取。 Depending on how I twerk these drop-down lists the sequence of the numbers in that specific range (E27:AI27) can look like the following alternatives: 根据我下拉的方式,这些下拉列表列出了该特定范围内的数字顺序(E27:AI27)可能类似于以下替代方法:

Alt. Alt。 01: 1 2 3 4 5................................29 30 31 (31 cells total) 01:1 2 3 4 5 ................................ 29 30 31(总共31个单元格)

Alt. Alt。 02: 2 3 4 5 6...........................29 30 31 na (31 cells total) 02:2 3 4 5 6 .................................... 29 30 31 na(总共31个单元格)

Alt. Alt。 03: 3 4 5 6 7...........................30 31 na na (31 cells total) 03:3 4 5 6 7 .................................... 30 31 na na(总共31个单元格)

Alt. Alt。 04: 4 5 6 7 8......................30 31 na na na (31 cells total) 04:4 5 6 7 8 ...................... 30 31 na na na(总共31个单元格)

etc etc etc. 等等等等

Alt. Alt。 30: 30 31 na......................na na na na na (31 cells in total) 30:30 31 na ...................... na na na na na(总共31个单元格)

Alt. Alt。 31: 31 na na..................... na na na na na (31 cells in total) 31:31 na na ........................... na na na na na(总共31个单元格)

Now what I want to do is to automatically, temporarily, hide those columns that contain string "na" within them. 现在,我要做的是自动地,暂时地隐藏其中包含字符串“ na”的那些列。 When I then re-twerk my drop down lists again, i want to be able to unhide those columns that change back from "na" to a number. 然后,当我再次重新更改下拉列表时,我希望能够取消隐藏从“ na”变为数字的那些列。 For example, i want to be able to shift between alternative 1 and alternative 2 (see above), depeding on how i twerk my drop down lists. 例如,我希望能够在备选方案1和备选方案2之间切换(请参见上文),以了解我如何下拉列表。 Shifting between these two alternatives would mean that AI27 would go from showing, to hiding, to showing, to hiding and so on (while E27AH27 would all be showing since they would all have numbers inside then, 1-30. 在这两种选择之间切换意味着AI27从显示,隐藏,显示,隐藏等等(而E27AH27都将显示,因为它们内部都有1-30的数字)。

Last but not least, there are two drop down lists that control the values of the cells E27:AI27. 最后但并非最不重要的一点是,有两个下拉列表控制着单元格E27:AI27的值。 Drop down list called month (C18) and drop down list called day (D18). 下拉列表称为月(C18)和下拉列表称为日(D18)。 The former (C18) sets the days for a month. 前者(C18)将日期设置为一个月。 If C18=February then there will be 28 days and the last two columns (AH:AI) would be "na". 如果C18 = February,那么将有28天,最后两列(AH:AI)为“ na”。 In addition to that the latter drop down list (D18) sets the start day, namly the first number (in cell E27) in the sequence. 除了后面的下拉列表(D18)设置开始日期外,还命名序列中的第一个数字(在单元格E27中)。 If D18=21, then the alternative for february month would be: 如果D18 = 21,那么二月份的替代方案将是:

21 22 23 24 25 26 27 28 na na na na na na na na na na na na.... (containing 23 na) 21 22 23 24 25 26 27 28 na na na na na na na na na na na na ....(包含23 na)

Can anyone please help me set up an easy and short VBA code for this, if possble? 如果可能的话,有人可以帮我建立一个简单且简短的VBA代码吗? Highly appreciated 高度赞赏

Try this one : 试试这个:

Private Sub Worksheet_Change(ByVal Target As Range)
    'change the "E23" with the cell with your dropdown
    If Not Intersect(Target, Range("E23")) Is Nothing Then
        Call UnhideHideColumns
    End If
End Sub

Sub UnhideHideColumns()

Dim bytColumnCheck As Byte
Dim blnNeedToUnhide As Boolean
Dim intFirstNAColumn As Integer

'check all columns and find out if they are already hidden
For bytColumnCheck = 5 To 35
    'if any of the columns is hidden, unhide all the columns to the right and exit loop
    If Cells(1, bytColumnCheck).EntireColumn.Hidden = True Then
        Range(Cells(1, bytColumnCheck), Cells(1, 35)).EntireColumn.Hidden = False
        Exit For
    End If
Next bytColumnCheck

'find first occurence of "na" in values, if any exists
If Not Cells.Find("na", LookIn:=xlValues, after:=Range("E27")) Is Nothing Then
    intFirstNAColumn = Cells.Find("na", LookIn:=xlValues).Column

    'now hide the columns to the right of the first "NA", including
    Range(Cells(1, intFirstNAColumn), Cells(1, 35)).EntireColumn.Hidden = True
End If

End Sub

Note that the Call UnhideHideColumns will be called every time the cell with the dropdown changes, even let's say from Set1 to Set1, or from 5 to 5. So it can trigger the macro unnecessary often. 请注意,每次具有下拉菜单的单元格发生更改时,都会Call UnhideHideColumns ,甚至可以说是从Set1到Set1,或者从5到5。所以它可以经常触发不必要的宏。

This worked for me: 这为我工作:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim i As Integer
    For i = 9 To 40: Cells(28, i).EntireColumn.Hidden = Cells(28, i) = "na": Next

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM