Excel Vba宏按顺序执行多项操作

时间:2017-03-10 23:51:03

标签: excel vba excel-vba

我试图让1个宏被推广,因此它可以用于多个报告。问题是它可能并不总是拥有一切,即:一个将有一个标题,一个将没有,一个将有一个列"这一个"一个不会,一个将有5张或不同的名称,一个将有13 ..

我想做以下事情:   1.始终保持所有行和列自动调整大小   2.总是冻结第一行   3.总是删除标题行(如果有的话)   4.始终将标签更改为图案(每张纸都有红色,蓝色,绿色,黄色,橙色重复)   5.按列名隐藏列列表(这一列,另一列,无论报告在何处,都是另一个)   6.确保冻结的顶行是可过滤的(如按ctrl shift l)

我认为这是在正确的轨道,但它没有做到最好,任何建议,使其成为错误证明(如果它没有正确的选项卡或列名称,不要失败)和更好方法一个接一个地调用所有单个宏。

谢谢!

Sub Auto_Size_Columns()
' Autosize the column after filling it all in.
Columns("A:CO").Select
Columns("A:CO").EntireColumn.AutoFit
Range("A1").Select
Selection.AutoFilter
Call Freeze_Top_Panes
End Sub

Sub Freeze_Top_Panes()
Application.ScreenUpdating = False
Rows("2:2").Select
ActiveWindow.FreezePanes = True
Application.ScreenUpdating = True
Call Auto_Size_Columns_Again
End Sub

Sub Auto_Size_Columns_Again()
' Autosize the column after filling it all in.

Columns("A:CO").Select
Columns("A:CO").EntireColumn.AutoFit
Range("A1").Select
Selection.AutoFilter
Call Delete_Header_Row
End Sub

Sub Delete_Header_Row()
'delete the extra header row
Rows("1:1").Select
Selection.Delete shift:=xlUp
Range("A1").Select
Call Tab_Color_Change
End Sub

Sub Tab_Color_Change()
Sheets("Sheet2").Tab.ColorIndex = 3
Sheets("Sheet3").Tab.ColorIndex = 4
Sheets("Sheet4").Tab.ColorIndex = 5
Sheets("Sheet5").Tab.ColorIndex = 6
Sheets("Sheet6").Tab.ColorIndex = 7
Sheets("Sheet7").Tab.ColorIndex = 8
Sheets("Sheet8").Tab.ColorIndex = 9
Sheets("Sheet9").Tab.ColorIndex = 10
Sheets("Sheet10").Tab.ColorIndex = 11
Sheets("Sheet11").Tab.ColorIndex = 12
Sheets("Sheet12").Tab.ColorIndex = 13
Sheets("Sheet13").Tab.ColorIndex = 14
Call Hide_Columns
End Sub

Sub Hide_Columns()
Dim s As Worksheet, N As Long, i As Long
For Each s In Worksheets
    s.Activate
    N = Cells(1, Columns.Count).End(xlToLeft).Column
    For i = 1 To N
        If Left(Cells(1, i).Value, 6) = "this one" Then
            Cells(1, i).EntireColumn.Hidden = True
        End If
    Next i
Next s
Call Auto_Size_Columns_Last
End Sub

Sub Auto_Size_Columns_Last()
' Autosize the column after filling it all in.

Columns("A:CO").Select
Columns("A:CO").EntireColumn.AutoFit
Range("A1").Select
Selection.AutoFilter
End Sub

1 个答案:

答案 0 :(得分:2)

我想首先开始您想要了解如何遍历当前工作簿中的所有页面。

Sub DoSheetActions()
    Dim wb As Workbook
    Set wb = ThisWorkbook
    Dim ws As Worksheet
    Dim CurrentColorIndex As Integer
    For Each ws In wb.Sheets
        'Execute commands for each sheet
        'To do this, you'll need to pass the each sheet to the Subroutine.
        Auto_Size_Columns ws

        'Increase the ColorIndex each time we iterate over a new sheet
        CurrentColorIndex = CurrentColorIndex + 1
        'Retrieve a new ColorIndex
        ws.Tab.ColorIndex = Tab_Color_Change(CurrentColorIndex)
    Next ws
End Sub

现在我们循环遍历每张工作表,我们可以逐个调用每张工作表上的任意数量的操作。 Auto_Size_Columns非常简单。

Sub Auto_Size_Columns(ws As Worksheet)
    'And to be honest, Auto_Size_Columns() probably 
    'doesn't need to be a Sub as it only has one statement.
    'Using Worksheet.UsedRange we can find
    'all the columns (as long as there isn't a gap)
    ws.UsedRange.Columns.AutoFit
End Sub

接下来,我们可以通过使用函数来确定工作簿中N个工作表的工作表颜色索引,以根据我们迭代的工作表数确定要使用的颜色。 over(CurrentColorIndex)。

'Passing ColorIndex byref so this function can change the value
'You could also use a Switch to get a more robust method to
'determine which color index to return
Function Tab_Color_Change(ByRef ColorIndex As Integer) As Integer
    'So if the value is less than 3, it starts off as 3.
    If (ColorIndex < 3) Then
        ColorIndex = 3
    'If the value is greater than 14, start back at 3.
    ElseIf (ColorIndex > 14) Then
        ColorIndex = 3
    End If

    'Return the color
    Tab_Color_Change = ColorIndex
End Function

我唯一没有涉及的是删除标题(如果它们存在)。但实质上,要确定是否存在标题,您需要一个存在于所有工作表中的公共标题列,如果它存在,您可以安全地删除第1行。否则,您最终会删除您的数据

我希望这能为您提供一种不同的方式来看待事物,并为您未来的努力提供一个起点。欢呼声。