Tag Archives: VBA

엑셀에서 방위 계산하는 VBA코드

데이터 샘플이 이런 형태

A            B        c

WindSpeed1 WindDir1
6.0 276.9
5.8 346.1
8.1 276.3
7.1 253.4
4.9 256.1
6.8 275.4

Sub ChangeDir()
    Dim num As Double
    num = ActiveCell.Offset(0, -1).Value
    ' ActiveCell.Offset(0,-1).Value
    Debug.Print (num)

    ' If IsEmpty(num) Or num = 0 Then
    If ActiveCell.Offset(0, -2).Value < 0.4 Then
        ActiveCell.Value = "Calm"
    ElseIf IsEmpty(ActiveCell.Offset(0, -1).Value) Then
        ActiveCell.Value = ""
    ElseIf Scope(11.25, 33.75, num) Then
        ActiveCell.Value = "NNE"
    ElseIf Scope(33.75, 56.25, num) Then
        ActiveCell.Value = "NE"
    ElseIf Scope(56.25, 78.75, num) Then
        ActiveCell.Value = "ENE"
    ElseIf Scope(78.75, 101.25, num) Then
        ActiveCell.Value = "E"
    ElseIf Scope(101.25, 123.75, num) Then
        ActiveCell.Value = "ESE"
    ElseIf Scope(123.75, 146.25, num) Then
        ActiveCell.Value = "SE"
    ElseIf Scope(146.25, 168.75, num) Then
        ActiveCell.Value = "SSE"
    ElseIf Scope(168.75, 191.25, num) Then
        ActiveCell.Value = "S"
    ElseIf Scope(191.25, 213.75, num) Then
        ActiveCell.Value = "SSW"
    ElseIf Scope(213.75, 236.25, num) Then
        ActiveCell.Value = "SW"
    ElseIf Scope(236.25, 258.75, num) Then
        ActiveCell.Value = "WSW"
    ElseIf Scope(258.75, 281.25, num) Then
        ActiveCell.Value = "W"
    ElseIf Scope(281.25, 303.75, num) Then
        ActiveCell.Value = "WNW"
    ElseIf Scope(303.75, 326.25, num) Then
        ActiveCell.Value = "NW"
    ElseIf Scope(326.25, 348.75, num) Then
        ActiveCell.Value = "NNW"
    Else
        ActiveCell.Value = "N"
    End If

    num = ActiveCell.Offset(1, 0).Select

End Sub

Function Scope(small As Double, big As Double, num As Double)

    If small <= num And num < big Then
        Scope = True
    Else
        Scope = False
    End If

End Function