简体   繁体   English

如何使用vba在Excel 2007中一次隐藏多个列

[英]How to hide multiple columns at once in Excel 2007 using vba

I would like to hide multiple columns inside an excel worksheet. 我想隐藏excel工作表中的多个列。 This works pretty fine using: 使用时效果非常好:

ActiveSheet.Range("R10:CO10").EntireColumn.Hidden = True ActiveSheet.Range(“R10:CO10”)。EntireColumn.Hidden = True

"R10" is the first and fix column to hide. “R10”是第一个隐藏的修复列。 The second column and all columns between to hide will be dynamically determined depending on it´s date value. 第二列和隐藏之间的所有列将根据其日期值动态确定。

Sample Coding: 样本编码:

Private Sub Worksheet_Activate()
    Dim c As Range
    Dim start As String
    Dim ende As String
    start = "R10"
    ende = "CO10"
    Dim d As Date
    d = Date
    For Each c In Range("R10:HU10")
        If c = (d - 8) Then
            ende = **how to assign???**
        End If
        If c = (d - 7) Then
            Application.Goto c, True
        End If
    Next c
    'ActiveSheet.Range(**"start:ende"**).EntireColumn.Hidden = True
End Sub

Row 10 holds date values and I would like to hide all columns which dates are older than 7 days and I can´t find any hints about hiding multiple columns using variables or with column identifier or the number of the column etc. 第10行保存日期值,我想隐藏日期超过7天的所有列,我找不到任何关于使用变量或列标识符或列号等隐藏多个列的提示。

The use of variables ends up in runtime error 1004. 变量的使用最终在运行时错误1004中结束。

As Scott pointed out, my first answer was not complete. 斯科特指出,我的第一个答案并不完整。 You can use the following function: 您可以使用以下功能:

Function GetColChars(col As Integer) As String

    Dim coldown As Integer 'Column Countdown
    Dim colrem As Integer 'Coumn Value Remaining
    Dim colname As String 'Temporary String value for column name

    Const alphanums = 26
    Const aposition = 64
    coldown = col
    colname = ""

    While coldown > 0

        colrem = coldown Mod alphanums
        If colrem = 0 Then colrem = 26
        newchar = Chr(64 + colrem)
        colname = newchar & colname
        coldown = Int((coldown - 1) / alphanums)

    Wend

    GetColChars = colname

 End Function

Then call the function to get the column letters: 然后调用函数来获取列字母:

ende = GetColChars(c.column)

I have tested Scotts solution approach and finally got it working. 我测试了Scotts解决方案的方法,最后让它运行起来。 In that case that some other people will struggle with the same problems, getting a working solution, here is my solution: 在这种情况下,其他人会遇到同样的问题,找到一个有效的解决方案,这是我的解决方案:

Private Sub Worksheet_Activate()

Dim lastDateRangeColumn As Range
Dim givenDateRange As Range 
Set givenDateRange = ActiveSheet.Range("R10:HU10")
Dim firstDateRangeColumn As Range 
Set firstDateRangeColumn = ActiveSheet.Range("R10")

Dim todaysDate As Date: todaysDate = Date

For Each tempDateRangeColumn In givenDateRange
    If tempDateRangeColumn < (todaysDate - 7) Then
        Set lastDateRangeColumn = ActiveSheet.Range(tempDateRangeColumn.Address)
    End If
    If tempDateRangeColumn = (todaysDate - 7) Then
        Application.Goto tempDateRangeColumn, True
    End If
Next tempDateRangeColumn

Dim firstColumnNumber As Long  
Dim lastColumnNumber  As Long
firstColumnNumber = Range(firstDateRangeColumn.Address).Column  
lastColumnNumber  = Range(lastDateRangeColumn.Address).Column

Dim rangeToBeHidden   As Range 
Set rangeToBeHidden = Range(Cells(1, firstColumnNumber), Cells(1, lastColumnNumber))

rangeToBeHidden.EntireColumn.Hidden = True

End Sub

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

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