2017-03-10 1 views
0

매크로가 일반화되어 여러 보고서에서 작동하도록하려고합니다. 문제는 그것이 항상 모든 것을 가질 수는 없다는 것입니다. 즉, 하나는 헤더를 가지지 않을 것입니다. 하나는 "이 하나"의 컬럼을 갖지 않을 것이며, 하나는 5 개의 시트를 가지거나 13 개의 다양한 이름을 가질 것입니다.Excel Vba 매크로가 여러 가지 작업을 수행합니다

다음을 원합니다. 1. 항상 모든 행과 열을 자동으로 유지하십시오. 2. 항상 첫 번째 행을 고정하십시오. 3. 항상 헤더 행을 삭제하십시오 (있는 경우) 4. 항상 탭을 패턴 (각 시트마다 빨간색, 파란색, 녹색, 노란색, 주황색 반복) 5. 열 이름별로 열 목록을 숨 깁니다 (이 중 하나, 다른 하나는 보고서에 관계 없음) 6. 확인하십시오. 고정 된 맨 위 행을 필터링 할 수 있음 (Ctrl 키를 누른 상태에서 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 

다음으로, 우리는 우리가 (CurrentColorIndex) 이상 반복 얼마나 많은 시트의 기반으로 사용하려면 어떤 색을 결정하는 기능을 사용하여 통합 문서의 시트의 N 번호에 대한 시트의 색상 인덱스를 설정 해결할 수 있습니다.

'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을 제거 할 수 있습니다. 그렇지 않으면 데이터 삭제가 어려워집니다.

나는 이것이 당신에게 미래의 노력을위한 출발점과 사물을 보는 다른 방법을 제공하기를 바랍니다. 건배.

관련 문제