728x90

셀의 셀서식을 내맘대로 변경하기

해야할일
1. 엑셀서식을 숫자로
2. 음수는 - 빨간색으로
3. -(하이픈) 인경우는 0으로
4.#NA 에러표시는 0으로
5. 4열부터 +50열까지만 변경(전체행하기엔 속도가 느려짐)
6.랜덤하게 셀을 선택해도 변경가능

완성
Sub a숫자형식변경a()
    Dim selectedRows As Range
    Dim row As Range
    Dim selectedRowNumbers As String
    
    ' Check if any rows are selected
    On Error Resume Next
    Set selectedRows = Selection.Rows
    On Error GoTo 0
    
    If selectedRows Is Nothing Then
        MsgBox "No rows selected."
        Exit Sub
    End If
        
    ' Loop through each selected row and build the string of row numbers
    For Each row In selectedRows
        iRow = row.row
        icol = 4
        Set rng = Range(Cells(iRow, icol), Cells(iRow, icol + 50))
        For Each cell In rng
            If IsError(cell.Value) Then  '#NA를 0으로
                If cell.Value = CVErr(xlErrNA) Then
                    cell.Value = 0
                End If
            ElseIf cell.Value = "-" Then '-하이픈을 0으로
                cell.Value = 0
            End If
        Next cell
        
        With rng
            .NumberFormat = "#,##0_);[Red](#,##0)" '형식지정
            .HorizontalAlignment = xlCenter
            .Value = .Value ' This line re-evaluates the cell values to apply the formatting
        End With

    Next row
End Sub

결과확인하기

변경전

(매크로실행시 랜덤하게 행 선택)

변경후

#NA 오타(테스트로 임의작성)..#N/A(실제 오류)면 정상동작 확인

 

728x90

+ Recent posts