Excel VBAmacros来做几件事情

我正在试图让1macros被广义化,所以它将在多个报告中工作。 问题是,它可能并不总是有一切,即:一个将有一个标题不会,一个将有一个“这一个”列不会,一个将有5张或变化的名字,将有13 ..

我想要做到以下几点:1.始终保持所有行和列的自动大小2.始终冻结第一行3.总是删除标题行(如果有的话)4.总是将选项卡更改为模式(红色,蓝色,绿色,黄色,橙色每张重复)5.按列名隐藏列表(这一个,那一个,另一个无论他们在哪里报告)6.确保冻结的顶行是可过滤的(比如按下ctrl shift l)

我认为这是正确的轨道,但它没有尽其所能,任何build议,使其无法certificate(如不失败,如果它没有正确的选项卡或列名称)和更好的方式调用所有个别的macros一个接一个的。

谢谢!!

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 

我想首先你要看看如何遍历当前工作簿中的所有页面。

 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张工作表的颜色索引,以根据我们迭代的工作表(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行。否则,将删除您的数据。

我希望这给你一个不同的方式来看待事情,并为你未来的努力起点。 干杯。