vba 행 높이, 열 높이 일괄정렬

vba 행 높이, 열 높이 일괄정렬

 

첨부된 bas파일을 엑셀 vba상에 가져오기 한 후 바로 사용가능 (컨트롤 j)

여러 셀을 선택한 상태에서 컨트롤 j를 누르면 전체 높이(너비)를 조정 가능

-주의: 병합된 셀이 포함되면 에러가 남

– 한 로우 또는 한 컬럼의 셀들을 선택하고 실행해야 함

– 너비인지 높이인지는 알아서 정해짐. 컬럼을 많이 선택했을 경우 높이를 결정하고, 로우를 많이 선택했을 경우 너비를 결정함.

– 0 이라고 입력하면 현재 높이(너비)를 출력

– 주의: 거꾸로 드래그 하면 오작동함. (반드시 좌에서 우로, 위에서 아래로 드래그 해서 선택해야 함)

 

 

Attribute VB_Name = “Module1”

Sub Macro2()
Attribute Macro2.VB_ProcData.VB_Invoke_Func = “j\n14”

‘ Macro2 Macro

‘ 바로 가기 키: Ctrl+j

basic_order = “row” ‘기준
    user_input = 0
   
    start_col = ActiveCell.Column ‘현재 열
    end_col = ActiveCell.Column + Selection.Columns.Count – 1
   
    start_row = ActiveCell.Row ‘현재 로우
    end_row = ActiveCell.Row + Selection.Rows.Count – 1
   
   
    If (end_row – start_row) > (end_col – start_col) Then
        basic_order = “row”
        user_input = InputBox(“목표 높이. 0 을 입력하면 높이의 합 출력”)
    Else
        basic_order = “col”
        user_input = InputBox(“목표 너비. 0 을 입력하면 너비의 합 출력”)
    End If
   
    ‘////////////////////////////////
   
    If (user_input = “”) Then
        End
    End If
   
    target = Int(user_input)
   
    If target < 0 Then
        End
    End If
       
   

   
    ‘/////////////////////////////////////////////

   
   
   
    ‘/////////////////////////////////////////
   
    ‘현재높이
   
    current = 0
    If basic_order = “row” Then
        For i = start_row To end_row
           
            Cells(i, start_col).Select
            current = current + Selection.RowHeight
       
        Next i
       
        Count = end_row – start_row + 1
       
    Else
        For i = start_col To end_col
           
            Cells(start_row, i).Select
            current = current + Selection.ColumnWidth
       
        Next i
       
        Count = end_col – start_col + 1
    End If
   
   
   
    If target = 0 Then
        Cells(start_row, start_col).Select
        MsgBox (“current ” & basic_order & “: ” & current)
        End
    End If
   
   
    ‘////////////////////////////////////////////
   
   
   
    ‘각자 채워야할 양 : (타겟 – 현재 높이)/ 갯수
    total_amount = target – current
    up_amount = total_amount / Count
   
   
    Call row_height_up(basic_order, start_row, end_row, start_col, end_col, up_amount)

   
End Sub

 

 

Function row_height_up(basic_order, start_row, end_row, start_col, end_col, up_amount)

    Dim strArr As String
    strArr = “”
   
    If basic_order = “row” Then
   
        For i = start_row To end_row
   
            Cells(i, start_col).Select
           
            temp = Selection.RowHeight ‘기존값
            Selection.RowHeight = temp + (up_amount) ‘증가된 값
           
            strArr = strArr & basic_order & Str(i) & “(” & Str(temp) & ” => ” & Str(Selection.RowHeight) & “), “
       
        Next i
   
    Else
      For i = start_col To end_col
     
            Cells(start_row, i).Select
           
            temp = Selection.ColumnWidth ‘기존값
            Selection.ColumnWidth = temp + (up_amount) ‘증가된 값
           
            strArr = strArr & basic_order & Str(i) & “(” & Str(temp) & ” => ” & Str(Selection.ColumnWidth) & “), “
       
        Next i
   
    End If
   
    MsgBox (strArr)

End Function