如何在多个Excel工作表上运行以下代码?

时间:2016-03-20 10:28:04

标签: windows macos vba excel-vba excel-2010

我有一个代码,我想在多张纸上使用,除了一张纸。但将代码应用于alle表也没关系。 这是我想要调整的代码。我目前已将其应用于OS X中的Excel 2011,但我想将其用于Windows中的Excel 2010。

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then

Dim the_selection As String
Dim month_in_review As String

the_selection = Sheet1.Range("A1")

    Dim Rep As Integer
    For Rep = 2 To 379
        the_column = GetColumnLetter_ByInteger(Rep)
        month_in_review = Sheet1.Range(the_column & "1")
            If the_selection = month_in_review Then
            Sheet1.Range(the_column & ":" & the_column).EntireColumn.Hidden = False
            Else
            Sheet1.Range(the_column & ":" & the_column).EntireColumn.Hidden = True
            End If
    Next Rep

End If
End Sub

在模块中,我有以下代码:

Public Function GetColumnLetter_ByInteger(what_number As Integer) As String
GetColumnLetter_ByInteger = ""

MyColumn_Integer = what_number

    If MyColumn_Ineger <= 26 Then
        column_letter = ChrW(64 + MyColumn_Integer)
    End If


If MyColumn_Integer > 26 Then
    column_letter = ChrW(Int((MyColumn_Integer - 1) / 26) + 64) & ChrW(((MyColumn_Integer - 1) Mod 26) + 65)

End If

GetColumnLetter_ByInteger = column_letter

End Function

3 个答案:

答案 0 :(得分:0)

您可以使用for each loop遍历所有工作表,并检查工作表名称是否应跳过。然后将您的代码应用到所选的工作表上。

类似的东西:

Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> "Skip Sheet" Then
            Dim the_selection As String
            Dim month_in_review As String

            the_selection = ws.Range("A1")

            Dim Rep As Integer
            For Rep = 2 To 379
                the_column = GetColumnLetter_ByInteger(Rep)
                month_in_review = ws.Range(the_column & "1")
                If the_selection = month_in_review Then
                    ws.Range(the_column & ":" & the_column).EntireColumn.Hidden = False
                    Else
                    ws.Range(the_column & ":" & the_column).EntireColumn.Hidden = True
                    End If
            Next Rep
        End If
    Next ws
End If
End Sub

我不完全确定你想要达到的目标,所以我把ws放在Sheet1的位置。

答案 1 :(得分:0)

如果列的第一个单元格与放置此代码的工作表的单元格A1不同,此示例将显示/隐藏所有其他工作表中的列:

Private Sub Worksheet_Change(ByVal Target As Range)

  ' exit if not cell A1
  If Target.row <> 1 Or Target.column <> 1 Then Exit Sub

  Dim sheet As Worksheet
  Dim the_selection As String
  Dim month_in_review As String
  Dim column As Integer

  the_selection = Target.Value

  ' iterate all the sheets
  For Each sheet In ThisWorkbook.Worksheets

    ' skip this sheet
    If Not sheet Is Me Then

      ' iterate the columns
      For column = 2 To 379

        ' get the first cell of the column
        month_in_review = sheet.Cells(1, column).Value

        ' hide or show the column if it's a match or not
        sheet.Columns(column).Hidden = month_in_review <> the_selection
      Next

    End If
  Next

End Sub

答案 2 :(得分:0)

如果您要求一张纸来检测单元格的变化&#34; A1&#34;然后在多张纸上隐藏/取消隐藏列,那么您问题的先前答案将很好地为您服务。

另一方面,如果您要求检测单元格的变化&#34; A1&#34;在任何工作表上,然后隐藏/取消隐藏更改的工作表上的列,然后下面的代码将适合您。它访问Workbook级别的Workbook_SheetChanged事件。

关于代码的几点:

  1. 您可以使用带有.Cell属性的整数或地址值来引用单元格,因此Sheet1.Cells(1, 1)Sheet1.Cells(1, "A")相同。这同样适用于.Columns属性。因此,没有必要将整数值转换为字符串。请参阅@Florent B的答案以获得一个很好的例子。
  2. 尽可能减少循环表交互,因为这些非常耗时。因此,不是循环遍历列并单独隐藏/取消隐藏每个列,您可以将它们分配到循环中的范围,然后在循环结束时一次性隐藏/取消隐藏范围。如果必须在循环的每次迭代中与工作表交互,则在循环开始之前将Application.ScreenUpdating属性设置为false。下面的示例代码中有此属性的示例。
  3. 将其放入Workbook模块:

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        Const TARGET_ADDRESS As String = "A1"
        Dim cell As Range
        Dim hiddenCols As Range
        Dim unhiddenCols As Range
        Dim selectedMonth As String
        Dim monthInReview As String
        Dim c As Integer
    
        'Ignore event if not a target worksheet
        If Sh.Name = "Not Wanted" Then Exit Sub
    
        'Ignore event if not in target range
        Set cell = Target.Cells(1)
        If cell.Address(False, False) <> TARGET_ADDRESS Then Exit Sub
    
        'Criteria met, so handle event
        selectedMonth = CStr(cell.Value)
        For c = 2 To 379
            Set cell = Sh.Cells(1, c)
            monthInReview = CStr(cell.Value)
            'Add cell to hidden or unhidden ranges
            If monthInReview = selectedMonth Then
                If unhiddenCols Is Nothing Then
                    Set unhiddenCols = cell
                Else
                    Set unhiddenCols = Union(unhiddenCols, cell)
                End If
            Else
                If hiddenCols Is Nothing Then
                    Set hiddenCols = cell
                Else
                    Set hiddenCols = Union(hiddenCols, cell)
                End If
            End If
        Next
    
        'Hide and unhide the cells
        Application.ScreenUpdating = False 'not really needed here but given as example
        If Not unhiddenCols Is Nothing Then
            unhiddenCols.EntireColumn.Hidden = False
        End If
        If Not hiddenCols Is Nothing Then
            hiddenCols.EntireColumn.Hidden = True
        End If
        Application.ScreenUpdating = True
    
    End Sub