2017-03-10 128 views
0
訂單

幾件事情,我試圖讓1個宏一概而論所以它會在多個報告工作。問題是,它可能不總是具有一切即:一個將有一個頭部中的一個不會,一個將有一個塔「這一」一個不會,一個將有5片或變化的名稱一個將具有13 ..Excel的VBA宏不要在

我想做以下事情: 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 
+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個工作簿中的片材的片材的顏色索引使用基於我們已經多少片的遍歷(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行。否則,你會風刪除數據。

我希望這給你一種不同的看待事物的方式和你未來努力的出發點。乾杯。